diff --git a/clickall.js b/clickall.js index 7ec1708..946182f 100644 --- a/clickall.js +++ b/clickall.js @@ -1,6 +1,6 @@ /** * Unchecker - Simple extension letting you uncheck all checkboxes on a page - * Copyright (c) Ad5001 2021 + * Copyright (c) Ad5001 2021-2023 * * 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 @@ -9,52 +9,67 @@ // This file handles all clicking automaticly all similar buttons (e.g : same text & element name) -const CLICK_ALL_SCRIPT = ` -var bound, selectedElements, clickedButton, buttonText, classes, query, sameElements; -// Get the potential selected button -bound = browser.menus.getTargetElement(targetElementId).getBoundingClientRect(); -selectedElements = document.elementsFromPoint(bound.x+bound.width/2, bound.y+bound.height/2) +const clickAllScript = (targetElementId) => { + let bound, selectedElements, clickedButton, buttonText, classes, query, sameElements + // Get the potential selected button + bound = browser.menus.getTargetElement(targetElementId).getBoundingClientRect() + selectedElements = document.elementsFromPoint(bound.x+bound.width/2, bound.y+bound.height/2) -// Leftover debug for positioning -//console.log(bound, browser.menus.getTargetElement(targetElementId), selectedElements) -//var div = document.createElement("div"); div.style.background = "black"; div.style.position = "absolute"; -//div.style.left = bound.x+bound.width/2-5 +"px"; div.style.top = bound.y+bound.height/2-5 + "px"; -//div.style.width = "10px"; div.style.height = "10px"; -//document.body.appendChild(div); + // Leftover debug for positioning + //console.log(bound, browser.menus.getTargetElement(targetElementId), selectedElements) + //var div = document.createElement("div"); div.style.background = "black"; div.style.position = "absolute"; + //div.style.left = bound.x+bound.width/2-5 +"px"; div.style.top = bound.y+bound.height/2-5 + "px"; + //div.style.width = "10px"; div.style.height = "10px"; + //document.body.appendChild(div); -selectedElements = selectedElements.filter(x => ["BUTTON","A","INPUT"].indexOf(x.tagName) > -1) -// If a button is selected -if(selectedElements.length > 0) { - clickedButton = selectedElements[0] - // Gather element that will be used in similar buttons (same text content). - buttonText = clickedButton.textContent.trim() - // Find the similar buttons - query = clickedButton.localName + (clickedButton.tagName == "INPUT" ? "[type=" + clickedButton.type + "]" : "") - sameElements = document.querySelectorAll(query) - sameElements = Array.from(sameElements).filter(btn => btn.textContent.trim() == buttonText) - // Click them automaticly. - sameElements.forEach(btn => { - btn.click() - }) + selectedElements = selectedElements.filter(x => ["BUTTON","A","INPUT"].indexOf(x.tagName) > -1) + // If a button is selected + if(selectedElements.length > 0) { + clickedButton = selectedElements[0] + // Gather element that will be used in similar buttons (same text content). + buttonText = clickedButton.textContent.trim() + // Find the similar buttons + query = clickedButton.localName + (clickedButton.tagName == "INPUT" ? "[type=" + clickedButton.type + "]" : "") + sameElements = document.querySelectorAll(query) + sameElements = Array.from(sameElements).filter(btn => btn.textContent.trim() == buttonText) + // Click them automaticly. + sameElements.forEach(btn => { + btn.click() + }) + } } -` // Requires targetElementId to be defined beforehands -const CLICK_ALL_TITLE = "Click all similar buttons"; -const CLICK_ALL_MENU_CONTEXTS = ["editable", "image", "link", "page"]; +// Requires targetElementId to be defined beforehands +const CLICK_ALL_TITLE = "Click all similar buttons" +const CLICK_ALL_MENU_CONTEXTS = ["all"] function clickAllSimilarButtons(info, tab) { - browser.tabs.executeScript(tab.id, { - frameId: info.frameId, - code: `var targetElementId = ${info.targetElementId};${CLICK_ALL_SCRIPT}`, - }); + browser.scripting.executeScript({ + target: { + tabId: tab.id, + frameIds: [ info.frameId ], + }, + func: clickAllScript, + args: [ info.targetElementId ] + }) } -browser.menus.create({ - id: "unchecker-clickall", - title: CLICK_ALL_TITLE, - icons: { - "16": "icons/click.svg", - "32": "icons/click.svg" - }, - contexts: CLICK_ALL_MENU_CONTEXTS, - onclick(info, tab) { clickAllSimilarButtons(info, tab) } -}); +/** + * Creating menu. + */ + +if(browser.menus) { // Not supported on Firefox for Android + browser.menus.create({ + id: "unchecker-clickall", + title: CLICK_ALL_TITLE, + icons: { + "16": "icons/click.svg", + "32": "icons/click.svg" + }, + contexts: CLICK_ALL_MENU_CONTEXTS, + }); + + browser.menus.onClicked.addListener((info, tab) => { + if(info.menuItemId == "unchecker-clickall") + clickAllSimilarButtons(info, tab) + }) +} diff --git a/manifest.json b/manifest.json index ebdcac7..d4e00a3 100644 --- a/manifest.json +++ b/manifest.json @@ -1,18 +1,21 @@ { "description": "Simple extension letting you uncheck all checkboxes on a page.", - "manifest_version": 2, + "manifest_version": 3, "name": "unchecker", - "version": "1.1", + "version": "1.2", "author": "Ad5001", "developer": { - "name": "Ad5001", - "url": "https://ad5001.eu" + "name": "Ad5001", + "url": "https://ad5001.eu" }, - "applications": { + "browser_specific_settings": { "gecko": { "id": "unchecker@ad5001.eu", - "strict_min_version": "60.0" + "strict_min_version": "113.0" + }, + "gecko_android": { + "strict_min_version": "113.0" } }, "homepage_url": "https://apps.ad5001.eu/unchecker/", @@ -20,10 +23,10 @@ "background": { "scripts": ["uncheck.js", "clickall.js"] }, - - "browser_action": { + + "action": { "default_icon": "icons/on.svg", - "browser_style": true + "default_title": "Uncheck all checkboxes" }, "icons": { "48": "icons/off.svg", @@ -32,7 +35,8 @@ "permissions": [ "activeTab", "tabs", - "menus" + "menus", + "scripting" ] } diff --git a/test/test.html b/test/test.html index 65d24a6..a5d1295 100644 --- a/test/test.html +++ b/test/test.html @@ -1,6 +1,6 @@