From 5305447f442b57443c4616ccd8d530c2c5e539b4 Mon Sep 17 00:00:00 2001 From: V Date: Mon, 17 Apr 2023 04:05:01 +0200 Subject: [PATCH] firefox: Fix csp (QuickCss, themes, some plugins) (#554) --- .github/workflows/build.yml | 2 +- .github/workflows/publish.yml | 5 ++--- browser/background.js | 32 +++++++++++++++++++++++++++ browser/manifestv2.json | 41 +++++++++++++++++++++++++++++++++++ package.json | 2 +- scripts/build/buildWeb.mjs | 3 ++- 6 files changed, 79 insertions(+), 6 deletions(-) create mode 100644 browser/background.js create mode 100644 browser/manifestv2.json diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 28a460e8..46645ad8 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -42,7 +42,7 @@ jobs: - name: Clean up obsolete files run: | - rm -rf dist/extension-unpacked Vencord.user.css vencordDesktopRenderer.css vencordDesktopRenderer.css.map + rm -rf dist/*-unpacked Vencord.user.css vencordDesktopRenderer.css vencordDesktopRenderer.css.map - name: Get some values needed for the release id: release_values diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 89cc2cb8..9f9df495 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -35,15 +35,15 @@ jobs: - name: Publish extension run: | - cd dist/extension-unpacked - # Do not fail so that even if chrome fails, firefox gets a shot. But also store exit code to fail workflow later EXIT_CODE=0 # Chrome + cd dist/chromium-unpacked pnpx chrome-webstore-upload-cli@2.1.0 upload --auto-publish || EXIT_CODE=$? # Firefox + cd ../chromium-unpacked npm i -g web-ext@7.4.0 web-ext-submit@7.4.0 web-ext-submit || EXIT_CODE=$? @@ -58,4 +58,3 @@ jobs: # Firefox WEB_EXT_API_KEY: ${{ secrets.WEBEXT_USER }} WEB_EXT_API_SECRET: ${{ secrets.WEBEXT_SECRET }} - diff --git a/browser/background.js b/browser/background.js new file mode 100644 index 00000000..7fc4a822 --- /dev/null +++ b/browser/background.js @@ -0,0 +1,32 @@ +/** + * @template T + * @param {T[]} arr + * @param {(v: T) => boolean} predicate + */ +function removeFirst(arr, predicate) { + const idx = arr.findIndex(predicate); + if (idx !== -1) arr.splice(idx, 1); +} + +chrome.webRequest.onHeadersReceived.addListener( + ({ responseHeaders, type, url }) => { + if (!responseHeaders) return; + + if (type === "main_frame") { + // In main frame requests, the CSP needs to be removed to enable fetching of custom css + // as desired by the user + removeFirst(responseHeaders, h => h.name.toLowerCase() === "content-security-policy"); + } else if (type === "stylesheet" && url.startsWith("https://raw.githubusercontent.com")) { + // Most users will load css from GitHub, but GitHub doesn't set the correct content type, + // so we fix it here + removeFirst(responseHeaders, h => h.name.toLowerCase() === "content-type"); + responseHeaders.push({ + name: "Content-Type", + value: "text/css" + }); + } + return { responseHeaders }; + }, + { urls: ["https://raw.githubusercontent.com/*", "*://*.discord.com/*"], types: ["main_frame", "stylesheet"] }, + ["blocking", "responseHeaders"] +); diff --git a/browser/manifestv2.json b/browser/manifestv2.json new file mode 100644 index 00000000..3cac9450 --- /dev/null +++ b/browser/manifestv2.json @@ -0,0 +1,41 @@ +{ + "manifest_version": 2, + "minimum_chrome_version": "91", + + "name": "Vencord Web", + "description": "The cutest Discord mod now in your browser", + "author": "Vendicated", + "homepage_url": "https://github.com/Vendicated/Vencord", + "icons": { + "128": "icon.png" + }, + + "permissions": [ + "webRequest", + "webRequestBlocking", + "*://*.discord.com/*", + "https://raw.githubusercontent.com/*" + ], + + "content_scripts": [ + { + "run_at": "document_start", + "matches": ["*://*.discord.com/*"], + "js": ["content.js"], + "all_frames": true + } + ], + + "background": { + "scripts": ["background.js"] + }, + + "web_accessible_resources": ["dist/Vencord.js", "dist/Vencord.css"], + + "browser_specific_settings": { + "gecko": { + "id": "vencord-firefox@vendicated.dev", + "strict_min_version": "91.0" + } + } +} diff --git a/package.json b/package.json index cf084880..b08a2433 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "vencord", "private": "true", - "version": "1.1.7", + "version": "1.1.8", "description": "The cutest Discord client mod", "homepage": "https://github.com/Vendicated/Vencord#readme", "bugs": { diff --git a/scripts/build/buildWeb.mjs b/scripts/build/buildWeb.mjs index 98d56b0d..cc27ea8c 100644 --- a/scripts/build/buildWeb.mjs +++ b/scripts/build/buildWeb.mjs @@ -142,6 +142,7 @@ const appendCssRuntime = readFile("dist/Vencord.user.css", "utf-8").then(content await Promise.all([ appendCssRuntime, buildPluginZip("extension.zip", ["modifyResponseHeaders.json", "content.js", "manifest.json", "icon.png"], true), - buildPluginZip("extension-unpacked", ["modifyResponseHeaders.json", "content.js", "manifest.json", "icon.png"], false), + buildPluginZip("chromium-unpacked", ["modifyResponseHeaders.json", "content.js", "manifest.json", "icon.png"], false), + buildPluginZip("firefox-unpacked", ["background.js", "content.js", "manifestv2.json", "icon.png"], false), ]);