From f9e680f111ff10fc5df4afa6107a3875efb4dd18 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Thu, 4 Jul 2019 14:08:56 -0400 Subject: [PATCH] Convert more resources as immutable Related commit: - https://github.com/gorhill/uBlock/commit/152cea2dfe9d0e35d86314633d612a9ad96ee94f --- src/js/redirect-engine.js | 14 +++++ .../addEventListener-defuser.js | 57 +++++++++++++++++++ .../addEventListener-logger.js | 36 ++++++++++++ src/web_accessible_resources/remove-attr.js | 50 ++++++++++++++++ .../window.open-defuser.js | 43 ++++++++++++++ 5 files changed, 200 insertions(+) create mode 100644 src/web_accessible_resources/addEventListener-defuser.js create mode 100644 src/web_accessible_resources/addEventListener-logger.js create mode 100644 src/web_accessible_resources/remove-attr.js create mode 100644 src/web_accessible_resources/window.open-defuser.js diff --git a/src/js/redirect-engine.js b/src/js/redirect-engine.js index f584794da..f76d3c0e0 100644 --- a/src/js/redirect-engine.js +++ b/src/js/redirect-engine.js @@ -57,6 +57,14 @@ const immutableResources = new Map([ alias: 'aopw.js', redirect: false } ], + [ 'addEventListener-defuser.js', { + alias: 'aeld.js', + redirect: false + } ], + [ 'addEventListener-logger.js', { + alias: 'aell.js', + redirect: false + } ], [ 'addthis_widget.js', { alias: 'addthis.com/addthis_widget.js', inject: false @@ -174,6 +182,10 @@ const immutableResources = new Map([ [ 'popads-dummy.js', { alias: 'popads-dummy.js', } ], + [ 'remove-attr.js', { + alias: 'ra.js', + redirect: false + } ], [ 'scorecardresearch_beacon.js', { alias: 'scorecardresearch.com/beacon.js', inject: false @@ -200,6 +212,8 @@ const immutableResources = new Map([ [ 'webrtc-if.js', { redirect: false } ], + [ 'window.open-defuser.js', { + } ], ]); const mimeMap = { diff --git a/src/web_accessible_resources/addEventListener-defuser.js b/src/web_accessible_resources/addEventListener-defuser.js new file mode 100644 index 000000000..ad6480b4b --- /dev/null +++ b/src/web_accessible_resources/addEventListener-defuser.js @@ -0,0 +1,57 @@ +/******************************************************************************* + + uBlock Origin - a browser extension to block requests. + Copyright (C) 2019-present 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 +*/ + +(function() { + 'use strict'; + let needle1 = '{{1}}'; + if ( needle1 === '' || needle1 === '{{1}}' ) { + needle1 = '.?'; + } else if ( /^\/.+\/$/.test(needle1) ) { + needle1 = needle1.slice(1,-1); + } else { + needle1 = needle1.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); + } + needle1 = new RegExp(needle1); + let needle2 = '{{2}}'; + if ( needle2 === '' || needle2 === '{{2}}' ) { + needle2 = '.?'; + } else if ( /^\/.+\/$/.test(needle2) ) { + needle2 = needle2.slice(1,-1); + } else { + needle2 = needle2.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); + } + needle2 = new RegExp(needle2); + self.EventTarget.prototype.addEventListener = new Proxy( + self.EventTarget.prototype.addEventListener, + { + apply: function(target, thisArg, args) { + const type = args[0].toString(); + const handler = String(args[1]); + if ( + needle1.test(type) === false || + needle2.test(handler) === false + ) { + return target.apply(thisArg, args); + } + } + } + ); +})(); diff --git a/src/web_accessible_resources/addEventListener-logger.js b/src/web_accessible_resources/addEventListener-logger.js new file mode 100644 index 000000000..cd19161cf --- /dev/null +++ b/src/web_accessible_resources/addEventListener-logger.js @@ -0,0 +1,36 @@ +/******************************************************************************* + + uBlock Origin - a browser extension to block requests. + Copyright (C) 2019-present 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 +*/ + +(function() { + 'use strict'; + const log = console.log.bind(console); + self.EventTarget.prototype.addEventListener = new Proxy( + self.EventTarget.prototype.addEventListener, + { + apply: function(target, thisArg, args) { + const type = args[0].toString(); + const handler = String(args[1]); + log('addEventListener("%s", %s)', type, handler); + return target.apply(thisArg, args); + } + } + ); +})(); diff --git a/src/web_accessible_resources/remove-attr.js b/src/web_accessible_resources/remove-attr.js new file mode 100644 index 000000000..c3edc7c50 --- /dev/null +++ b/src/web_accessible_resources/remove-attr.js @@ -0,0 +1,50 @@ +/******************************************************************************* + + uBlock Origin - a browser extension to block requests. + Copyright (C) 2019-present 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 +*/ + +(function() { + 'use strict'; + const token = '{{1}}'; + if ( token === '' || token === '{{1}}' ) { return; } + const tokens = token.split(/\s*\|\s*/); + let selector = '{{2}}'; + if ( selector === '' || selector === '{{2}}' ) { + selector = `[${tokens.join('],[')}]`; + } + const rmattr = function(ev) { + if ( ev ) { + window.removeEventListener(ev.type, rmattr, true); + } + try { + const nodes = document.querySelectorAll(selector); + for ( const node of nodes ) { + for ( const attr of tokens ) { + node.removeAttribute(attr); + } + } + } catch(ex) { + } + }; + if ( document.readyState === 'loading' ) { + window.addEventListener('DOMContentLoaded', rmattr, true); + } else { + rmattr(); + } +})(); diff --git a/src/web_accessible_resources/window.open-defuser.js b/src/web_accessible_resources/window.open-defuser.js new file mode 100644 index 000000000..4558cac20 --- /dev/null +++ b/src/web_accessible_resources/window.open-defuser.js @@ -0,0 +1,43 @@ +/******************************************************************************* + + uBlock Origin - a browser extension to block requests. + Copyright (C) 2019-present 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 +*/ + +(function() { + 'use strict'; + let result = parseInt('{{1}}', 10); + result = isNaN(result) || result !== 0; + let needle = '{{2}}'; + if ( needle === '' || needle === '{{2}}' ) { + needle = '.?'; + } else if ( /^\/.+\/$/.test(needle) ) { + needle = needle.slice(1,-1); + } else { + needle = needle.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); + } + needle = new RegExp(needle); + window.open = new Proxy(window.open, { + apply: function(target, thisArg, args) { + const url = args[0]; + if ( needle.test(url) === result ) { + return target.apply(thisArg, args); + } + } + }); +})();