From a6ebcc85be9d7b1c830deb7458f9276935cfa6a9 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Mon, 28 Jan 2019 16:12:26 -0500 Subject: [PATCH] Reuse both flavors of webRequest wrapper in webext package Related issue: - https://github.com/uBlockOrigin/uBlock-issues/issues/407 Both flavors will be stitched together into a single `vapi-qebrequest.js` file. The decision of which flavor to use will be made at runtime, according to the browser environment. --- platform/chromium/vapi-webrequest.js | 8 +- platform/firefox/vapi-webrequest.js | 7 +- platform/webext/vapi-webrequest.js | 174 --------------------------- tools/make-webext.sh | 8 +- 4 files changed, 20 insertions(+), 177 deletions(-) delete mode 100644 platform/webext/vapi-webrequest.js diff --git a/platform/chromium/vapi-webrequest.js b/platform/chromium/vapi-webrequest.js index d999b56b9..fc7963505 100644 --- a/platform/chromium/vapi-webrequest.js +++ b/platform/chromium/vapi-webrequest.js @@ -26,6 +26,9 @@ /******************************************************************************/ (function() { + // https://github.com/uBlockOrigin/uBlock-issues/issues/407 + if ( vAPI.webextFlavor.soup.has('chromium') === false ) { return; } + const extToTypeMap = new Map([ ['eot','font'],['otf','font'],['svg','font'],['ttf','font'],['woff','font'],['woff2','font'], ['mp3','media'],['mp4','media'],['webm','media'], @@ -152,7 +155,10 @@ // https://github.com/gorhill/uBlock/issues/2067 // Experimental: Block everything until uBO is fully ready. -vAPI.net.onBeforeReady = (function() { +vAPI.net.onBeforeReady = vAPI.net.onBeforeReady || (function() { + // https://github.com/uBlockOrigin/uBlock-issues/issues/407 + if ( vAPI.webextFlavor.soup.has('chromium') === false ) { return; } + let pendings; const handler = function(details) { diff --git a/platform/firefox/vapi-webrequest.js b/platform/firefox/vapi-webrequest.js index 951b8a2e5..78b1cfc03 100644 --- a/platform/firefox/vapi-webrequest.js +++ b/platform/firefox/vapi-webrequest.js @@ -26,6 +26,8 @@ /******************************************************************************/ (function() { + // https://github.com/uBlockOrigin/uBlock-issues/issues/407 + if ( vAPI.webextFlavor.soup.has('firefox') === false ) { return; } // https://github.com/gorhill/uBlock/issues/2950 // Firefox 56 does not normalize URLs to ASCII, uBO must do this itself. @@ -136,7 +138,10 @@ // - https://github.com/uBlockOrigin/uBlock-issues/issues/128 // - https://bugzilla.mozilla.org/show_bug.cgi?id=1503721 -vAPI.net.onBeforeReady = (function() { +vAPI.net.onBeforeReady = vAPI.net.onBeforeReady || (function() { + // https://github.com/uBlockOrigin/uBlock-issues/issues/407 + if ( vAPI.webextFlavor.soup.has('firefox') === false ) { return; } + let pendings; const handler = function(details) { diff --git a/platform/webext/vapi-webrequest.js b/platform/webext/vapi-webrequest.js deleted file mode 100644 index 92f902d02..000000000 --- a/platform/webext/vapi-webrequest.js +++ /dev/null @@ -1,174 +0,0 @@ -/******************************************************************************* - - uBlock Origin - a browser extension to block requests. - Copyright (C) 2018 Raymond Hill - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see {http://www.gnu.org/licenses/}. - - Home: https://github.com/gorhill/uBlock -*/ - -// For background page - -'use strict'; - -/******************************************************************************/ - -vAPI.net = { - onBeforeRequest: {}, - onBeforeMaybeSpuriousCSPReport: {}, - onHeadersReceived: {}, - nativeCSPReportFiltering: true, - webRequest: chrome.webRequest, - canFilterResponseBody: - typeof chrome.webRequest === 'object' && - typeof chrome.webRequest.filterResponseData === 'function' -}; - -/******************************************************************************/ - -vAPI.net.registerListeners = function() { - - let wrApi = chrome.webRequest; - - // legacy Chromium understands only these network request types. - let validTypes = new Set([ - 'image', - 'main_frame', - 'object', - 'other', - 'script', - 'stylesheet', - 'sub_frame', - 'xmlhttprequest', - ]); - // modern Chromium/WebExtensions: more types available. - if ( wrApi.ResourceType ) { - for ( let typeKey in wrApi.ResourceType ) { - if ( wrApi.ResourceType.hasOwnProperty(typeKey) ) { - validTypes.add(wrApi.ResourceType[typeKey]); - } - } - } - - let denormalizeTypes = function(aa) { - if ( aa.length === 0 ) { - return Array.from(validTypes); - } - let out = new Set(), - i = aa.length; - while ( i-- ) { - let type = aa[i]; - if ( validTypes.has(type) ) { - out.add(type); - } - if ( type === 'image' && validTypes.has('imageset') ) { - out.add('imageset'); - } - } - return Array.from(out); - }; - - let normalizeRequestDetails = function(details) { - if ( details.tabId === vAPI.noTabId ) { - // Chromium uses `initiator` property. - if ( - details.documentUrl === undefined && - typeof details.initiator === 'string' && - details.initiator !== 'null' - ) { - details.documentUrl = details.initiator; - } - } - - // https://github.com/gorhill/uBlock/issues/1493 - // Chromium 49+/WebExtensions support a new request type: `ping`, - // which is fired as a result of using `navigator.sendBeacon`. - if ( details.type === 'ping' ) { - details.type = 'beacon'; - return; - } - - if ( details.type === 'imageset' ) { - details.type = 'image'; - return; - } - }; - - let onBeforeRequestClient = this.onBeforeRequest.callback; - let onBeforeRequest = function(details) { - normalizeRequestDetails(details); - return onBeforeRequestClient(details); - }; - - if ( onBeforeRequest ) { - let urls = this.onBeforeRequest.urls || ['']; - let types = this.onBeforeRequest.types || undefined; - if ( - (validTypes.has('websocket')) && - (types === undefined || types.indexOf('websocket') !== -1) && - (urls.indexOf('') === -1) - ) { - if ( urls.indexOf('ws://*/*') === -1 ) { - urls.push('ws://*/*'); - } - if ( urls.indexOf('wss://*/*') === -1 ) { - urls.push('wss://*/*'); - } - } - wrApi.onBeforeRequest.addListener( - onBeforeRequest, - { urls: urls, types: types }, - this.onBeforeRequest.extra - ); - } - - // https://github.com/gorhill/uBlock/issues/3140 - if ( typeof this.onBeforeMaybeSpuriousCSPReport.callback === 'function' ) { - wrApi.onBeforeRequest.addListener( - this.onBeforeMaybeSpuriousCSPReport.callback, - { - urls: [ 'http://*/*', 'https://*/*' ], - types: [ 'csp_report' ] - }, - [ 'blocking', 'requestBody' ] - ); - } - - let onHeadersReceivedClient = this.onHeadersReceived.callback, - onHeadersReceivedClientTypes = this.onHeadersReceived.types.slice(0), - onHeadersReceivedTypes = denormalizeTypes(onHeadersReceivedClientTypes); - let onHeadersReceived = function(details) { - normalizeRequestDetails(details); - if ( - onHeadersReceivedClientTypes.length !== 0 && - onHeadersReceivedClientTypes.indexOf(details.type) === -1 - ) { - return; - } - return onHeadersReceivedClient(details); - }; - - if ( onHeadersReceived ) { - let urls = this.onHeadersReceived.urls || ['']; - let types = onHeadersReceivedTypes; - wrApi.onHeadersReceived.addListener( - onHeadersReceived, - { urls: urls, types: types }, - this.onHeadersReceived.extra - ); - } -}; - -/******************************************************************************/ diff --git a/tools/make-webext.sh b/tools/make-webext.sh index d65f53ef5..fc1a80640 100755 --- a/tools/make-webext.sh +++ b/tools/make-webext.sh @@ -28,7 +28,13 @@ cp LICENSE.txt $DES/ cp platform/webext/manifest.json $DES/ cp platform/webext/vapi-usercss.js $DES/js/ -cp platform/webext/vapi-webrequest.js $DES/js/ + +# https://github.com/uBlockOrigin/uBlock-issues/issues/407 +echo "*** uBlock0.webext: concatenating vapi-webrequest.js" +cat platform/chromium/vapi-webrequest.js > /tmp/vapi-webrequest.js +echo >> /tmp/contentscript.js +grep -v "^'use strict';$" platform/firefox/vapi-webrequest.js >> /tmp/vapi-webrequest.js +mv /tmp/vapi-webrequest.js $DES/js/vapi-webrequest.js echo "*** uBlock0.webext: concatenating content scripts" cat $DES/js/vapi-usercss.js > /tmp/contentscript.js