|
| 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