From 12535fb6fee4235f6c86f4c09c708ca00645f3e4 Mon Sep 17 00:00:00 2001 From: EnixCoda Date: Fri, 16 Dec 2022 01:24:19 +0800 Subject: [PATCH] fix: patch webext-domain-permission-toggle --- scripts/fix-deps/index.js | 14 ++++++++-- .../webext-domain-permission-toggle.js | 26 +++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 scripts/fix-deps/webext-domain-permission-toggle.js diff --git a/scripts/fix-deps/index.js b/scripts/fix-deps/index.js index bbb9d0b..94097ad 100644 --- a/scripts/fix-deps/index.js +++ b/scripts/fix-deps/index.js @@ -24,15 +24,25 @@ function modify(source = '', pairs = []) { const nodeModulesPath = path.resolve(__dirname, '../../', `node_modules`) +const MODIFIED_MARK = `\n/* This file has been modified */\n` + exports.fixDep = async function fixDep(targetFilePath, pairs) { const filePath = path.resolve(nodeModulesPath, targetFilePath) const source = await fs.readFile(filePath, 'utf-8') - const modified = modify(source, pairs) + if (source.includes(MODIFIED_MARK)) { + console.log(`${filePath} has been fixed, skipping.`) + return + } + const modified = modify(source, pairs) + MODIFIED_MARK await fs.writeFile(filePath, modified, 'utf-8') } async function fixDeps() { - for (const fix of [require('./pjax-api').fix, require('./styled-components').fix]) { + for (const fix of [ + require('./pjax-api').fix, + require('./styled-components').fix, + require('./webext-domain-permission-toggle').fix, + ]) { await fix() } } diff --git a/scripts/fix-deps/webext-domain-permission-toggle.js b/scripts/fix-deps/webext-domain-permission-toggle.js new file mode 100644 index 0000000..1f5a2b8 --- /dev/null +++ b/scripts/fix-deps/webext-domain-permission-toggle.js @@ -0,0 +1,26 @@ +const { fixDep } = require('.') + +const targetFilePath = `webext-domain-permission-toggle/index.js` +const pairs = [ + [ + `const { name, optional_permissions: optionalPermissions } = chrome.runtime.getManifest();`, // prettier-ignore + `const { name, optional_host_permissions: optionalPermissions } = chrome.runtime.getManifest();`, // prettier-ignore + ], + [ + `const optionalHosts = optionalPermissions === null || optionalPermissions === void 0 ? void 0 : optionalPermissions.filter(permission => /|\\*/.test(permission));`, // prettier-ignore + `const optionalHosts = optionalPermissions === null || optionalPermissions === void 0 ? void 0 : optionalPermissions.filter(permission => '*://*/*' === permission);`, // prettier-ignore + ], + [ + `contexts: ['page_action', 'browser_action']`, // prettier-ignore + `contexts: ['action', 'page_action', 'browser_action']`, // prettier-ignore + ], +] + +exports.fix = async () => { + try { + await fixDep(targetFilePath, pairs) + } catch (err) { + console.error((err && err.message) || err) + process.exit(1) + } +}