-
-
Notifications
You must be signed in to change notification settings - Fork 92
/
manifest.js
132 lines (113 loc) · 4.53 KB
/
manifest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* NoScript Commons Library
* Reusable building blocks for cross-browser security/privacy WebExtensions.
* Copyright (C) 2020-2024 Giorgio Maone <https://maone.net>
*
* SPDX-License-Identifier: GPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
"use strict";
const fs = require('fs');
const path = require('path');
const args = process.argv.slice(2);
const MANIFEST_VER = /^m?v?[2-3]/i.test(args[0]) ? args.shift() : "mv3edge";
const MANIFEST_SRC = args[0] || "src/manifest.json";
const MANIFEST_DEST = args[1] || args[0] || "build/manifest.json";
console.log(`${MANIFEST_SRC} --[${MANIFEST_VER}]--> ${MANIFEST_DEST}`);
const srcContent = fs.readFileSync(MANIFEST_SRC, 'utf8');
const json = JSON.parse(srcContent);
const permissions = new Set(json.permissions);
let extVer = json.version;
const FIREFOX_UPDATE_URL = "https://secure.informaction.com/update/?v=" + extVer;
const EDGE_UPDATE_URL = "https://edge.microsoft.com/extensionwebstorebase/v1/crx";
const isFirefox = MANIFEST_VER.includes("firefox");
if (isFirefox && /rc|\.9\d{2}$/.test(extVer)) {
json.browser_specific_settings.gecko.update_url = FIREFOX_UPDATE_URL;
}
if (MANIFEST_VER.includes(3)) {
// MV3
json.manifest_version = 3;
if (!isFirefox) {
// convert ${ver}(a|b|rc)xx into ${ver--}.9xx
json.version = extVer.replace(/(\d+)(?:\.0)*[a-z]+(\d+)$/,
(all, maj, min) => `${parseInt(maj) - 1}.${900 + parseInt(min)}`);
delete json.browser_specific_settings;
delete json.content_security_policy;
const {scripts} = json.background;
delete json.background.scripts;
delete json.background.persistent;
const requiredPath = path.join(path.dirname(MANIFEST_DEST), "REQUIRED.js");
scripts && fs.writeFileSync(requiredPath,
`include.REQUIRED = ${JSON.stringify(scripts, null, 2)};`)
}
if (MANIFEST_VER.includes("edge")) {
json.update_url = EDGE_UPDATE_URL;
} else if (json.update_url === EDGE_UPDATE_URL) {
delete json.update_url;
}
for (const p of [
"<all_urls>",
"webRequestBlocking",
"webRequestBlocking",
"webRequestFilterResponse",
"webRequestFilterResponse.serviceWorkerScript",
]) {
permissions.delete(p);
}
const excludedScriptsRx = /\bcontent\/(?:embeddingDocument|dirindex)\.js$/;
for (const cs of json.content_scripts) {
cs.js = cs.js.filter(src => !excludedScriptsRx.test(src));
}
delete json.browser_action;
delete json.commands._execute_browser_action;
} else {
// MV2
json.manifest_version = 2;
delete json.background.service_worker;
delete json.web_accessible_resources;
delete json.host_permissions;
delete json.action;
for (const p of [
"debugger",
"declarativeNetRequest",
"declarativeNetRequestFeedback",
]) {
permissions.delete(p);
}
// Append MAIN world "*.main.js" scripts to their isolated counterparts
// (on Gecko we will patch windows through xray)
const isolatedWorldJS = json.content_scripts.find(
cs => cs.world != "MAIN" && cs.js?.some(src => src.endsWith("/Worlds.js"))).js;
json.content_scripts.find(cs => cs.world == "MAIN" && cs.js?.some(src => src.endsWith("/Worlds.main.js")))
.js.filter(src => src.endsWith(".main.js"))
.forEach(src => {
const isolatedSrc = src.replace(/.*(\/[\w+.]+)\.main(?=\.js$)/, "$1");
const idx = isolatedWorldJS.findIndex(src => src.endsWith(isolatedSrc));
if (idx > -1) {
isolatedWorldJS.splice(idx + 1, 0, src)
} else {
isolatedWorldJS.push(src);
}
});
// remove all the MAIN world content script
json.content_scripts = json.content_scripts.filter(cs => cs.world != "MAIN");
// match_origin_as_fallback is MV3 only
json.content_scripts.forEach(cs => delete cs.match_origin_as_fallback);
}
// remove developer-only stuff
permissions.delete("declarativeNetRequestFeedback");
json.permissions = [...permissions];
const destContent = JSON.stringify(json, null, 2);
fs.writeFileSync(MANIFEST_DEST, destContent);
console.log(`Written ${MANIFEST_DEST}`);