Add runwithprofile to run in copy of real profile

This commit is contained in:
Oliver Blanthorn 2026-07-28 13:12:15 +02:00
parent dfe15d12a7
commit d9e727780b
No known key found for this signature in database
GPG key ID: 2BB8C36BB504BFF3
2 changed files with 46 additions and 0 deletions

View file

@ -70,6 +70,7 @@
"make-zip": "web-ext build --source-dir build --overwrite-dest",
"pretty": "bash scripts/pretty.sh",
"run": "web-ext run -s build/ -u 'paste.to'",
"runwithprofile": "node scripts/runwithprofile.js",
"test": "yarn run build && web-ext build --source-dir ./build --overwrite-dest && jest --silent",
"update-buildsystem": "rm -rf src/node_modules; yarn run clean",
"watch": "echo 'watch is broken, use build instead'; exit 0;",

45
scripts/runwithprofile.js Normal file
View file

@ -0,0 +1,45 @@
#!/usr/bin/env node
const { spawnSync } = require("child_process")
const fs = require("fs")
const os = require("os")
const path = require("path")
const firefoxDir =
process.platform === "win32"
? path.join(process.env.APPDATA || os.homedir(), "Mozilla", "Firefox")
: process.platform === "darwin"
? path.join(os.homedir(), "Library", "Application Support", "Firefox")
: path.join(os.homedir(), ".mozilla", "firefox")
function findDefaultProfile() {
const sections = []
let section
for (const line of fs.readFileSync(path.join(firefoxDir, "profiles.ini"), "utf8").split(/\r?\n/)) {
const header = line.trim().match(/^\[([^\]]+)\]$/)
if (header) {
section = { name: header[1] }
sections.push(section)
} else if (section) {
const separator = line.indexOf("=")
if (separator > 0) section[line.slice(0, separator)] = line.slice(separator + 1)
}
}
const profilePath =
sections.find(section => section.name.startsWith("Profile") && section.Name === "default-release")?.Path ??
sections.find(section => section.name.startsWith("Install") && section.Default)?.Default ??
sections.find(section => section.name.startsWith("Profile") && section.Default === "1")?.Path
if (!profilePath) throw new Error(`No default profile found in ${firefoxDir}`)
return path.isAbsolute(profilePath) ? profilePath : path.resolve(firefoxDir, profilePath)
}
try {
const profile = process.argv[2] || findDefaultProfile()
const command = process.platform === "win32" ? "web-ext.cmd" : "web-ext"
const result = spawnSync(command, ["run", "--source-dir", "build/", "--firefox", "deved", "--pre-install", "--firefox-profile", profile, "--no-reload", "--pref", "browser.startup.page=3", "--pref", "browser.sessionstore.resume_from_crash=true"], { stdio: "inherit" })
if (result.error) throw result.error
process.exitCode = result.status ?? 1
} catch (error) {
console.error(`runwithprofile: ${error.message}`)
process.exitCode = 1
}