diff options
author | praschke <stel@comfy.monster> | 2023-10-29 20:59:05 +0000 |
---|---|---|
committer | praschke <stel@comfy.monster> | 2023-10-29 20:59:05 +0000 |
commit | ba8eec942c60cc8b676408efd99e3fbbb9670c06 (patch) | |
tree | c748805962c9c8bfefd19e8a341110d0ae453a2e /ext | |
parent | 0adf9cef27de2641718116b91a0c7426aac6814e (diff) |
fix: session rules should be used instead of dynamic rules
session rules are less persistent than dynamic rules, and the
intention of RequestBuilder is to only have rules active for the
lifetime of specific requests.
Diffstat (limited to 'ext')
-rw-r--r-- | ext/js/background/request-builder.js | 50 |
1 files changed, 46 insertions, 4 deletions
diff --git a/ext/js/background/request-builder.js b/ext/js/background/request-builder.js index 03088fac..32c4a788 100644 --- a/ext/js/background/request-builder.js +++ b/ext/js/background/request-builder.js @@ -42,6 +42,7 @@ class RequestBuilder { async prepare() { try { await this._clearDynamicRules(); + await this._clearSessionRules(); } catch (e) { // NOP } @@ -260,6 +261,21 @@ class RequestBuilder { } } + async _clearSessionRules() { + if (!isObject(chrome.declarativeNetRequest)) { return; } + + const rules = await this._getSessionRules(); + + if (rules.length === 0) { return; } + + const removeRuleIds = []; + for (const {id} of rules) { + removeRuleIds.push(id); + } + + await this._updateSessionRules({removeRuleIds}); + } + async _clearDynamicRules() { if (!isObject(chrome.declarativeNetRequest)) { return; } @@ -311,17 +327,43 @@ class RequestBuilder { } }]; - await this._updateDynamicRules({addRules}); + await this._updateSessionRules({addRules}); try { return await fetch(url, init); } finally { - await this._tryUpdateDynamicRules({removeRuleIds: [id]}); + await this._tryUpdateSessionRules({removeRuleIds: [id]}); } } finally { this._ruleIds.delete(id); } } + _getSessionRules() { + return new Promise((resolve, reject) => { + chrome.declarativeNetRequest.getSessionRules((result) => { + const e = chrome.runtime.lastError; + if (e) { + reject(new Error(e.message)); + } else { + resolve(result); + } + }); + }); + } + + _updateSessionRules(options) { + return new Promise((resolve, reject) => { + chrome.declarativeNetRequest.updateSessionRules(options, () => { + const e = chrome.runtime.lastError; + if (e) { + reject(new Error(e.message)); + } else { + resolve(); + } + }); + }); + } + _getDynamicRules() { return new Promise((resolve, reject) => { chrome.declarativeNetRequest.getDynamicRules((result) => { @@ -348,9 +390,9 @@ class RequestBuilder { }); } - async _tryUpdateDynamicRules(options) { + async _tryUpdateSessionRules(options) { try { - await this._updateDynamicRules(options); + await this._updateSessionRules(options); return true; } catch (e) { return false; |