From 5b473baf3b09cb6777811c89331890e5f96c0841 Mon Sep 17 00:00:00 2001 From: nachtjasmin Date: Sat, 8 Jul 2023 01:22:16 +0200 Subject: [PATCH] Use browser-specific manifests --- scripts/build.mjs | 4 +++- scripts/manifest.mjs | 56 ++++++++++++++++++++++++++++++++++++++++++++ scripts/shared.mjs | 20 ++++++++++++++++ scripts/watch.mjs | 5 +++- src/manifest.json | 41 -------------------------------- 5 files changed, 83 insertions(+), 43 deletions(-) create mode 100644 scripts/manifest.mjs delete mode 100644 src/manifest.json diff --git a/scripts/build.mjs b/scripts/build.mjs index 955a06b..effd94e 100644 --- a/scripts/build.mjs +++ b/scripts/build.mjs @@ -1,5 +1,7 @@ import * as esbuild from "esbuild"; -import { defaultBuildOptions } from "./shared.mjs"; +import { defaultBuildOptions, writeManifest } from "./shared.mjs"; + +writeManifest(); await esbuild.build({ ...defaultBuildOptions, diff --git a/scripts/manifest.mjs b/scripts/manifest.mjs new file mode 100644 index 0000000..ce2d797 --- /dev/null +++ b/scripts/manifest.mjs @@ -0,0 +1,56 @@ +/* eslint-disable */ +const manifest = { + manifest_version: 3, + name: "ProToots", + version: "1.1.2", + + icons: { + 48: "icons/icon small_size/icon small_size.png", + 96: "icons/icon small_size/icon small_size.png", + }, + + description: "puts pronouns next to usernames on mastodon", + homepage_url: "https://github.com/ItsVipra/ProToots", + permissions: ["storage", "activeTab", "https://en.pronouns.page/api/*"], + + action: { + default_icon: "icons/icon small_size/icon small_size.png", + default_title: "Enable ProToots on this page", + }, + content_scripts: [ + { + matches: ["*://*/*"], + js: ["content_scripts/protoots.js"], + css: ["styles/proplate.css"], + run_at: "document_start", + }, + ], + + options_ui: { + page: "options/options.html", + browser_style: false, + }, + + browser_specific_settings: { + gecko: { + id: "protoots@trans.rights", + }, + }, +}; + +const firefoxManifest = { + ...manifest, + background: { + scripts: ["background/worker.js"], + }, +}; + +const chromeManifest = { + ...manifest, + background: { + service_worker: "background/worker.js", + type: "module", + }, +}; + +export { firefoxManifest, chromeManifest }; diff --git a/scripts/shared.mjs b/scripts/shared.mjs index 761ce8d..d238562 100644 --- a/scripts/shared.mjs +++ b/scripts/shared.mjs @@ -1,6 +1,8 @@ import copyPluginPkg from "@sprout2000/esbuild-copy-plugin"; import path from "path"; const { copyPlugin } = copyPluginPkg; // js and your fucking mess of imports, sigh. +import { firefoxManifest, chromeManifest } from "./manifest.mjs"; +import fs from "fs"; /** * This array contains all files that we want to handle with esbuild. @@ -47,3 +49,21 @@ export const defaultBuildOptions = { }), ], }; + +/** Writes the manifest for the current browser to the dist folder. */ +export const writeManifest = () => { + const allowedBrowsers = ["firefox", "chrome"]; + const browser = process.env.TARGET ?? "firefox"; + + if (!allowedBrowsers.includes(browser)) { + throw new Error( + "The browser set via the TARGET environment is not valid. Only 'firefox' or 'chrome' are allowed.", + ); + } + + const { outdir } = defaultBuildOptions; + if (!fs.existsSync(outdir)) fs.mkdirSync(outdir); + + const manifest = JSON.stringify(browser === "firefox" ? firefoxManifest : chromeManifest); + fs.writeFileSync(path.join(outdir, "manifest.json"), manifest, { flag: "w" }); +}; diff --git a/scripts/watch.mjs b/scripts/watch.mjs index 75e61fd..da9cde8 100644 --- a/scripts/watch.mjs +++ b/scripts/watch.mjs @@ -1,4 +1,7 @@ import * as esbuild from "esbuild"; -import { defaultBuildOptions } from "./shared.mjs"; +import { defaultBuildOptions, writeManifest } from "./shared.mjs"; + +writeManifest(); + let ctx = await esbuild.context(defaultBuildOptions); await ctx.watch(); diff --git a/src/manifest.json b/src/manifest.json deleted file mode 100644 index 9e2233c..0000000 --- a/src/manifest.json +++ /dev/null @@ -1,41 +0,0 @@ -{ - "manifest_version": 3, - "name": "ProToots", - "version": "1.1.2", - - "icons": { - "48": "icons/icon small_size/icon small_size.png", - "96": "icons/icon small_size/icon small_size.png" - }, - - "description": "puts pronouns next to usernames on mastodon", - "homepage_url": "https://github.com/ItsVipra/ProToots", - "permissions": ["storage", "activeTab", "https://en.pronouns.page/api/*"], - - "action": { - "default_icon": "icons/icon small_size/icon small_size.png", - "default_title": "Enable ProToots on this page" - }, - "background": { - "scripts": ["background/worker.js"] - }, - "content_scripts": [ - { - "matches": ["*://*/*"], - "js": ["content_scripts/protoots.js"], - "css": ["styles/proplate.css"], - "run_at": "document_start" - } - ], - - "options_ui": { - "page": "options/options.html", - "browser_style": false - }, - - "browser_specific_settings": { - "gecko": { - "id": "protoots@trans.rights" - } - } -}