diff options
author | hackademix <giorgio@maone.net> | 2018-07-30 16:18:17 +0200 |
---|---|---|
committer | hackademix <giorgio@maone.net> | 2018-07-30 16:18:17 +0200 |
commit | 5e0ab1515ab1c399a6405f579c3b931563e4020d (patch) | |
tree | d163af155124b72fe3dc4c326d998bd6b2e2361a /bg | |
parent | 096cd90010b093e7c211f3318cbe41711ecfdef9 (diff) |
Implement early whitelisting / blacklisting logic.
Diffstat (limited to 'bg')
-rw-r--r-- | bg/ResponseProcessor.js | 24 | ||||
-rw-r--r-- | bg/Storage.js | 29 |
2 files changed, 46 insertions, 7 deletions
diff --git a/bg/ResponseProcessor.js b/bg/ResponseProcessor.js index 3f3151b..400cb42 100644 --- a/bg/ResponseProcessor.js +++ b/bg/ResponseProcessor.js @@ -52,6 +52,13 @@ class ResponseProcessor { } } +Object.assign(ResponseProcessor, { + // control flow values to be returned by handler.pre() callbacks + ACCEPT: {}, + REJECT: {cancel: true}, + CONTINUE: null +}); + class ResponseTextFilter { constructor(request) { this.request = request; @@ -64,9 +71,16 @@ class ResponseTextFilter { } process(handler) { - if (!this.canProcess) return {}; - let metaData = this.metaData; - let {requestId, responseHeaders} = this.request; + if (!this.canProcess) return ResponseProcessor.ACCEPT; + let {metaData, request} = this; + if (typeof handler.pre === "function") { + let res = handler.pre({request, metaData}); + if (res) return res; + if (handler.post) handler = handler.post; + if (typeof handler !== "function") ResponseProcessor.ACCEPT; + } + + let {requestId, responseHeaders} = request; let filter = browser.webRequest.filterResponseData(requestId); let buffer = []; @@ -83,7 +97,7 @@ class ResponseTextFilter { let editedText = null; try { let response = { - request: this.request, + request, metaData, text, }; @@ -103,7 +117,7 @@ class ResponseTextFilter { filter.disconnect(); } - return metaData.forceUTF8() ? {responseHeaders} : {}; + return metaData.forceUTF8() ? {responseHeaders} : ResponseProcessor.ACCEPT;; } } diff --git a/bg/Storage.js b/bg/Storage.js index 1386538..ecdc9e4 100644 --- a/bg/Storage.js +++ b/bg/Storage.js @@ -58,14 +58,35 @@ class ListStore { this.items = new Set(); } + static hashItem(hash) { + return hash.startsWith("(") ? hash : `(${hash})`; + } + static urlItem(url) { + let queryPos = url.indexOf("?"); + return queryPos === -1 ? url : url.substring(0, queryPos); + } + static siteItem(url) { + if (url.endsWith("/*")) return url; + try { + return `${new URL(url).origin}/*`; + } catch (e) { + return `${url}/*`; + } + } + async save() { return await this.storage.save(this.key, this.items); } async load() { - return await this.storage.load(this.key); + try { + this.items = await this.storage.load(this.key); + } catch (e) { + console.error(e); + } + return this.items; } - + async store(item) { let size = this.items.size; return (size !== this.items.add(item).size) && await this.save(); @@ -74,6 +95,10 @@ class ListStore { async remove(item) { return this.items.delete(item) && await this.save(); } + + contains(item) { + return this.items.has(item); + } } module.exports = { ListStore, Storage }; |