Skip to content
This repository was archived by the owner on Mar 29, 2026. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ jobs:
- run: yarn install --frozen-lockfile
- name: yarn build and test
run: |
yarn build
yarn build:all
yarn test
yarn lint:check
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

# production
/dist
/dist-*

# misc
.DS_Store
Expand Down
3 changes: 2 additions & 1 deletion manifest.json → manifest.template.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"permissions": ["alarms", "notifications", "storage"],
"optional_permissions": ["tabs"],
"background": {
"service_worker": "background.js",
"__chrome__service_worker": "background.js",
"__firefox__scripts": ["background.js"],
"type": "module"
},
"action": {
Expand Down
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@
"scripts": {
"start": "webpack serve",
"watch": "tsc --noEmit --watch",
"build": "rm -rf dist; webpack --mode production",
"build:chrome-mv3": "rm -rf dist-chrome-mv3; BROWSER=chrome webpack --mode production",
"build:firefox-mv3": "rm -rf dist-firefox-mv3; BROWSER=firefox webpack --mode production",
"build:all": "npm run build:chrome-mv3 && npm run build:firefox-mv3",
"zip:chrome-mv3": "cd dist-chrome-mv3 && zip -r ../dist-chrome-mv3.zip .",
"zip:firefox-mv3": "cd dist-firefox-mv3 && zip -r ../dist-firefox-mv3.zip .",
"zip:all": "npm run zip:chrome-mv3 && npm run zip:firefox-mv3",
"test": "jest",
"lint:check": "eslint \"src/**/*.ts\" \"src/**/*.tsx\"",
"lint:fix": "eslint --fix \"src/**/*.ts\" \"src/**/*.tsx\""
Expand Down
64 changes: 64 additions & 0 deletions scripts/transform-manifest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* Transforms the manifest.json file for the given browser.
* @param {string} browser - The browser to transform the manifest.json for.
* @returns {function} - A function that transforms the manifest.json file.
*/
function transform(browser) {
/**
* Transforms the manifest.json file for the given browser.
* @param {Buffer} buffer - The buffer to transform.
* @returns {string} - The transformed manifest.json file.
*/
return (buffer) => {
const manifest = JSON.parse(buffer.toString());

transformObject(manifest, browser);

return JSON.stringify(manifest, null, 2);
};
}

const browserKeyPattern = /^__(.+?)__(.+)$/;

/**
* Recursively traverses the object and transforms the browser-specific keys.
* @param {object} obj - The object to transform.
* @param {string} browser - The browser to transform the object for.
*/
function transformObject(obj, browser) {
if (typeof obj !== 'object' || obj === null || Array.isArray(obj)) {
return;
}

const keysToProcess = Object.keys(obj);
const keysToAdd = {};
const keysToDelete = [];

for (const key of keysToProcess) {
const match = key.match(browserKeyPattern);
if (match) {
const [, keyBrowser, actualKey] = match;

if (keyBrowser === browser) {
keysToAdd[actualKey] = obj[key];
keysToDelete.push(key);
} else {
keysToDelete.push(key);
}
} else {
transformObject(obj[key], browser);
}
}

for (const [newKey, value] of Object.entries(keysToAdd)) {
obj[newKey] = value;
}

for (const keyToDelete of keysToDelete) {
delete obj[keyToDelete];
}
}

module.exports = {
transform,
};
11 changes: 10 additions & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ const BundleAnalyzerPlugin =
require("webpack-bundle-analyzer").BundleAnalyzerPlugin;
const CopyPlugin = require("copy-webpack-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const { transform } = require("./scripts/transform-manifest");

const browser = process.env.BROWSER || "chrome";
const outputDir = `dist-${browser}-mv3`;

module.exports = {
module: {
Expand Down Expand Up @@ -41,6 +45,7 @@ module.exports = {
output: {
filename: "[name].js",
publicPath: "/",
path: path.resolve(__dirname, outputDir),
},
mode: "development",
devServer: {
Expand All @@ -64,7 +69,11 @@ module.exports = {
}),
new CopyPlugin({
patterns: [
{ from: "manifest.json", to: "." },
{
from: "manifest.template.json",
to: "manifest.json",
transform: transform(browser),
},
{ from: "images", to: "images" },
],
}),
Expand Down
Loading