From 03e8090cd1f82f369ba8411c89d71dcae33bfcd7 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Mon, 24 Aug 2020 18:26:07 +0300 Subject: [PATCH] Add flag to set browser executable path --- puppet/Dockerfile | 2 +- puppet/example-config.json | 4 +++- puppet/package.json | 1 + puppet/src/main.js | 24 +++++++++++++++++------- puppet/src/puppet.js | 14 +++++++++----- puppet/yarn.lock | 5 +++++ 6 files changed, 36 insertions(+), 14 deletions(-) diff --git a/puppet/Dockerfile b/puppet/Dockerfile index 5c72ffc..23706b6 100644 --- a/puppet/Dockerfile +++ b/puppet/Dockerfile @@ -11,4 +11,4 @@ COPY package.json yarn.lock ./ RUN yarn --production && rm -rf node_modules/puppeteer/.local-chromium COPY . /opt/mautrix-amp/puppet -CMD ["yarn", "start", "/data/config.json"] +CMD ["yarn", "start", "--config", "/data/config.json", "--browser", "/usr/lib/chromium/chrome"] diff --git a/puppet/example-config.json b/puppet/example-config.json index 8abb094..ffeef59 100644 --- a/puppet/example-config.json +++ b/puppet/example-config.json @@ -3,5 +3,7 @@ "type": "unix", "path": "/var/run/mautrix-amp/puppet.sock" }, - "profile_dir": "./profiles" + "profile_dir": "./profiles", + "disable_debug": true, + "url": "https://messages.google.com/web/", } diff --git a/puppet/package.json b/puppet/package.json index 964c6c0..25fa99f 100644 --- a/puppet/package.json +++ b/puppet/package.json @@ -15,6 +15,7 @@ "start": "node ./src/main.js" }, "dependencies": { + "arg": "^4.1.3", "chrono-node": "^2.1.7", "puppeteer": "5.1.0" }, diff --git a/puppet/src/main.js b/puppet/src/main.js index ac460a4..7f2783c 100644 --- a/puppet/src/main.js +++ b/puppet/src/main.js @@ -16,16 +16,26 @@ import process from "process" import fs from "fs" +import arg from "arg" + import PuppetAPI from "./api.js" import MessagesPuppeteer from "./puppet.js" -let path = process.argv[process.argv.length - 1] -if (!path.endsWith(".json")) { - path = "config.json" -} -console.log("Reading config from", path) -const config = JSON.parse(fs.readFileSync(path).toString()) -MessagesPuppeteer.profileDir = config.profile_dir +const args = arg({ + "--config": String, + "--browser": String, + "-c": "--config", + "-b": "--browser", +}) + +const configPath = args["--config"] || "config.json" +MessagesPuppeteer.executablePath = args["--browser"] || MessagesPuppeteer.executablePath + +console.log("Reading config from", configPath) +const config = JSON.parse(fs.readFileSync(configPath).toString()) +MessagesPuppeteer.profileDir = config.profile_dir || MessagesPuppeteer.profileDir +MessagesPuppeteer.disableDebug = !!config.disable_debug +MessagesPuppeteer.url = config.url || MessagesPuppeteer.url const api = new PuppetAPI(config.listen) diff --git a/puppet/src/puppet.js b/puppet/src/puppet.js index 9af773f..94a05a8 100644 --- a/puppet/src/puppet.js +++ b/puppet/src/puppet.js @@ -24,7 +24,10 @@ import { sleep } from "./util.js" export default class MessagesPuppeteer { static profileDir = "./profiles" - url = "https://messages.google.com/web/" + static executablePath = undefined + static disableDebug = false + static viewport = { width: 1920, height: 1080 } + static url = "https://messages.google.com/web/" /** * @@ -59,9 +62,10 @@ export default class MessagesPuppeteer { async start(debug = false) { this.log("Launching browser") this.browser = await puppeteer.launch({ + executablePath: MessagesPuppeteer.executablePath, userDataDir: this.profilePath, - headless: !debug, - defaultViewport: { width: 1920, height: 1080 }, + headless: MessagesPuppeteer.disableDebug || !debug, + defaultViewport: MessagesPuppeteer.viewport, }) this.log("Opening new tab") const pages = await this.browser.pages() @@ -70,8 +74,8 @@ export default class MessagesPuppeteer { } else { this.page = await this.browser.newPage() } - this.log("Opening", this.url) - await this.page.goto(this.url) + this.log("Opening", MessagesPuppeteer.url) + await this.page.goto(MessagesPuppeteer.url) this.log("Injecting content script") await this.page.addScriptTag({ path: "./src/contentscript.js", type: "module" }) diff --git a/puppet/yarn.lock b/puppet/yarn.lock index 1f94340..eeffcd0 100644 --- a/puppet/yarn.lock +++ b/puppet/yarn.lock @@ -170,6 +170,11 @@ ansi-styles@^4.1.0: "@types/color-name" "^1.1.1" color-convert "^2.0.1" +arg@^4.1.3: + version "4.1.3" + resolved "https://registry.yarnpkg.com/arg/-/arg-4.1.3.tgz#269fc7ad5b8e42cb63c896d5666017261c144089" + integrity sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA== + argparse@^1.0.7: version "1.0.10" resolved "https://registry.yarnpkg.com/argparse/-/argparse-1.0.10.tgz#bcd6791ea5ae09725e17e5ad988134cd40b3d911"