Tabs only reload if they are whitelisted to, fixes #23

This commit is contained in:
Jean Viscogliosi-Pate 2025-01-26 03:57:03 -08:00
parent aca0140471
commit 6103d56860
2 changed files with 35 additions and 6 deletions

View File

@ -116,7 +116,7 @@
<ul id="also"></ul> <ul id="also"></ul>
<div id="button-grid"> <div id="button-grid">
<button type="submit" id="save" title="Save options"> <button type="button" id="save" title="Save options">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M15.2 3a2 2 0 0 1 1.4.6l3.8 3.8a2 2 0 0 1 .6 1.4V19a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2z"/><path d="M17 21v-7a1 1 0 0 0-1-1H8a1 1 0 0 0-1 1v7"/><path d="M7 3v4a1 1 0 0 0 1 1h7"/></svg> <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M15.2 3a2 2 0 0 1 1.4.6l3.8 3.8a2 2 0 0 1 .6 1.4V19a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2z"/><path d="M17 21v-7a1 1 0 0 0-1-1H8a1 1 0 0 0-1 1v7"/><path d="M7 3v4a1 1 0 0 0 1 1h7"/></svg>
<p>Save</p> <p>Save</p>
</button> </button>

View File

@ -6,7 +6,7 @@ function init() {
restore(); restore();
document.querySelector("#preset").addEventListener("change", showOther); document.querySelector("#preset").addEventListener("change", showOther);
document.querySelector("#options").addEventListener("submit", save); document.querySelector("#save").addEventListener("click", save);
document.querySelector("#restore").addEventListener("click", restore); document.querySelector("#restore").addEventListener("click", restore);
} }
@ -17,6 +17,11 @@ function loadDomain() {
const hostname = new URL(tabs[0].url).hostname; const hostname = new URL(tabs[0].url).hostname;
document.querySelector("#domain").innerHTML = hostname; document.querySelector("#domain").innerHTML = hostname;
if (!hostname) {
disableToggle();
return;
}
let storage = browser.storage.local.get("domains"); let storage = browser.storage.local.get("domains");
storage.then((storage) => { storage.then((storage) => {
const hostname = document.querySelector("#domain").innerHTML; const hostname = document.querySelector("#domain").innerHTML;
@ -43,10 +48,9 @@ function toggle() {
} else { } else {
domains.push(hostname); domains.push(hostname);
} }
browser.storage.local.set({"domains": domains}); browser.storage.local.set({"domains": domains}).then(reloadTabs, logError);
}, logError); }, logError);
toggleButton(); toggleButton();
browser.tabs.reload();
} }
/* Toggles the displayed button state */ /* Toggles the displayed button state */
@ -64,6 +68,32 @@ function toggleButton() {
} }
} }
/* Disables the toggle if the site is invalid */
function disableToggle() {
let state = document.querySelector("#site-state");
state.querySelector("#domain").innerHTML = "Not available";
state.querySelector("p").innerHTML = "This tab does not allow MetamorPOV to modify its contents!";
state.querySelector("#toggle").style.display = "none";
}
/* Reloads tabs belonging to saved domains */
function reloadTabs() {
browser.tabs.query({ currentWindow: true }).then((tabs) => {
console.log("tabs now!");
browser.storage.local.get("domains").then((storage) => {
const domains = storage.domains;
console.log("storage now!");
tabs.forEach((tab) => {
const hostname = new URL(tab.url).hostname;
if (domains.includes(hostname)) {
browser.tabs.reload(tab.id);
console.log("reloading!");
}
});
}, logError);
}, logError);
}
/* Gets saved options and loads them */ /* Gets saved options and loads them */
function restore() { function restore() {
let options = browser.storage.local.get(); let options = browser.storage.local.get();
@ -131,8 +161,7 @@ function save() {
"other": saveOther(), "other": saveOther(),
"pov": document.querySelector("#pov").value, "pov": document.querySelector("#pov").value,
"also": saveAlso() "also": saveAlso()
}); }).then(reloadTabs, logError);
browser.tabs.reload();
} }
/* Helper for save, gets contents of other */ /* Helper for save, gets contents of other */