Skip to content

Commit 7b72363

Browse files
committed
Stateless-compatible temporary permissions.
1 parent 7fd88e6 commit 7b72363

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

src/bg/Settings.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ var Settings = {
178178
}
179179

180180
if (typeof unrestrictedTab === "boolean") {
181-
ns.unrestrictedTabs[unrestrictedTab ? "add" : "delete"](tabId);
181+
ns.toggleTabRestrictions(tabId, !unrestrictedTab);
182182
}
183183
if (reloadAffected && tabId !== -1) {
184184
try {

src/bg/main.js

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,28 @@
6464
}
6565
}
6666

67+
const session = new SessionCache(
68+
"NoScriptSession",
69+
{
70+
afterLoad(data) {
71+
if (data) {
72+
ns.policy = new Policy(data.policy);
73+
ns.unrestrictedTabs = new Set(data.unrestrictedTabs);
74+
}
75+
},
76+
beforeSave() { // beforeSave
77+
return {
78+
policy: ns.policy.dry(true),
79+
unrestrictedTabs: [...ns.unrestrictedTabs],
80+
};
81+
},
82+
}
83+
);
84+
6785
async function init() {
6886
await Defaults.init();
69-
70-
if (!ns.policy) { // it could have been already retrieved by LifeCycle
87+
await session.load();
88+
if (!ns.policy) { // ns.policy could have been already set by LifeCycle or SessionCache
7189
const policyData = (await Storage.get("sync", "policy")).policy;
7290
if (policyData && policyData.DEFAULT) {
7391
ns.policy = new Policy(policyData);
@@ -110,8 +128,7 @@
110128
active: true
111129
}));
112130
if (tab) {
113-
const toggle = ns.unrestrictedTabs.has(tab.id) ? "delete" : "add";
114-
ns.unrestrictedTabs[toggle](tab.id);
131+
ns.toggleTabRestrictions(tab.id);
115132
browser.tabs.reload(tab.id);
116133
}
117134
},
@@ -253,6 +270,10 @@
253270
sync: null,
254271
initializing: null,
255272
unrestrictedTabs: new Set(),
273+
toggleTabRestrictions(tabId, restrict = ns.unrestrictedTabs.has(tabId)) {
274+
ns.unrestrictedTabs[restrict ? "delete": "add"](tabId);
275+
session.save();
276+
},
256277
isEnforced(tabId = -1) {
257278
return this.policy.enforced && (tabId === -1 || !this.unrestrictedTabs.has(tabId));
258279
},
@@ -345,10 +366,13 @@
345366

346367
async savePolicy() {
347368
if (this.policy) {
348-
await Storage.set("sync", {
349-
policy: this.policy.dry()
350-
});
351-
await browser.webRequest.handlerBehaviorChanged()
369+
await Promise.all([
370+
Storage.set("sync", {
371+
policy: this.policy.dry()
372+
}),
373+
session.save(),
374+
browser.webRequest.handlerBehaviorChanged()
375+
]);
352376
}
353377
return this.policy;
354378
},

0 commit comments

Comments
 (0)