diff --git a/background.mjs b/background.mjs index 37e881d..42de1bb 100644 --- a/background.mjs +++ b/background.mjs @@ -8,6 +8,7 @@ import fetchPlus from "./modules/fetchPlus.mjs"; import addonSettings from "./modules/addonSettings.mjs"; import resizeImage from "./modules/resizeImage.mjs"; import remoteSettings from "./modules/remoteSettings.mjs"; +import ContentScriptRegister from "./modules/contentScriptRegister.mjs"; if (!window.browser) { window.browser = chrome; @@ -18,6 +19,7 @@ await settings.init(); const settings_remote = new remoteSettings(); await settings_remote.init(); +setInterval(async () => await settings_remote.sync(), 1 * 60 * 1000); // 30 minutes // change user-agent browser.webRequest.onBeforeSendHeaders.addListener( @@ -159,55 +161,42 @@ browser.runtime.onMessage.addListener(function (message, sender) { } }); -class ContentScriptRegister { - constructor(key, script_options) { - this.key = key; - this.script_options = script_options; - this.registed = null; - - let wait = new Promise((resolve) => { resolve() }); - if (settings_remote.get(this.key)) { - wait = this.#load(); - } - - settings_remote.addListener(async (settings_key, settings) => { - await wait; - if (this.key == settings_key) { - if (settings[key]) { - await this.#load(); - } else { - this.#unload(); - } - } - }); - } - async #load() { - if (!this.registed) { - this.registed = await browser.contentScripts.register(this.script_options); - } - } - #unload() { - if (this.registed) { - this.registed.unregister(); - this.registed = null; - } - } -} - -new ContentScriptRegister( - "injectTouchStartEventBlockingScript", +const touchStartEventBlockingScriptRegister = new ContentScriptRegister( { js: [{ file: "/injects/touch_start_event_blocking.js" }], matches: ["*://lens.google.com/*"], runAt: "document_start", }, ); - -new ContentScriptRegister( - "injectPreventDetectFirefoxBrowserScript", +const preventDetectFirefoxBrowser = new ContentScriptRegister( { js: [{ file: "/injects/prevent_detect_firefox_browser.js" }], matches: ["*://lens.google.com/*"], runAt: "document_start", }, ); + +async function onRemoteSettingsChange(key, settings) { + if (key == "injectTouchStartEventBlockingScript") { + if (settings[key]) { + console.log("Load \"touch_start_event_blocking.js\""); + await touchStartEventBlockingScriptRegister.load(); + } else { + console.log("Unload \"touch_start_event_blocking.js\""); + await touchStartEventBlockingScriptRegister.unload(); + } + } + if (key == "injectPreventDetectFirefoxBrowserScript") { + if (settings[key]) { + console.log("Load \"prevent_detect_firefox_browser.js\""); + await preventDetectFirefoxBrowser.load(); + } else { + console.log("Unload \"prevent_detect_firefox_browser.js\""); + await preventDetectFirefoxBrowser.unload(); + } + } +} +settings_remote.addListener(onRemoteSettingsChange); +for (const key of Object.keys(settings_remote.getAll())) { // init + onRemoteSettingsChange(key, settings_remote.getAll()); +} diff --git a/modules/contentScriptRegister.mjs b/modules/contentScriptRegister.mjs new file mode 100644 index 0000000..b406170 --- /dev/null +++ b/modules/contentScriptRegister.mjs @@ -0,0 +1,22 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +export default class ContentScriptRegister { + constructor(script_options) { + this.script_options = script_options; + this.registed = null; + } + async load() { + if (!this.registed) { + this.registed = await browser.contentScripts.register(this.script_options); + } + } + unload() { + if (this.registed) { + this.registed.unregister(); + this.registed = null; + } + } +} diff --git a/modules/remoteSettings.mjs b/modules/remoteSettings.mjs index b8e6160..d738be0 100644 --- a/modules/remoteSettings.mjs +++ b/modules/remoteSettings.mjs @@ -15,18 +15,20 @@ const DEFAULT_SETTINGS = { }; export default class { + #remote; + constructor() { this.initializing = false; this.initialized = false; this._listeners = []; - this.remote = []; + this.#remote = []; } async init() { if (this.initializing || this.initialized) return; this.initializing = true; - this.remote = Object.assign({}, DEFAULT_SETTINGS); + this.#remote = Object.assign({}, DEFAULT_SETTINGS); await this.sync(); @@ -55,31 +57,35 @@ export default class { return false; } - const new_remote = Object.assign({}, DEFAULT_SETTINGS, settings); + const old_remote = Object.assign({}, this.#remote); + this.#remote = Object.assign({}, DEFAULT_SETTINGS, settings); + if (this.initialized) { const key_sets = new Set(); - for (const key of Object.keys(this.remote)) { + for (const key of Object.keys(old_remote)) { key_sets.add(key); } - for (const key of Object.keys(new_remote)) { + for (const key of Object.keys(this.#remote)) { key_sets.add(key); } for (const key of key_sets) { - if (this.remote[key] !== new_remote[key]) { + if (old_remote[key] !== this.#remote[key]) { for (const listener of this._listeners) { - listener.callback(key, new_remote); + listener.callback(key, this.#remote); } } } } - this.remote = new_remote; return true; } get(key) { - return this.remote[key]; + return this.#remote[key]; + } + getAll() { + return Object.assign({}, this.#remote); } addListener(callback) { if (!this.initialized) throw new Error("must be initialized");