82 lines
1.7 KiB
Go
82 lines
1.7 KiB
Go
package intimate
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"runtime"
|
|
"time"
|
|
|
|
"github.com/tebeka/selenium"
|
|
"github.com/tebeka/selenium/chrome"
|
|
)
|
|
|
|
var zeroTime time.Time
|
|
|
|
func init() {
|
|
|
|
tm, err := time.Parse("15:04:05", "0:00:00")
|
|
if err != nil {
|
|
log.Println(err)
|
|
}
|
|
zeroTime = tm
|
|
|
|
}
|
|
|
|
// ParseDuration time to duration eg: 1:40:00 -> time.Duration
|
|
func ParseDuration(dt string) (time.Duration, error) {
|
|
|
|
var parse []byte = []byte("00:00:00")
|
|
|
|
j := len(parse) - 1
|
|
for i := len(dt) - 1; i >= 0; i-- {
|
|
c := dt[i]
|
|
if c != ':' {
|
|
parse[j] = dt[i]
|
|
} else {
|
|
for parse[j] != ':' {
|
|
j--
|
|
}
|
|
}
|
|
j--
|
|
}
|
|
|
|
tdt, err := time.Parse("15:04:05", string(parse))
|
|
if err != nil {
|
|
|
|
return time.Duration(0), err
|
|
}
|
|
return tdt.Sub(zeroTime), nil
|
|
}
|
|
|
|
func GetChromeDriver(port int) selenium.WebDriver {
|
|
caps := selenium.Capabilities{"browserName": "chrome"}
|
|
chromecaps := chrome.Capabilities{}
|
|
err := chromecaps.AddExtension("../../../crx/0.1.2_0.crx")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
chromecaps.Args = append(chromecaps.Args, "--proxy-pac-url=http://127.0.0.1:1081/pac")
|
|
chromecaps.Args = append(chromecaps.Args, "--disk-cache-dir=/tmp/chromedriver-cache")
|
|
chromecaps.ExcludeSwitches = append(chromecaps.ExcludeSwitches, "enable-automation")
|
|
caps.AddChrome(chromecaps)
|
|
_, err = selenium.NewChromeDriverService("/usr/bin/chromedriver", port)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
wd, err := selenium.NewRemote(caps, fmt.Sprintf("http://localhost:%d/wd/hub", port))
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
runtime.SetFinalizer(wd, func(obj interface{}) {
|
|
log.Println(obj)
|
|
if err := obj.(selenium.WebDriver).Close(); err != nil {
|
|
log.Println(err)
|
|
}
|
|
})
|
|
wd.ExecuteScript("windows.navigator.webdriver = undefined", nil)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return wd
|
|
}
|