Skip to content

Commit 581be13

Browse files
committed
Initial commit
0 parents  commit 581be13

File tree

5 files changed

+151
-0
lines changed

5 files changed

+151
-0
lines changed

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
language: go
2+
sudo: false
3+
go:
4+
- tip

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 Emir Ribić
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.MD

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Golang Chrome Automation using ChromeDP - Example repo
2+
3+
[![Build Status](https://travis-ci.org/ribice/golang-chrome-automation.svg?branch=master)](https://travis-ci.org/ribice/golang-chrome-automation)
4+
[![Go Report Card](https://goreportcard.com/badge/github.com/ribice/golang-chrome-automation)](https://goreportcard.com/report/github.com/ribice/golang-chrome-automation)
5+
6+
This repo contains a small real world tool built using [chromedp](https://github.com/chromedp/chromedp).
7+
8+
It reads config data from config.json and launches Chrome.
9+
10+
Then it navigates to a Drupal website, logs-in and changes Bootstrap CDN settings.
11+
12+
Detailed explanation is available as a blog-post on my website - [LINK](https://ribice.ba/golang-chrome-automation/).
13+
14+
## License
15+
16+
golang-chrome-automation-example is licensed under the MIT license. Check the [LICENSE](LICENSE.md) file for details.
17+
18+
## Author
19+
20+
[Emir Ribic](https://ribice.ba)

config.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"url": "https://drupalwebsite.com",
3+
"username": "user",
4+
"password": "password",
5+
"bootstrap_css": "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css",
6+
"bootstrap_css_min": "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css",
7+
"bootstrap_js": "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.js",
8+
"bootstrap_js_min": "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"
9+
}

main.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"io/ioutil"
7+
"log"
8+
"runtime"
9+
"time"
10+
11+
"github.com/chromedp/chromedp/kb"
12+
13+
"github.com/chromedp/chromedp"
14+
)
15+
16+
func main() {
17+
cfg, err := readConfig()
18+
if err != nil {
19+
log.Fatalf("Could not read config file: %v", err)
20+
}
21+
22+
// create context
23+
ctxt, cancel := context.WithCancel(context.Background())
24+
defer cancel()
25+
26+
// create chrome instance
27+
c, err := chromedp.New(ctxt, chromedp.WithLog(log.Printf))
28+
checkErr(err)
29+
30+
// run task list
31+
checkErr(c.Run(ctxt, changeDrupalSettings(cfg)))
32+
33+
// shutdown chrome
34+
checkErr(c.Shutdown(ctxt))
35+
36+
// wait for chrome to finish
37+
checkErr(c.Wait())
38+
39+
log.Println("Successfully changed Drupal settings")
40+
}
41+
42+
func readConfig() (*config, error) {
43+
_, filePath, _, _ := runtime.Caller(0)
44+
pwd := filePath[:len(filePath)-7]
45+
txt, err := ioutil.ReadFile(pwd + "/config.json")
46+
if err != nil {
47+
return nil, err
48+
}
49+
var cfg = new(config)
50+
if err := json.Unmarshal(txt, cfg); err != nil {
51+
return nil, err
52+
}
53+
return cfg, nil
54+
}
55+
56+
func changeDrupalSettings(cfg *config) chromedp.Tasks {
57+
return chromedp.Tasks{
58+
chromedp.Navigate(cfg.URL + "user/login"),
59+
chromedp.WaitVisible(`#edit-name`, chromedp.ByID),
60+
chromedp.SendKeys(`#edit-name`, cfg.Username, chromedp.ByID),
61+
chromedp.SendKeys(`#edit-pass`, cfg.Password, chromedp.ByID),
62+
chromedp.Click("#edit-submit"),
63+
chromedp.Sleep(1 * time.Second),
64+
chromedp.Navigate(cfg.URL + "admin/appearance/settings/bootstrap#edit-advanced"),
65+
chromedp.WaitVisible(`#edit-cdn`, chromedp.ByID),
66+
chromedp.Click(`#edit-cdn`),
67+
chromedp.Click(`#edit-cdn-provider`),
68+
chromedp.SendKeys(`#edit-cdn-provider`, "c"+kb.Select, chromedp.ByID),
69+
chromedp.WaitVisible(`#edit-cdn-custom-css`, chromedp.ByID),
70+
chromedp.Clear(`#edit-cdn-custom-css`),
71+
chromedp.Clear(`#edit-cdn-custom-css-min`),
72+
chromedp.Clear(`#edit-cdn-custom-js`),
73+
chromedp.Clear(`#edit-cdn-custom-js-min`),
74+
chromedp.SendKeys(`#edit-cdn-custom-css`, cfg.BootstrapCSS, chromedp.ByID),
75+
chromedp.SendKeys(`#edit-cdn-custom-css-min`, cfg.BootstrapCSSMin, chromedp.ByID),
76+
chromedp.SendKeys(`#edit-cdn-custom-js`, cfg.BootstrapJS, chromedp.ByID),
77+
chromedp.SendKeys(`#edit-cdn-custom-js-min`, cfg.BootstrapJSMin, chromedp.ByID),
78+
chromedp.Click("#edit-submit"),
79+
chromedp.Sleep(1 * time.Second),
80+
}
81+
}
82+
83+
func checkErr(err error) {
84+
if err != nil {
85+
log.Fatal(err)
86+
}
87+
}
88+
89+
type config struct {
90+
URL string `json:"url"`
91+
Username string `json:"username"`
92+
Password string `json:"password"`
93+
BootstrapCSS string `json:"bootstrap_css"`
94+
BootstrapCSSMin string `json:"bootstrap_css_min"`
95+
BootstrapJS string `json:"bootstrap_js"`
96+
BootstrapJSMin string `json:"bootstrap_js_min"`
97+
}

0 commit comments

Comments
 (0)