From 2424408902a4d073e4addd758201bd31963d86c7 Mon Sep 17 00:00:00 2001 From: Yuchen Pei Date: Tue, 27 Sep 2022 10:02:00 +1000 Subject: simplify process(handler) the only filter has a pre and a post function --- bg/ResponseProcessor.js | 43 ++++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 21 deletions(-) (limited to 'bg') diff --git a/bg/ResponseProcessor.js b/bg/ResponseProcessor.js index 0340a6f..922ff19 100644 --- a/bg/ResponseProcessor.js +++ b/bg/ResponseProcessor.js @@ -2,6 +2,7 @@ * GNU LibreJS - A browser add-on to block nonfree nontrivial JavaScript. * * Copyright (C) 2018 Giorgio Maone +* Copyright (C) 2022 Yuchen Pei * * This file is part of GNU LibreJS. * @@ -24,16 +25,16 @@ only the "interesting" HTML and script requests and leaving the other alone */ -let { ResponseMetaData } = require('./ResponseMetaData'); +const { ResponseMetaData } = require('./ResponseMetaData'); -let listeners = new WeakMap(); -let webRequestEvent = browser.webRequest.onHeadersReceived; +const listeners = new WeakMap(); +const webRequestEvent = browser.webRequest.onHeadersReceived; class ResponseProcessor { static install(handler, types = ['main_frame', 'sub_frame', 'script']) { if (listeners.has(handler)) return false; - let listener = + const listener = async request => await new ResponseTextFilter(request).process(handler); listeners.set(handler, listener); webRequestEvent.addListener( @@ -45,7 +46,7 @@ class ResponseProcessor { } static uninstall(handler) { - let listener = listeners.get(handler); + const listener = listeners.get(handler); if (listener) { webRequestEvent.removeListener(listener); } @@ -62,8 +63,8 @@ Object.assign(ResponseProcessor, { class ResponseTextFilter { constructor(request) { this.request = request; - let { type, statusCode } = request; - let md = this.metaData = new ResponseMetaData(request); + const { type, statusCode } = request; + const md = this.metaData = new ResponseMetaData(request); this.canProcess = // we want to process html documents and scripts only (statusCode < 300 || statusCode >= 400) && // skip redirections !md.disposition && // skip forced downloads @@ -72,29 +73,29 @@ class ResponseTextFilter { async process(handler) { if (!this.canProcess) return ResponseProcessor.ACCEPT; - let { metaData, request } = this; - let response = { request, metaData }; // we keep it around allowing callbacks to store state - if (typeof handler.pre === 'function') { - let res = await handler.pre(response); - if (res) return res; - if (handler.post) handler = handler.post; - if (typeof handler !== 'function') return ResponseProcessor.ACCEPT; + const { metaData, request } = this; + const response = { request, metaData }; // we keep it around allowing callbacks to store state + + if (typeof handler.pre !== 'function' || typeof handler.post !== 'function') { + throw new Error('handler should have a pre and post function.'); } + const res = await handler.pre(response); + if (res) return res; - let { requestId } = request; - let filter = browser.webRequest.filterResponseData(requestId); + const { requestId } = request; + const filter = browser.webRequest.filterResponseData(requestId); let buffer = []; filter.ondata = event => { buffer.push(event.data); }; - filter.onstop = async unused => { + filter.onstop = async _ => { // concatenate chunks - let size = buffer.reduce((sum, chunk) => sum + chunk.byteLength, 0) + const size = buffer.reduce((sum, chunk) => sum + chunk.byteLength, 0) let allBytes = new Uint8Array(size); let pos = 0; - for (let chunk of buffer) { + for (const chunk of buffer) { allBytes.set(new Uint8Array(chunk), pos); pos += chunk.byteLength; } @@ -113,13 +114,13 @@ class ResponseTextFilter { } let editedText = null; try { - editedText = await handler(response); + editedText = await handler.post(response); } catch (e) { console.error(e); } if (editedText !== null && editedText.indexOf('/* LibreJS: script accepted') !== 0) { // we changed the content, let's re-encode - let encoded = new TextEncoder().encode(editedText); + const encoded = new TextEncoder().encode(editedText); // pre-pending the UTF-8 BOM will force the charset per HTML 5 specs allBytes = new Uint8Array(encoded.byteLength + 3); allBytes.set(ResponseMetaData.UTF8BOM, 0); // UTF-8 BOM -- cgit v1.2.3