Add flag to set browser executable path

This commit is contained in:
Tulir Asokan 2020-08-24 18:26:07 +03:00
parent ccf7e25f46
commit 03e8090cd1
6 changed files with 36 additions and 14 deletions

View File

@ -11,4 +11,4 @@ COPY package.json yarn.lock ./
RUN yarn --production && rm -rf node_modules/puppeteer/.local-chromium RUN yarn --production && rm -rf node_modules/puppeteer/.local-chromium
COPY . /opt/mautrix-amp/puppet COPY . /opt/mautrix-amp/puppet
CMD ["yarn", "start", "/data/config.json"] CMD ["yarn", "start", "--config", "/data/config.json", "--browser", "/usr/lib/chromium/chrome"]

View File

@ -3,5 +3,7 @@
"type": "unix", "type": "unix",
"path": "/var/run/mautrix-amp/puppet.sock" "path": "/var/run/mautrix-amp/puppet.sock"
}, },
"profile_dir": "./profiles" "profile_dir": "./profiles",
"disable_debug": true,
"url": "https://messages.google.com/web/",
} }

View File

@ -15,6 +15,7 @@
"start": "node ./src/main.js" "start": "node ./src/main.js"
}, },
"dependencies": { "dependencies": {
"arg": "^4.1.3",
"chrono-node": "^2.1.7", "chrono-node": "^2.1.7",
"puppeteer": "5.1.0" "puppeteer": "5.1.0"
}, },

View File

@ -16,16 +16,26 @@
import process from "process" import process from "process"
import fs from "fs" import fs from "fs"
import arg from "arg"
import PuppetAPI from "./api.js" import PuppetAPI from "./api.js"
import MessagesPuppeteer from "./puppet.js" import MessagesPuppeteer from "./puppet.js"
let path = process.argv[process.argv.length - 1] const args = arg({
if (!path.endsWith(".json")) { "--config": String,
path = "config.json" "--browser": String,
} "-c": "--config",
console.log("Reading config from", path) "-b": "--browser",
const config = JSON.parse(fs.readFileSync(path).toString()) })
MessagesPuppeteer.profileDir = config.profile_dir
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) const api = new PuppetAPI(config.listen)

View File

@ -24,7 +24,10 @@ import { sleep } from "./util.js"
export default class MessagesPuppeteer { export default class MessagesPuppeteer {
static profileDir = "./profiles" 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) { async start(debug = false) {
this.log("Launching browser") this.log("Launching browser")
this.browser = await puppeteer.launch({ this.browser = await puppeteer.launch({
executablePath: MessagesPuppeteer.executablePath,
userDataDir: this.profilePath, userDataDir: this.profilePath,
headless: !debug, headless: MessagesPuppeteer.disableDebug || !debug,
defaultViewport: { width: 1920, height: 1080 }, defaultViewport: MessagesPuppeteer.viewport,
}) })
this.log("Opening new tab") this.log("Opening new tab")
const pages = await this.browser.pages() const pages = await this.browser.pages()
@ -70,8 +74,8 @@ export default class MessagesPuppeteer {
} else { } else {
this.page = await this.browser.newPage() this.page = await this.browser.newPage()
} }
this.log("Opening", this.url) this.log("Opening", MessagesPuppeteer.url)
await this.page.goto(this.url) await this.page.goto(MessagesPuppeteer.url)
this.log("Injecting content script") this.log("Injecting content script")
await this.page.addScriptTag({ path: "./src/contentscript.js", type: "module" }) await this.page.addScriptTag({ path: "./src/contentscript.js", type: "module" })

View File

@ -170,6 +170,11 @@ ansi-styles@^4.1.0:
"@types/color-name" "^1.1.1" "@types/color-name" "^1.1.1"
color-convert "^2.0.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: argparse@^1.0.7:
version "1.0.10" version "1.0.10"
resolved "https://registry.yarnpkg.com/argparse/-/argparse-1.0.10.tgz#bcd6791ea5ae09725e17e5ad988134cd40b3d911" resolved "https://registry.yarnpkg.com/argparse/-/argparse-1.0.10.tgz#bcd6791ea5ae09725e17e5ad988134cd40b3d911"