|
64 | 64 | } |
65 | 65 | } |
66 | 66 |
|
| 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 | + |
67 | 85 | async function init() { |
68 | 86 | 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 |
71 | 89 | const policyData = (await Storage.get("sync", "policy")).policy; |
72 | 90 | if (policyData && policyData.DEFAULT) { |
73 | 91 | ns.policy = new Policy(policyData); |
|
110 | 128 | active: true |
111 | 129 | })); |
112 | 130 | if (tab) { |
113 | | - const toggle = ns.unrestrictedTabs.has(tab.id) ? "delete" : "add"; |
114 | | - ns.unrestrictedTabs[toggle](tab.id); |
| 131 | + ns.toggleTabRestrictions(tab.id); |
115 | 132 | browser.tabs.reload(tab.id); |
116 | 133 | } |
117 | 134 | }, |
|
253 | 270 | sync: null, |
254 | 271 | initializing: null, |
255 | 272 | unrestrictedTabs: new Set(), |
| 273 | + toggleTabRestrictions(tabId, restrict = ns.unrestrictedTabs.has(tabId)) { |
| 274 | + ns.unrestrictedTabs[restrict ? "delete": "add"](tabId); |
| 275 | + session.save(); |
| 276 | + }, |
256 | 277 | isEnforced(tabId = -1) { |
257 | 278 | return this.policy.enforced && (tabId === -1 || !this.unrestrictedTabs.has(tabId)); |
258 | 279 | }, |
|
345 | 366 |
|
346 | 367 | async savePolicy() { |
347 | 368 | 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 | + ]); |
352 | 376 | } |
353 | 377 | return this.policy; |
354 | 378 | }, |
|
0 commit comments