273 lines
6.5 KiB
Go
273 lines
6.5 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"log"
|
|
"math/rand"
|
|
"time"
|
|
|
|
"github.com/chromedp/cdproto/network"
|
|
"github.com/chromedp/cdproto/runtime"
|
|
"github.com/chromedp/chromedp"
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
func page1(taskCtx context.Context) {
|
|
page := "PAGE1"
|
|
c, cancelCtxWTO := context.WithTimeout(taskCtx, 5*time.Second)
|
|
defer cancelCtxWTO()
|
|
|
|
// ensure that the browser process is started
|
|
if err := chromedp.Run(c,
|
|
network.Enable(),
|
|
network.SetExtraHTTPHeaders(network.Headers(headers)),
|
|
chromedp.Navigate("https://m.facebook.com/"),
|
|
//chromedp.Nodes(`document`, &nodes, chromedp.ByJSPath),
|
|
chromedp.WaitVisible(emailField),
|
|
chromedp.WaitVisible(passwordField),
|
|
chromedp.SendKeys(emailField, viper.GetString("login.email")),
|
|
chromedp.SendKeys(passwordField, viper.GetString("login.passwd")),
|
|
chromedp.WaitReady(`//*[@id="signup-button"]`),
|
|
chromedp.Click(`//*[@name="login"]`),
|
|
chromedp.Sleep(2*time.Second),
|
|
); err != nil {
|
|
log.Println(page)
|
|
log.Println(err)
|
|
return
|
|
//panic(err)
|
|
}
|
|
|
|
makeScreenShot(taskCtx, page)
|
|
}
|
|
|
|
func page2(taskCtx context.Context) {
|
|
return // currently not need
|
|
page := "PAGE2"
|
|
|
|
c, cancelCtxWTO := context.WithTimeout(taskCtx, 5*time.Second)
|
|
defer cancelCtxWTO()
|
|
|
|
// ensure that the browser process is started
|
|
if err := chromedp.Run(c,
|
|
network.Enable(),
|
|
network.SetExtraHTTPHeaders(network.Headers(headers)),
|
|
chromedp.Navigate("https://m.facebook.com/"),
|
|
//chromedp.Nodes(`document`, &nodes, chromedp.ByJSPath),
|
|
chromedp.Sleep(2*time.Second),
|
|
); err != nil {
|
|
log.Println(page)
|
|
log.Println(err)
|
|
return
|
|
//panic(err)
|
|
}
|
|
|
|
makeScreenShot(taskCtx, page)
|
|
}
|
|
|
|
func page3(taskCtx context.Context) {
|
|
page := "PAGE3"
|
|
c, cancelCtxWTO := context.WithTimeout(taskCtx, 5*time.Second)
|
|
defer cancelCtxWTO()
|
|
|
|
if err := chromedp.Run(c,
|
|
chromedp.WaitVisible(`div._5xu4 > form`),
|
|
chromedp.Submit(`div._5xu4 > form`),
|
|
chromedp.Sleep(2*time.Second),
|
|
); err != nil {
|
|
log.Println(page)
|
|
log.Println(err)
|
|
return
|
|
//panic(err)
|
|
}
|
|
|
|
makeScreenShot(taskCtx, page)
|
|
}
|
|
|
|
func page4(taskCtx context.Context) {
|
|
page := "PAGE4"
|
|
c, cancelCtxWTO := context.WithTimeout(taskCtx, 5*time.Second)
|
|
defer cancelCtxWTO()
|
|
|
|
if err := chromedp.Run(c,
|
|
chromedp.WaitVisible(`#u_0_0`),
|
|
chromedp.SendKeys(`#u_0_0`, viper.GetString("login.passwd")),
|
|
chromedp.WaitVisible(`div._4g34 > form`),
|
|
chromedp.Submit(`div._4g34 > form`),
|
|
chromedp.Sleep(2*time.Second),
|
|
); err != nil {
|
|
log.Println(page)
|
|
log.Println(err)
|
|
return
|
|
//panic(err)
|
|
}
|
|
|
|
makeScreenShot(taskCtx, page)
|
|
|
|
}
|
|
|
|
func page5(taskCtx context.Context) {
|
|
page := "PAGE5"
|
|
c, cancelCtxWTO := context.WithTimeout(taskCtx, 5*time.Second)
|
|
defer cancelCtxWTO()
|
|
|
|
if err := chromedp.Run(c,
|
|
chromedp.WaitVisible(`div._4g34 > div._2pii > div > a`),
|
|
chromedp.Click(`div._4g34 > div._2pii > div > a`),
|
|
chromedp.Sleep(2*time.Second),
|
|
); err != nil {
|
|
log.Println(page)
|
|
log.Println(err)
|
|
return
|
|
//panic(err)
|
|
}
|
|
|
|
makeScreenShotOnly(taskCtx, page)
|
|
}
|
|
|
|
func page6(taskCtx context.Context) {
|
|
page := "PAGE6"
|
|
c, cancelCtxWTO := context.WithTimeout(taskCtx, 5*time.Second)
|
|
defer cancelCtxWTO()
|
|
|
|
// ensure that the browser process is started
|
|
if err := chromedp.Run(c,
|
|
network.Enable(),
|
|
network.SetExtraHTTPHeaders(network.Headers(headers)),
|
|
chromedp.Navigate("https://m.facebook.com/herowarsgame/posts/"),
|
|
chromedp.Sleep(2*time.Second),
|
|
); err != nil {
|
|
log.Println(page)
|
|
log.Println(err)
|
|
return
|
|
//panic(err)
|
|
}
|
|
|
|
makeScreenShot(taskCtx, page)
|
|
}
|
|
|
|
func page7(taskCtx context.Context) {
|
|
page := "PAGE7"
|
|
c, cancelCtxWTO := context.WithTimeout(taskCtx, 5*time.Second)
|
|
defer cancelCtxWTO()
|
|
|
|
// ensure that the browser process is started
|
|
if err := chromedp.Run(c,
|
|
chromedp.WaitNotPresent(`a._15ko._77li.touchable._77la`),
|
|
chromedp.WaitVisible(`a._15ko._77li.touchable`),
|
|
chromedp.Click(`a._15ko._77li.touchable`),
|
|
chromedp.Sleep(2*time.Second),
|
|
); err != nil {
|
|
log.Println(page)
|
|
log.Println(err)
|
|
return
|
|
//panic(err)
|
|
}
|
|
|
|
makeScreenShot(taskCtx, page)
|
|
}
|
|
|
|
func page8(taskCtx context.Context) (postID string, fbPost *FBPostData, err error) {
|
|
page := "PAGE8"
|
|
fbPost = nil
|
|
err = nil
|
|
|
|
c, cancelCtxWTO := context.WithTimeout(taskCtx, 10*time.Second)
|
|
defer cancelCtxWTO()
|
|
|
|
const function1 = `(function(d) {
|
|
var b = d.evaluate('//*/div[@id="pages_msite_body_contents"]/div/div[3]/div[1]/div/article', document, null, XPathResult.ANY_TYPE, null );
|
|
let bb = b.iterateNext();
|
|
let json = JSON.parse(bb.dataset.ft)
|
|
console.log(json)
|
|
return json.top_level_post_id
|
|
})(document);`
|
|
|
|
// ensure that the browser process is started
|
|
if err := chromedp.Run(c,
|
|
chromedp.WaitVisible(`a._15ko._77li.touchable`),
|
|
chromedp.ActionFunc(func(ctx context.Context) error {
|
|
tmpPostID, exp, err := runtime.Evaluate(function1).Do(ctx)
|
|
if err != nil {
|
|
log.Println(err)
|
|
return err
|
|
}
|
|
if exp != nil {
|
|
log.Println(exp)
|
|
return exp
|
|
}
|
|
|
|
err = json.Unmarshal(tmpPostID.Value, &postID)
|
|
if err != nil {
|
|
log.Println(err)
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}),
|
|
chromedp.Sleep(1*time.Second),
|
|
); err != nil {
|
|
log.Println(page)
|
|
log.Println(err)
|
|
return "", nil, err
|
|
//panic(err)
|
|
}
|
|
|
|
fbPost, err = makeScreenShotAndParsePost(taskCtx, page)
|
|
|
|
return postID, fbPost, err
|
|
}
|
|
|
|
func page9(taskCtx context.Context, postID string) {
|
|
page := "PAGE9"
|
|
c, cancelCtxWTO := context.WithTimeout(taskCtx, 15*time.Second)
|
|
defer cancelCtxWTO()
|
|
|
|
clickSel := `//*[@id="composer-` + postID + `"]/form/div[1]/div[3]/button`
|
|
//formSel := `//*[@id="composer-` + postID + `"]/form`
|
|
|
|
if postID != "" {
|
|
// ensure that the browser process is started
|
|
if err := chromedp.Run(c,
|
|
chromedp.WaitVisible(`//*[@id="feedback_inline_`+postID+`"]/div[2]/div[2]/a`),
|
|
chromedp.Click(`//*[@id="feedback_inline_`+postID+`"]/div[2]/div[2]/a`),
|
|
chromedp.Sleep(2*time.Second),
|
|
); err != nil {
|
|
log.Println(page)
|
|
log.Println(err)
|
|
return
|
|
//panic(err)
|
|
}
|
|
}
|
|
|
|
makeScreenShot(taskCtx, page)
|
|
|
|
page = page + "-1"
|
|
// ensure that the browser process is started
|
|
if err := chromedp.Run(c,
|
|
chromedp.WaitVisible(`//*/textarea[@id="composerInput"]`),
|
|
chromedp.SendKeys(`//*/textarea[@id="composerInput"]`, sendRandomText()),
|
|
chromedp.Sleep(time.Second),
|
|
chromedp.WaitVisible(clickSel),
|
|
chromedp.WaitEnabled(clickSel),
|
|
chromedp.Click(clickSel),
|
|
//chromedp.Sleep(2*time.Second),
|
|
chromedp.Sleep(2*time.Second),
|
|
); err != nil {
|
|
log.Println(page)
|
|
log.Println(err)
|
|
panic(err)
|
|
//return
|
|
|
|
}
|
|
|
|
makeScreenShot(taskCtx, page)
|
|
}
|
|
|
|
func sendRandomText() string {
|
|
s := rand.NewSource(time.Now().Unix())
|
|
r := rand.New(s) // initialize local pseudorandom generator
|
|
return randomText[r.Intn(len(randomText))]
|
|
|
|
}
|