Stateless-compatible temporary permissions.

This commit is contained in:
hackademix 2024-10-23 00:23:23 +02:00
parent 7fd88e6228
commit 7b72363b72
No known key found for this signature in database
GPG Key ID: 231A83AFDA9C2434
2 changed files with 33 additions and 9 deletions

View File

@ -178,7 +178,7 @@ var Settings = {
} }
if (typeof unrestrictedTab === "boolean") { if (typeof unrestrictedTab === "boolean") {
ns.unrestrictedTabs[unrestrictedTab ? "add" : "delete"](tabId); ns.toggleTabRestrictions(tabId, !unrestrictedTab);
} }
if (reloadAffected && tabId !== -1) { if (reloadAffected && tabId !== -1) {
try { try {

View File

@ -64,10 +64,28 @@
} }
} }
const session = new SessionCache(
"NoScriptSession",
{
afterLoad(data) {
if (data) {
ns.policy = new Policy(data.policy);
ns.unrestrictedTabs = new Set(data.unrestrictedTabs);
}
},
beforeSave() { // beforeSave
return {
policy: ns.policy.dry(true),
unrestrictedTabs: [...ns.unrestrictedTabs],
};
},
}
);
async function init() { async function init() {
await Defaults.init(); await Defaults.init();
await session.load();
if (!ns.policy) { // it could have been already retrieved by LifeCycle if (!ns.policy) { // ns.policy could have been already set by LifeCycle or SessionCache
const policyData = (await Storage.get("sync", "policy")).policy; const policyData = (await Storage.get("sync", "policy")).policy;
if (policyData && policyData.DEFAULT) { if (policyData && policyData.DEFAULT) {
ns.policy = new Policy(policyData); ns.policy = new Policy(policyData);
@ -110,8 +128,7 @@
active: true active: true
})); }));
if (tab) { if (tab) {
const toggle = ns.unrestrictedTabs.has(tab.id) ? "delete" : "add"; ns.toggleTabRestrictions(tab.id);
ns.unrestrictedTabs[toggle](tab.id);
browser.tabs.reload(tab.id); browser.tabs.reload(tab.id);
} }
}, },
@ -253,6 +270,10 @@
sync: null, sync: null,
initializing: null, initializing: null,
unrestrictedTabs: new Set(), unrestrictedTabs: new Set(),
toggleTabRestrictions(tabId, restrict = ns.unrestrictedTabs.has(tabId)) {
ns.unrestrictedTabs[restrict ? "delete": "add"](tabId);
session.save();
},
isEnforced(tabId = -1) { isEnforced(tabId = -1) {
return this.policy.enforced && (tabId === -1 || !this.unrestrictedTabs.has(tabId)); return this.policy.enforced && (tabId === -1 || !this.unrestrictedTabs.has(tabId));
}, },
@ -345,10 +366,13 @@
async savePolicy() { async savePolicy() {
if (this.policy) { if (this.policy) {
await Storage.set("sync", { await Promise.all([
Storage.set("sync", {
policy: this.policy.dry() policy: this.policy.dry()
}); }),
await browser.webRequest.handlerBehaviorChanged() session.save(),
browser.webRequest.handlerBehaviorChanged()
]);
} }
return this.policy; return this.policy;
}, },