-
Notifications
You must be signed in to change notification settings - Fork 0
/
stealth.go
66 lines (56 loc) · 1.79 KB
/
stealth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright 2022 Wayback Archiver. All rights reserved.
// Use of this source code is governed by the GNU GPL v3
// license that can be found in the LICENSE file.
package screenshot
import (
"context"
"os"
"github.com/chromedp/cdproto/page"
"github.com/chromedp/chromedp"
)
// JavaScript for stealth
// Copied from https://github.com/chromedp/chromedp/issues/396#issuecomment-503351342
const stealthScript = `(function(w, n, wn) {
// Pass the Webdriver Test.
Object.defineProperty(n, 'webdriver', {
get: () => false,
});
// Pass the Plugins Length Test.
// Overwrite the plugins property to use a custom getter.
Object.defineProperty(n, 'plugins', {
// This just needs to have length > 0 for the current test,
// but we could mock the plugins too if necessary.
get: () => [1, 2, 3, 4, 5],
});
// Pass the Languages Test.
// Overwrite the plugins property to use a custom getter.
Object.defineProperty(n, 'languages', {
get: () => ['en-US', 'en'],
});
// Pass the Chrome Test.
// We can mock this in as much depth as we need for the test.
w.chrome = {
runtime: {},
};
// Pass the Permissions Test.
const originalQuery = wn.permissions.query;
return wn.permissions.query = (parameters) => (
parameters.name === 'notifications' ?
Promise.resolve({ state: Notification.permission }) :
originalQuery(parameters)
);
})(window, navigator, window.navigator);`
func stealth() chromedp.Action {
enabled := os.Getenv("CHROMEDP_STEALTH")
if enabled == "true" || enabled == "yes" || enabled == "on" {
return chromedp.Tasks{
chromedp.ActionFunc(func(ctx context.Context) error {
if _, err := page.AddScriptToEvaluateOnNewDocument(stealthScript).Do(ctx); err != nil {
return err
}
return nil
}),
}
}
return chromedp.Tasks{}
}