2021-08-27 20:02:52 +00:00
|
|
|
/**
|
|
|
|
* Unchecker - Simple extension letting you uncheck all checkboxes on a page
|
2023-10-16 17:55:14 +00:00
|
|
|
* Copyright (c) Ad5001 2021-2023
|
2021-08-27 20:02:52 +00:00
|
|
|
*
|
|
|
|
* 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/.
|
|
|
|
**/
|
|
|
|
|
2023-10-16 17:55:14 +00:00
|
|
|
const uncheckAll = () => {
|
2023-11-08 16:11:59 +00:00
|
|
|
document.body.querySelectorAll('input[type="checkbox"]').forEach(function(el){
|
2021-08-27 20:02:52 +00:00
|
|
|
el.checked = false
|
|
|
|
})
|
|
|
|
}
|
2023-10-16 17:55:14 +00:00
|
|
|
|
|
|
|
const checkAll = () => {
|
2023-11-08 16:11:59 +00:00
|
|
|
document.body.querySelectorAll('input[type="checkbox"]').forEach(function(el){
|
2021-08-27 20:02:52 +00:00
|
|
|
el.checked = true
|
|
|
|
})
|
|
|
|
}
|
2023-10-16 17:55:14 +00:00
|
|
|
const TITLE_UNCHECK = "Uncheck all checkboxes"
|
|
|
|
const TITLE_CHECK = "Check all checkboxes"
|
|
|
|
const APPLICABLE_PROTOCOLS = ["http:", "https:"]
|
|
|
|
const MENU_CONTEXTS = ["all"]
|
2021-08-27 20:02:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Toggle Script: based on the current title, insert or remove the Script.
|
2023-10-16 17:55:14 +00:00
|
|
|
* Update the page actions title and icon, aswell as the context menu item to reflect its state.
|
2021-08-27 20:02:52 +00:00
|
|
|
**/
|
|
|
|
function toggleScript(tab) {
|
|
|
|
|
|
|
|
function gotTitle(title) {
|
2023-11-08 16:11:59 +00:00
|
|
|
console.log((title === TITLE_UNCHECK ? "Unchecking all checkboxes..." : "Checking all checkboxes...") + " at tab " + tab.id)
|
2023-10-16 17:55:14 +00:00
|
|
|
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: {
|
2023-11-08 16:24:21 +00:00
|
|
|
tabId: tab.id,
|
|
|
|
allFrames: true
|
2023-10-16 17:55:14 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
if(browser.menus)
|
|
|
|
browser.menus.update("unchecker-main", {
|
|
|
|
title: TITLE_CHECK,
|
2021-08-27 20:02:52 +00:00
|
|
|
icons: {
|
|
|
|
"16": "icons/off.svg"
|
2023-10-16 17:55:14 +00:00
|
|
|
}
|
|
|
|
})
|
2021-08-27 20:02:52 +00:00
|
|
|
} else {
|
2023-10-16 17:55:14 +00:00
|
|
|
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,
|
2021-08-27 20:02:52 +00:00
|
|
|
icons: {
|
|
|
|
"16": "icons/on.svg"
|
2023-10-16 17:55:14 +00:00
|
|
|
}
|
|
|
|
})
|
2021-08-27 20:02:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-16 17:55:14 +00:00
|
|
|
browser.action.getTitle({tabId: tab.id}).then(gotTitle)
|
2021-08-27 20:02:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true only if the URL's protocol is in APPLICABLE_PROTOCOLS.
|
|
|
|
**/
|
|
|
|
function protocolIsApplicable(url) {
|
2023-10-16 17:55:14 +00:00
|
|
|
let anchor = document.createElement('a')
|
|
|
|
anchor.href = url
|
|
|
|
return APPLICABLE_PROTOCOLS.includes(anchor.protocol)
|
2021-08-27 20:02:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-10-16 17:55:14 +00:00
|
|
|
* Initialize the page action set icon and title, then show.
|
2021-08-27 20:02:52 +00:00
|
|
|
* Only operates on tabs whose URL's protocol is applicable.
|
|
|
|
**/
|
|
|
|
function initializebrowserAction(tab, createMenu) {
|
2023-10-16 17:55:14 +00:00
|
|
|
// console.log("Initializing browser action for tab", tab, createMenu)
|
2021-08-27 20:02:52 +00:00
|
|
|
if(protocolIsApplicable(tab.url)) {
|
2023-10-16 17:55:14 +00:00
|
|
|
browser.action.setIcon({tabId: tab.id, path: "icons/on.svg"})
|
|
|
|
browser.action.setTitle({tabId: tab.id, title: TITLE_UNCHECK})
|
2021-08-27 20:02:52 +00:00
|
|
|
}
|
2023-10-16 17:55:14 +00:00
|
|
|
if(createMenu && browser.menus) {
|
2021-08-27 20:02:52 +00:00
|
|
|
browser.menus.remove("unchecker-main").then(function() {
|
|
|
|
browser.menus.create({
|
|
|
|
id: "unchecker-main",
|
2023-10-16 17:55:14 +00:00
|
|
|
title: TITLE_UNCHECK,
|
2021-08-27 20:02:52 +00:00
|
|
|
icons: {
|
|
|
|
"16": "icons/on.svg"
|
|
|
|
},
|
2023-10-16 17:55:14 +00:00
|
|
|
contexts: MENU_CONTEXTS
|
|
|
|
})
|
|
|
|
})
|
2021-08-27 20:02:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-16 17:55:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-08-27 20:02:52 +00:00
|
|
|
/**
|
|
|
|
* When first loaded, initialize the page action for all tabs.
|
|
|
|
**/
|
2023-10-16 17:55:14 +00:00
|
|
|
|
|
|
|
browser.tabs.query({}).then((tabs) => {
|
|
|
|
for(let tab of tabs) {
|
|
|
|
initializebrowserAction(tab, false)
|
2021-08-27 20:02:52 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
2023-10-16 17:55:14 +00:00
|
|
|
* Each time a tab is updated, reset the page browser.action.for that tab.
|
2021-08-27 20:02:52 +00:00
|
|
|
**/
|
|
|
|
browser.tabs.onUpdated.addListener((id, changeInfo, tab) => {
|
2023-10-16 17:55:14 +00:00
|
|
|
initializebrowserAction(tab, true)
|
2021-08-27 20:02:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
2023-10-16 17:55:14 +00:00
|
|
|
* Toggle Script when the page browser.action.is clicked.
|
2021-08-27 20:02:52 +00:00
|
|
|
**/
|
2023-10-16 17:55:14 +00:00
|
|
|
browser.action.onClicked.addListener(toggleScript)
|
2021-08-27 20:02:52 +00:00
|
|
|
|
|
|
|
|