port background to service worker

This commit is contained in:
Son NK
2024-03-25 23:28:00 +01:00
parent 3bae7686ac
commit 34077e16d9
4 changed files with 34 additions and 15 deletions
+13 -8
View File
@@ -14,14 +14,19 @@ async function setupContentScript() {
return;
}
browser.tabs.executeScript(tabId, {
file: "content_script/input_tools.js",
runAt: "document_idle",
});
browser.tabs.insertCSS(tabId, {
file: "content_script/input_tools.css",
});
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));
});
}
+3 -2
View File
@@ -11,8 +11,6 @@ import {
} from "./context-menu";
import Utils from "../popup/Utils";
global.isBackgroundJS = true;
/**
* Get app settings
*/
@@ -33,6 +31,9 @@ async function handleExtensionSetup() {
const url = apiUrl + API_ROUTE.GET_API_KEY_FROM_COOKIE.path;
const res = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
device: Utils.getDeviceName(),
}),
+7 -1
View File
@@ -14,7 +14,9 @@
"permissions": [
"activeTab",
"storage",
"contextMenus"
"contextMenus",
"scripting",
"tabs"
],
"host_permissions": [
"https://*.simplelogin.io/*",
@@ -32,6 +34,10 @@
"128": "icons/icon_128.png"
}
},
"background": {
"service_worker": "background.js",
"type": "module"
},
"browser_specific_settings": {
"gecko": {
"id": "addon@simplelogin"
+11 -4
View File
@@ -75,8 +75,9 @@ const callAPI = async function (
method: method,
headers: headers,
};
if (method === "POST") {
if (method === "POST" || method === "PUT") {
fetchParam.body = JSON.stringify(data);
headers["Content-Type"] = "application/json";
}
let res = await fetch(url, fetchParam);
@@ -84,6 +85,7 @@ const callAPI = async function (
const apiRes = await res.json();
// wrap apiRes in data to look like axios which was used before
return {
status: res.status,
data: apiRes,
};
} else {
@@ -91,7 +93,7 @@ const callAPI = async function (
console.error(res);
}
if (res.status === 401 && !global.isBackgroundJS) {
if (res.status === 401) {
await handle401Error();
return null;
}
@@ -99,7 +101,7 @@ const callAPI = async function (
if (errHandlerMethod === API_ON_ERR.TOAST) {
let apiRes = await res.json();
if (apiRes.error) {
Utils.showError(err.response.data.error);
Utils.showError(apiRes.error);
} else {
Utils.showError("Unknown error");
}
@@ -107,7 +109,12 @@ const callAPI = async function (
}
if (errHandlerMethod === API_ON_ERR.THROW) {
throw new Error("Request failed");
throw {
response: {
status: res.status,
data: await res.json()
}
};
}
}
};