Skip to content
This repository was archived by the owner on Aug 30, 2020. It is now read-only.

Commit 0e15e10

Browse files
author
Hongze Xia
committed
hide headless mode
1 parent c7da36d commit 0e15e10

File tree

6 files changed

+52
-6
lines changed

6 files changed

+52
-6
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.12
44

55
require (
66
cloud.google.com/go v0.30.0
7-
github.com/chromedp/cdproto v0.0.0-20191114225735-6626966fbae4 // indirect
7+
github.com/chromedp/cdproto v0.0.0-20191114225735-6626966fbae4
88
github.com/chromedp/chromedp v0.5.1
99
github.com/google/go-cmp v0.3.1 // indirect
1010
github.com/googleapis/gax-go v2.0.0+incompatible // indirect

gutils/tasks.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func processEntity(ent *models.Entity, pushClient *pushover.Client) {
2323
defer func() {
2424
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(CancelWaitTime))
2525
if err := ent.Save(ctx, EntityType, DsClient, true); err != nil {
26-
log.Println("ERROR: failed to save entity:", err, ". Entity: ", ent)
26+
log.Printf("ERROR: failed to save entity [%s] with %v", ent.Name, err)
2727
}
2828
cancel()
2929
}()
@@ -91,6 +91,7 @@ func Refresh(pushClient *pushover.Client, fetchLimit int) {
9191
log.Println("INFO: Refresh started")
9292
entities := FetchData(fetchLimit)
9393
for _, ent := range entities {
94+
log.Printf("INFO: processing [%s] XPATH (%s) at %s", ent.Name, ent.XPATH, ent.URL)
9495
processEntity(&ent, pushClient)
9596
}
9697
log.Println("INFO: Refresh ended")

main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ func main() {
4949
flag.StringVar(&userToken, "userToken", "", "pushover user token")
5050
flag.StringVar(&port, "port", "8080", "server port")
5151
flag.IntVar(&schdlFreq, "schdlFreq", 2, "schedule frequency in minutes")
52-
flag.IntVar(&fetchLimit, "fetchLimit", 2, "fetch limit from google datastore")
52+
flag.IntVar(&fetchLimit, "fetchLimit", 10, "fetch limit from google datastore")
53+
flag.Parse()
5354
if appToken == "" || userToken == "" {
5455
log.Fatalln("appToken and userToken must be given")
5556
}

models/requests.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import "cloud.google.com/go/datastore"
55
type (
66
// Options is the options for an entry
77
Options struct {
8+
Email string `json:"email"`
89
CheckFreq int16 `json:"checkFreq"` // in minutes
910
AlertType string `json:"alertType"`
1011
Threshold float32 `json:"threshold"`

tests/chromedp_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package main
2+
3+
import (
4+
"log"
5+
"testing"
6+
7+
"github.com/xiahongze/pricetracker/trackers"
8+
)
9+
10+
func TestChromedp(t *testing.T) {
11+
url := "https://shop.coles.com.au/a/a-nsw-metro-westmead/product/goldn-canola-canola-oil"
12+
xpath := `//span/strong[@class="product-price"]`
13+
price, ok := trackers.ChromeTracker(&url, &xpath)
14+
if !ok {
15+
t.Errorf("can't fetch price from %s with %s", url, xpath)
16+
return
17+
}
18+
log.Printf("price: %s", price)
19+
}

trackers/chrome_tracker.go

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,19 @@ import (
88
"strings"
99
"time"
1010

11+
"github.com/chromedp/cdproto/page"
1112
"github.com/chromedp/chromedp"
1213
)
1314

1415
var (
15-
chromeTimeout = time.Second * 60
16+
chromeTimeout = time.Second * 10
1617
chromeOpts = []chromedp.ExecAllocatorOption{
1718
chromedp.NoFirstRun,
1819
chromedp.NoDefaultBrowserCheck,
1920
chromedp.Headless,
2021
chromedp.DisableGPU,
2122
chromedp.Flag("enable-automation", false),
23+
chromedp.UserAgent(`Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36`),
2224
chromedp.Flag("disable-background-networking", true),
2325
chromedp.Flag("disable-background-timer-throttling", true),
2426
chromedp.Flag("disable-backgrounding-occluded-windows", true),
@@ -35,6 +37,23 @@ var (
3537
chromedp.Flag("disable-renderer-backgrounding", true),
3638
chromedp.Flag("disable-sync", true),
3739
}
40+
/**
41+
* Hard research to find out how to hide the automation mode
42+
* https://github.com/chromedp/chromedp/issues/396
43+
* https://intoli.com/blog/not-possible-to-block-chrome-headless/
44+
**/
45+
hiddenScript = `
46+
Object.defineProperty(navigator, 'webdriver', {
47+
get: () => false,
48+
});`
49+
hide = chromedp.ActionFunc(func(ctx context.Context) error {
50+
_, err := page.AddScriptToEvaluateOnNewDocument(hiddenScript).Do(ctx)
51+
if err != nil {
52+
return err
53+
}
54+
// log.Println("identifier: ", identifier.String())
55+
return nil
56+
})
3857
)
3958

4059
func init() {
@@ -59,10 +78,15 @@ func ChromeTracker(url, xpath *string) (string, bool) {
5978
defer cancel()
6079

6180
var res string
62-
err := chromedp.Run(ctx, chromedp.Navigate(*url), chromedp.Text(*xpath, &res, chromedp.NodeVisible, chromedp.BySearch))
81+
82+
err := chromedp.Run(ctx,
83+
hide,
84+
chromedp.Navigate(*url),
85+
chromedp.Text(*xpath, &res, chromedp.NodeVisible, chromedp.BySearch),
86+
)
6387

6488
if err != nil {
65-
log.Fatal(err)
89+
log.Printf("WARN: failed to fetch with chromedp with %v", err)
6690
}
6791

6892
return strings.TrimSpace(res), true

0 commit comments

Comments
 (0)