Compare commits

..

No commits in common. "683749748b62b2015701e3e5712b31facc9359b7" and "3086c5ef41a12703f523a23b531b8fc9bfbfed66" have entirely different histories.

5 changed files with 131 additions and 247 deletions

View file

@ -1,6 +1,6 @@
/**
* Unchecker - Simple extension letting you uncheck all checkboxes on a page
* Copyright (c) Ad5001 2021-2023
* Copyright (c) Ad5001 2021
*
* 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,67 +9,52 @@
// This file handles all clicking automaticly all similar buttons (e.g : same text & element name)
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)
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)
// 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 = ["all"]
` // Requires targetElementId to be defined beforehands
const CLICK_ALL_TITLE = "Click all similar buttons";
const CLICK_ALL_MENU_CONTEXTS = ["editable", "image", "link", "page"];
function clickAllSimilarButtons(info, tab) {
browser.scripting.executeScript({
target: {
tabId: tab.id,
frameIds: [ info.frameId ],
},
func: clickAllScript,
args: [ info.targetElementId ]
})
}
/**
* 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.tabs.executeScript(tab.id, {
frameId: info.frameId,
code: `var targetElementId = ${info.targetElementId};${CLICK_ALL_SCRIPT}`,
});
browser.menus.onClicked.addListener((info, tab) => {
if(info.menuItemId == "unchecker-clickall")
clickAllSimilarButtons(info, tab)
})
}
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) }
});

View file

@ -1,21 +1,18 @@
{
"description": "Simple extension letting you uncheck all checkboxes on a page.",
"manifest_version": 3,
"manifest_version": 2,
"name": "unchecker",
"version": "1.2.2",
"version": "1.1",
"author": "Ad5001",
"developer": {
"name": "Ad5001",
"url": "https://ad5001.eu"
"name": "Ad5001",
"url": "https://ad5001.eu"
},
"browser_specific_settings": {
"applications": {
"gecko": {
"id": "unchecker@ad5001.eu",
"strict_min_version": "113.0"
},
"gecko_android": {
"strict_min_version": "113.0"
"strict_min_version": "60.0"
}
},
"homepage_url": "https://apps.ad5001.eu/unchecker/",
@ -24,9 +21,9 @@
"scripts": ["uncheck.js", "clickall.js"]
},
"action": {
"browser_action": {
"default_icon": "icons/on.svg",
"default_title": "Uncheck all checkboxes"
"browser_style": true
},
"icons": {
"48": "icons/off.svg",
@ -35,8 +32,7 @@
"permissions": [
"activeTab",
"tabs",
"menus",
"scripting"
"menus"
]
}

View file

@ -1,86 +0,0 @@
<!--
* Unchecker - Simple extension letting you uncheck all checkboxes on a page
* 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
* obtain one at http://mozilla.org/MPL/2.0/.
-->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Unchecker tests in iframe</title>
<style>
legend {
background-color: #000;
color: #fff;
padding: 3px 6px;
}
.toggled {
border-color: blue;
}
input {
margin: .4rem;
}
</style>
<script src="test.js"></script>
</head>
<body>
<fieldset>
<legend>Example radio list</legend>
<input type="radio" id="choice-value1" name="choice-value">
<label for="choice-value">Example, irrelevant choice value 1</label><br/>
<input type="radio" id="choice-value2" name="choice-value">
<label for="choice-value">Example, irrelevant choice value 2</label><br/>
</fieldset>
<br>
<fieldset>
<legend>Third party partners example</legend>
<input type="checkbox" id="ga-choice" checked="true">
<label for="ga-choice">Google Analytics</label><br/>
<input type="checkbox" id="facebook-ads" checked="true">
<label for="facebook-ads">Facebook Partnering</label><br/>
<input type="checkbox" id="amazon-sales" checked="true">
<label for="amazon-sales">Amazon</label><br/>
<input type="checkbox" id="salesforce" checked="true">
<label for="salesforce">Salesforce</label><br/>
</fieldset>
<br>
<fieldset>
<legend>Third party partners second</legend>
<table>
<tr>
<td>Google Analytics</td>
<td><button role="toggle" class="toggled" name="ga-choice">On</button></td>
<td><button role="toggle" name="ga-choice">Off</button></td>
</tr>
<tr>
<td>Facebook Partnering</td>
<td><button role="toggle" class="toggled" name="facebook-ads">On</button></td>
<td><button role="toggle" name="facebook-ads">Off</button></td>
</tr>
<tr>
<td>Amazon</td>
<td><button role="toggle" class="toggled" name="amazon-sales">On</button></td>
<td><button role="toggle" name="amazon-sales">Off</button></td>
</tr>
<tr>
<td>Salesforce</td>
<td><button role="toggle" class="toggled" name="salesforce">On</button></td>
<td><button role="toggle" name="salesforce">Off</button></td>
</tr>
</table>
</fieldset>
</body>
</html>

View file

@ -1,6 +1,6 @@
<!--
* Unchecker - Simple extension letting you uncheck all checkboxes on a page
* Copyright (c) Ad5001 2021-2023
* Copyright (c) Ad5001 2021
*
* 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
@ -25,11 +25,6 @@
input {
margin: .4rem;
}
iframe {
width: 100%;
height: 500px;
}
</style>
<script src="test.js"></script>
</head>
@ -86,9 +81,5 @@
</tr>
</table>
</fieldset>
<fieldset>
<legend>IFramed tests</legend>
<iframe src="./frame.html"></iframe>
</body>
</html>

View file

@ -1,148 +1,146 @@
/**
* Unchecker - Simple extension letting you uncheck all checkboxes on a page
* Copyright (c) Ad5001 2021-2023
* Copyright (c) Ad5001 2021
*
* 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
* obtain one at http://mozilla.org/MPL/2.0/.
**/
const uncheckAll = () => {
const UNCHK_SCRIPT = `
function disable_checkboxes(element){
document.body.querySelectorAll('input[type="checkbox"]').forEach(function(el){
el.checked = false
})
}
const checkAll = () => {
disable_checkboxes(document.body)
document.body.querySelectorAll('iframe').forEach(function(el){disable_checkboxes(el.contentDocument || el.contentWindow.document)})
`
const CHK_SCRIPT = `
function check_checkboxes(element){
document.body.querySelectorAll('input[type="checkbox"]').forEach(function(el){
el.checked = true
})
}
const TITLE_UNCHECK = "Uncheck all checkboxes"
const TITLE_CHECK = "Check all checkboxes"
const APPLICABLE_PROTOCOLS = ["http:", "https:"]
const MENU_CONTEXTS = ["all"]
check_checkboxes(document.body)
document.body.querySelectorAll('iframe').forEach(function(el){check_checkboxes(el.contentDocument || el.contentWindow.document)})
`
const TITLE_APPLY = "Uncheck all checkboxes";
const TITLE_REMOVE = "Check all checkboxes";
const APPLICABLE_PROTOCOLS = ["http:", "https:"];
const MENU_CONTEXTS = ["editable", "image", "link", "page"];
/**
* Toggle Script: based on the current title, insert or remove the Script.
* Update the page actions title and icon, aswell as the context menu item to reflect its state.
* Update the page action's title and icon, aswell as the context menu item to reflect its state.
**/
function toggleScript(tab) {
function gotTitle(title) {
console.log((title === TITLE_UNCHECK ? "Unchecking all checkboxes..." : "Checking all checkboxes...") + " at tab " + tab.id)
if(title === TITLE_UNCHECK) {
browser.action.setIcon({tabId: tab.id, path: "icons/off.svg"})
browser.action.setTitle({tabId: tab.id, title: TITLE_CHECK})
browser.scripting.executeScript({
func: uncheckAll,
target: {
tabId: tab.id,
allFrames: true
}
})
if(browser.menus)
browser.menus.update("unchecker-main", {
title: TITLE_CHECK,
if (title === TITLE_APPLY) {
browser.browserAction.setIcon({tabId: tab.id, path: "icons/off.svg"});
browser.browserAction.setTitle({tabId: tab.id, title: TITLE_REMOVE});
browser.menus.remove("unchecker-main").then(function() {
browser.menus.create({
id: "unchecker-main",
title: TITLE_REMOVE,
icons: {
"16": "icons/off.svg"
}
})
},
contexts: MENU_CONTEXTS,
onclick(info,tab) { toggleScript(tab) }
});
}, function(){})
browser.tabs.executeScript({
code: UNCHK_SCRIPT
});
} else {
browser.action.setIcon({tabId: tab.id, path: "icons/on.svg"})
browser.action.setTitle({tabId: tab.id, title: TITLE_UNCHECK})
browser.scripting.executeScript({
func: checkAll,
target: {
tabId: tab.id,
allFrames: true,
}
})
if(browser.menus)
browser.menus.update("unchecker-main", {
title: TITLE_UNCHECK,
browser.browserAction.setIcon({tabId: tab.id, path: "icons/on.svg"});
browser.browserAction.setTitle({tabId: tab.id, title: TITLE_APPLY});
browser.menus.remove("unchecker-main").then(function() {
browser.menus.create({
id: "unchecker-main",
title: TITLE_APPLY,
icons: {
"16": "icons/on.svg"
}
})
},
contexts: MENU_CONTEXTS,
onclick(info,tab) { toggleScript(tab) }
});
}, function(){})
browser.tabs.executeScript({
code: CHK_SCRIPT
});
}
}
browser.action.getTitle({tabId: tab.id}).then(gotTitle)
var gettingTitle = browser.browserAction.getTitle({tabId: tab.id});
gettingTitle.then(gotTitle);
}
/**
* Returns true only if the URL's protocol is in APPLICABLE_PROTOCOLS.
**/
function protocolIsApplicable(url) {
let anchor = document.createElement('a')
anchor.href = url
return APPLICABLE_PROTOCOLS.includes(anchor.protocol)
let anchor = document.createElement('a');
anchor.href = url;
return APPLICABLE_PROTOCOLS.includes(anchor.protocol);
}
/**
* Initialize the page action set icon and title, then show.
* Initialize the page action: set icon and title, then show.
* Only operates on tabs whose URL's protocol is applicable.
**/
function initializebrowserAction(tab, createMenu) {
// console.log("Initializing browser action for tab", tab, createMenu)
if(protocolIsApplicable(tab.url)) {
browser.action.setIcon({tabId: tab.id, path: "icons/on.svg"})
browser.action.setTitle({tabId: tab.id, title: TITLE_UNCHECK})
browser.browserAction.setIcon({tabId: tab.id, path: "icons/on.svg"});
browser.browserAction.setTitle({tabId: tab.id, title: TITLE_APPLY});
browser.browserAction.show(tab.id);
}
if(createMenu && browser.menus) {
if(createMenu) {
browser.menus.remove("unchecker-main").then(function() {
browser.menus.create({
id: "unchecker-main",
title: TITLE_UNCHECK,
title: TITLE_APPLY,
icons: {
"16": "icons/on.svg"
},
contexts: MENU_CONTEXTS
})
})
contexts: MENU_CONTEXTS,
onclick(info,tab) { toggleScript(tab) }
});
}, function(){})
}
}
/**
* Create the default context menu for the main function.
*/
if(browser.menus) { // Not supported on Firefox for Android
browser.menus.create({
id: "unchecker-main",
title: TITLE_UNCHECK,
icons: {
"16": "icons/on.svg"
},
contexts: MENU_CONTEXTS
})
browser.menus.onClicked.addListener((info, tab) => {
if(info.menuItemId == "unchecker-main")
toggleScript(tab)
})
}
/**
* When first loaded, initialize the page action for all tabs.
**/
browser.tabs.query({}).then((tabs) => {
for(let tab of tabs) {
initializebrowserAction(tab, false)
browser.menus.create({
id: "unchecker-main",
title: TITLE_APPLY,
icons: {
"16": "icons/on.svg"
},
contexts: MENU_CONTEXTS,
onclick(info,tab) { toggleScript(tab) }
});
let gettingAllTabs = browser.tabs.query({});
gettingAllTabs.then((tabs) => {
for (let tab of tabs) {
initializebrowserAction(tab, false);
}
});
/**
* Each time a tab is updated, reset the page browser.action.for that tab.
* Each time a tab is updated, reset the page action for that tab.
**/
browser.tabs.onUpdated.addListener((id, changeInfo, tab) => {
initializebrowserAction(tab, true)
initializebrowserAction(tab, true);
});
/**
* Toggle Script when the page browser.action.is clicked.
* Toggle Script when the page action is clicked.
**/
browser.action.onClicked.addListener(toggleScript)
browser.browserAction.onClicked.addListener(toggleScript);