From 05fe47dd2050fb5b0e5f90598526adf70b4e0ed1 Mon Sep 17 00:00:00 2001 From: nea Date: Thu, 1 Dec 2022 06:32:52 +0100 Subject: Add Manifest V2 support Because there are so few differences between manifest v2 and v3 aside from header rewriting i just wrote two different manifests. Right now this only works on firefox with unpacked extensions. --- browser/background.js | 28 ++++++++++++++++++++++++++++ browser/manifest.json | 40 ---------------------------------------- browser/manifestv2.json | 35 +++++++++++++++++++++++++++++++++++ browser/manifestv3.json | 40 ++++++++++++++++++++++++++++++++++++++++ scripts/build/buildWeb.mjs | 40 +++++++++++++++++++++++----------------- 5 files changed, 126 insertions(+), 57 deletions(-) create mode 100644 browser/background.js delete mode 100644 browser/manifest.json create mode 100644 browser/manifestv2.json create mode 100644 browser/manifestv3.json diff --git a/browser/background.js b/browser/background.js new file mode 100644 index 0000000..9c5ed96 --- /dev/null +++ b/browser/background.js @@ -0,0 +1,28 @@ + +function setContentTypeOnStylesheets(details) { + if (details.type === 'stylesheet') { + details.responseHeaders.push({name: 'Content-Type', value: 'text/css'}) + } + return {responseHeaders: details.responseHeaders } +} + +var cspHeaders = [ + 'content-security-policy', + 'content-security-policy-report-only', +] + +function removeCSPHeaders(details) { + return {responseHeaders: details.responseHeaders.filter(header => + !cspHeaders.includes(header.name.toLowerCase()))} +} + + + + +browser.webRequest.onHeadersReceived.addListener( + setContentTypeOnStylesheets, {urls: ["https://raw.githubusercontent.com/*"]}, ['blocking'] +) + +browser.webRequest.onHeadersReceived.addListener( + removeCSPHeaders, {urls: ["https://raw.githubusercontent.com/*", "*://*.discord.com/*"]}, ['blocking', 'responseHeaders'] +) diff --git a/browser/manifest.json b/browser/manifest.json deleted file mode 100644 index ea79d12..0000000 --- a/browser/manifest.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "manifest_version": 3, - "name": "Vencord Web", - "description": "Yeee", - "version": "1.0.0", - "author": "Vendicated", - "homepage_url": "https://github.com/Vendicated/Vencord", - - "host_permissions": [ - "*://*.discord.com/*", - "https://raw.githubusercontent.com/*" - ], - - "permissions": ["declarativeNetRequest"], - - "content_scripts": [ - { - "run_at": "document_start", - "matches": ["*://*.discord.com/*"], - "js": ["content.js"] - } - ], - - "web_accessible_resources": [ - { - "resources": ["dist/Vencord.js"], - "matches": ["*://*.discord.com/*"] - } - ], - - "declarative_net_request": { - "rule_resources": [ - { - "id": "modifyResponseHeaders", - "enabled": true, - "path": "modifyResponseHeaders.json" - } - ] - } -} diff --git a/browser/manifestv2.json b/browser/manifestv2.json new file mode 100644 index 0000000..f18443c --- /dev/null +++ b/browser/manifestv2.json @@ -0,0 +1,35 @@ +{ + "manifest_version": 2, + "name": "Vencord Web", + "description": "Yeee", + "version": "1.0.0", + "author": "Vendicated", + "homepage_url": "https://github.com/Vendicated/Vencord", + "host_permissions": [ + "*://*.discord.com/*", + "https://raw.githubusercontent.com/*" + ], + "permissions": [ + "webRequest", + "webRequestBlocking" + ], + "content_scripts": [ + { + "run_at": "document_start", + "matches": [ + "*://*.discord.com/*" + ], + "js": [ + "content.js" + ] + } + ], + "web_accessible_resources": [ + "dist/Vencord.js" + ], + "background": { + "scripts": [ + "background.js" + ] + } +} diff --git a/browser/manifestv3.json b/browser/manifestv3.json new file mode 100644 index 0000000..ea79d12 --- /dev/null +++ b/browser/manifestv3.json @@ -0,0 +1,40 @@ +{ + "manifest_version": 3, + "name": "Vencord Web", + "description": "Yeee", + "version": "1.0.0", + "author": "Vendicated", + "homepage_url": "https://github.com/Vendicated/Vencord", + + "host_permissions": [ + "*://*.discord.com/*", + "https://raw.githubusercontent.com/*" + ], + + "permissions": ["declarativeNetRequest"], + + "content_scripts": [ + { + "run_at": "document_start", + "matches": ["*://*.discord.com/*"], + "js": ["content.js"] + } + ], + + "web_accessible_resources": [ + { + "resources": ["dist/Vencord.js"], + "matches": ["*://*.discord.com/*"] + } + ], + + "declarative_net_request": { + "rule_resources": [ + { + "id": "modifyResponseHeaders", + "enabled": true, + "path": "modifyResponseHeaders.json" + } + ] + } +} diff --git a/scripts/build/buildWeb.mjs b/scripts/build/buildWeb.mjs index a4ad87f..74f0857 100755 --- a/scripts/build/buildWeb.mjs +++ b/scripts/build/buildWeb.mjs @@ -72,20 +72,26 @@ await Promise.all( ] ); -zip({ - dist: { - "Vencord.js": readFileSync("dist/browser.js") - }, - ...Object.fromEntries(await Promise.all(["modifyResponseHeaders.json", "content.js", "manifest.json"].map(async f => [ - f, - await readFile(join("browser", f)) - ]))), -}, {}, (err, data) => { - if (err) { - console.error(err); - process.exitCode = 1; - } else { - writeFileSync("dist/extension.zip", data); - console.info("Extension written to dist/extension.zip"); - } -}); +async function buildPluginZip(target, files) { + zip({ + dist: { + "Vencord.js": readFileSync("dist/browser.js") + }, + ...Object.fromEntries(await Promise.all(files.map(async f => [ + (f.startsWith("manifest") ? "manifest.json" : f), + await readFile(join("browser", f)) + ]))), + }, {}, (err, data) => { + if (err) { + console.error(err); + process.exitCode = 1; + } else { + writeFileSync("dist/" + target, data); + console.info("Extension written to dist/" + target); + } + }); +} + +await buildPluginZip("extension-v3.zip", ["modifyResponseHeaders.json", "content.js", "manifestv3.json"]); +await buildPluginZip("extension-v2.xpi", ["background.js", "content.js", "manifestv2.json"]); + -- cgit