Skip to content

Commit

Permalink
Improve codes
Browse files Browse the repository at this point in the history
  • Loading branch information
typeling1578 committed Nov 18, 2024
1 parent 3cd3491 commit 3afbf2f
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 49 deletions.
69 changes: 29 additions & 40 deletions background.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(
Expand Down Expand Up @@ -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());
}
22 changes: 22 additions & 0 deletions modules/contentScriptRegister.mjs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
24 changes: 15 additions & 9 deletions modules/remoteSettings.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit 3afbf2f

Please sign in to comment.