fix: extension automatic setup

This commit is contained in:
Carlos Quintana
2024-07-15 09:05:01 +02:00
parent b4d2eda3e6
commit cead8c33d5
4 changed files with 23 additions and 43 deletions
-33
View File
@@ -1,33 +0,0 @@
import browser from "webextension-polyfill";
import SLStorage from "../popup/SLStorage";
async function setupContentScript() {
const apiUrl = SLStorage.get(SLStorage.SETTINGS.API_URL);
browser.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
if (
tab.url.startsWith("chrome") || // chrome internal pages
tab.url.startsWith("about") || // firefox internal pages
tab.url.startsWith("moz") || // firefox internal pages
tab.url.startsWith(apiUrl) // app domain
) {
return;
}
browser.scripting
.registerContentScripts([
{
id: "input-tools",
js: ["content_script/input_tools.js"],
css: ["content_script/input_tools.css"],
persistAcrossSessions: false,
matches: ["*://*/*"],
runAt: "document_idle",
},
])
.then(() => console.log("registration input tools complete"))
.catch((err) => console.warn("unexpected error", err));
});
}
setupContentScript();
+6 -7
View File
@@ -2,7 +2,6 @@ import browser from "webextension-polyfill";
import APIService, { API_ROUTE } from "../popup/APIService";
import SLStorage from "../popup/SLStorage";
import Onboarding from "./onboarding";
import "./content-script";
import { handleNewRandomAlias } from "./create-alias";
import {
@@ -66,13 +65,12 @@ async function handleExtensionSetup() {
/**
* Check if a message comes from an authorized source
* @param {string} url
* @returns boolean
* @returns Promise<boolean>
*/
const isMessageAllowed = (url) => {
const isMessageAllowed = async (url) => {
const requestUrl = new URL(url);
const apiUrl = new URL(
SLStorage.DEFAULT_SETTINGS[SLStorage.SETTINGS.API_URL]
);
const apiUrlValue = await SLStorage.get(SLStorage.SETTINGS.API_URL);
const apiUrl = new URL(apiUrlValue);
let allowedSources = [
new RegExp(apiUrl.hostname),
@@ -119,7 +117,8 @@ browser.runtime.onMessage.addListener(async function (request, sender) {
}
// Check messages allowed only from authorized sources
if (!isMessageAllowed(sender.url)) return;
const messageAllowed = await isMessageAllowed(sender.url);
if (!messageAllowed) return;
if (request.tag === "EXTENSION_SETUP") {
return await handleExtensionSetup();
+14
View File
@@ -53,5 +53,19 @@
},
"description": "Open the extension action menu"
}
},
"content_scripts": [
{
"js": ["content_script/input_tools.js"],
"css": ["content_script/input_tools.css"],
"matches": ["*://*/*"],
"exclude_matches": [
"chrome*",
"about*",
"moz*",
"app.simplelogin.io*"
],
"run_at": "document_idle"
}
]
}
+2 -2
View File
@@ -119,12 +119,12 @@ const config = {
if (process.env.FIREFOX) {
jsonContent.background = {
"scripts": ["background.js"]
}
};
} else { // CHROME
jsonContent.background = {
"service_worker": "background.js",
"type": "module"
}
};
}
if (process.env.LITE) {