fix(scripts): wrong background script performance

This commit is contained in:
wanhose 2021-07-01 17:53:54 +02:00
parent c7ce7eb0a8
commit 4ebbcc4308

View File

@ -23,66 +23,58 @@ const isValid = (cache) =>
/** /**
* @function disableIcon * @function disableIcon
* @description Disables icon if there is a tab * @description Disables icon
* *
* @param {string} [tabId] * @param {string} [tabId]
*/ */
const disableIcon = (tabId) => { const disableIcon = (tabId) => {
if (tabId) {
chrome.browserAction.setIcon({ chrome.browserAction.setIcon({
path: "assets/icons/disabled.png", path: "assets/icons/disabled.png",
tabId, tabId,
}); });
}
}; };
/** /**
* @function disablePopup * @function disablePopup
* @description Disables popup if there is a tab * @description Disables popup
* *
* @param {string} [tabId] * @param {string} [tabId]
*/ */
const disablePopup = (tabId) => { const disablePopup = (tabId) => {
if (tabId) {
chrome.browserAction.setPopup({ chrome.browserAction.setPopup({
popup: "", popup: "",
tabId, tabId,
}); });
}
}; };
/** /**
* @function enableIcon * @function enableIcon
* @description Enables icon if there is a tab * @description Enables icon
* *
* @param {string} [tabId] * @param {string} [tabId]
*/ */
const enableIcon = (tabId) => { const enableIcon = (tabId) => {
if (tabId) {
chrome.browserAction.setIcon({ chrome.browserAction.setIcon({
path: "assets/icons/enabled.png", path: "assets/icons/enabled.png",
tabId, tabId,
}); });
}
}; };
/** /**
* @function enablePopup * @function enablePopup
* @description Enables popup if there is a tab * @description Enables popup
* *
* @param {string} [tabId] * @param {string} [tabId]
*/ */
const enablePopup = (tabId) => { const enablePopup = (tabId) => {
if (tabId) {
chrome.browserAction.setPopup({ chrome.browserAction.setPopup({
popup: "popup.html", popup: "popup.html",
tabId, tabId,
}); });
}
}; };
/** /**
@ -188,15 +180,23 @@ const updateCache = (hostname, state) => {
const updateState = (tabId, state) => { const updateState = (tabId, state) => {
switch (state) { switch (state) {
case "loading": case "loading":
chrome.tabs.insertCSS(tabId, { chrome.tabs.insertCSS(
tabId,
{
file: "styles/content.css", file: "styles/content.css",
runAt: "document_start", runAt: "document_start",
}); },
() => chrome.runtime.lastError
);
break; break;
case "ready": case "ready":
chrome.tabs.removeCSS(tabId, { chrome.tabs.removeCSS(
tabId,
{
file: "styles/content.css", file: "styles/content.css",
}); },
() => chrome.runtime.lastError
);
break; break;
default: default:
break; break;
@ -208,10 +208,8 @@ const updateState = (tabId, state) => {
*/ */
chrome.runtime.onMessage.addListener((request, sender, responseCallback) => { chrome.runtime.onMessage.addListener((request, sender, responseCallback) => {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
const hasPermission = !sender.frameId || sender.frameId === 0; const hasPermission = !sender.frameId || sender.frameId === 0;
const tab = tabs[0]; const tabId = sender.tab.id;
const tabId = tab ? tab.id : undefined;
switch (request.type) { switch (request.type) {
case "DISABLE_ICON": case "DISABLE_ICON":
@ -244,7 +242,6 @@ chrome.runtime.onMessage.addListener((request, sender, responseCallback) => {
default: default:
break; break;
} }
});
return true; return true;
}); });