diff --git a/pesticide_for_chrome/manifest.json b/pesticide_for_chrome/manifest.json index 7d0a2b9..781a6b7 100644 --- a/pesticide_for_chrome/manifest.json +++ b/pesticide_for_chrome/manifest.json @@ -1,21 +1,21 @@ { - "manifest_version": 2, + "manifest_version": 3, "name": "Pesticide for Chrome", "short_name": "Pesticide", "description": "This extension inserts the Pesticide CSS into the current page, outlining each element to better see placement on the page.", - "version": "1.1", + "version": "1.2", "permissions": [ - "activeTab" + "activeTab", + "scripting" ], "background": { - "scripts": ["pesticide-injector.js"], - "persistent": false + "service_worker": "pesticide-injector.js" }, - "browser_action": { + "action": { "default_title": "Toggle Pesticide", "default_icon": { "19": "images/toolbar-chrome.png", @@ -24,13 +24,18 @@ }, "icons": { - "128": "icon_128.png", - "16": "icon_16.png", - "48": "icon_48.png" - }, + "128": "icon_128.png", + "16": "icon_16.png", + "48": "icon_48.png" + }, "web_accessible_resources": [ - "pesticide.min.css", - "pesticide.js" + { + "resources": [ + "pesticide.min.css", + "pesticide.js" + ], + "matches": [""] + } ] } diff --git a/pesticide_for_chrome/pesticide-injector.js b/pesticide_for_chrome/pesticide-injector.js index 8711a08..cd9e12f 100644 --- a/pesticide_for_chrome/pesticide-injector.js +++ b/pesticide_for_chrome/pesticide-injector.js @@ -1,45 +1,35 @@ -(function(window, document){ - - 'use strict'; - - // inject the pesticide CSS and JS - function toggleAssets(tab) { - var injector = ''; - - // logic test if the injected assets exists - injector += 'if (document.getElementById("pesticideCSS") && document.getElementById("pesticideJS") ) {'; - - //if they exist, remove them - injector += 'document.getElementsByTagName("head")[0].removeChild(document.getElementById("pesticideCSS"));'; - injector += 'document.getElementsByTagName("head")[0].removeChild(document.getElementById("pesticideJS"));'; - injector += 'document.getElementsByTagName("body")[0].removeChild(document.getElementById("pesticide-for-chrome-result"));'; - - //if they don't exist, inject them - injector += '} else {'; - - injector += 'pesticideCSS = document.createElement("link");'; - injector += 'pesticideCSS.rel = "stylesheet";'; - injector += 'pesticideCSS.type = "text/css";'; - injector += 'pesticideCSS.href = chrome.extension.getURL("/pesticide.min.css");'; - injector += 'pesticideCSS.id = "pesticideCSS";'; - injector += 'document.getElementsByTagName("head")[0].appendChild(pesticideCSS);'; - injector += 'pesticideJS = document.createElement("script");'; - injector += 'pesticideJS.type = "text/javascript";'; - injector += 'pesticideJS.src = chrome.extension.getURL("/pesticide.js");'; - injector += 'pesticideJS.id = "pesticideJS";'; - injector += 'document.getElementsByTagName("head")[0].appendChild(pesticideJS);'; - injector += 'pesticideResult = document.createElement("div"),'; - injector += 'pesticideResult.id = "pesticide-for-chrome-result",'; - injector += 'document.getElementsByTagName("body")[0].appendChild(pesticideResult)'; - - //close logic test - injector += '}'; - - chrome.tabs.executeScript({code: injector}); - } - - chrome.browserAction.onClicked.addListener(function(tab){ - toggleAssets(tab); +chrome.action.onClicked.addListener((tab) => { + chrome.scripting.executeScript({ + target: { tabId: tab.id }, + function: toggleAssets }); - -}(window, document)); +}); + +function toggleAssets() { + // Inject the pesticide CSS and JS or remove them if already present + + if (document.getElementById("pesticideCSS") && document.getElementById("pesticideJS")) { + // If they exist, remove them + document.getElementsByTagName("head")[0].removeChild(document.getElementById("pesticideCSS")); + document.getElementsByTagName("head")[0].removeChild(document.getElementById("pesticideJS")); + document.getElementsByTagName("body")[0].removeChild(document.getElementById("pesticide-for-chrome-result")); + } else { + // If they don't exist, inject them + const pesticideCSS = document.createElement("link"); + pesticideCSS.rel = "stylesheet"; + pesticideCSS.type = "text/css"; + pesticideCSS.href = chrome.runtime.getURL("/pesticide.min.css"); + pesticideCSS.id = "pesticideCSS"; + document.getElementsByTagName("head")[0].appendChild(pesticideCSS); + + const pesticideJS = document.createElement("script"); + pesticideJS.type = "text/javascript"; + pesticideJS.src = chrome.runtime.getURL("/pesticide.js"); + pesticideJS.id = "pesticideJS"; + document.getElementsByTagName("head")[0].appendChild(pesticideJS); + + const pesticideResult = document.createElement("div"); + pesticideResult.id = "pesticide-for-chrome-result"; + document.getElementsByTagName("body")[0].appendChild(pesticideResult); + } +}