|
| 1 | +/******************************************************************************* |
| 2 | +
|
| 3 | + uBlock Origin - a comprehensive, efficient content blocker |
| 4 | + Copyright (C) 2019-present Raymond Hill |
| 5 | +
|
| 6 | + This program is free software: you can redistribute it and/or modify |
| 7 | + it under the terms of the GNU General Public License as published by |
| 8 | + the Free Software Foundation, either version 3 of the License, or |
| 9 | + (at your option) any later version. |
| 10 | +
|
| 11 | + This program is distributed in the hope that it will be useful, |
| 12 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + GNU General Public License for more details. |
| 15 | +
|
| 16 | + You should have received a copy of the GNU General Public License |
| 17 | + along with this program. If not, see {http://www.gnu.org/licenses/}. |
| 18 | +
|
| 19 | + Home: https://github.com/gorhill/uBlock |
| 20 | +
|
| 21 | + The scriptlets below are meant to be injected only into a |
| 22 | + web page context. |
| 23 | +*/ |
| 24 | + |
| 25 | +import { registerScriptlet } from './base.js'; |
| 26 | +import { safeSelf } from './safe-self.js'; |
| 27 | + |
| 28 | +/** |
| 29 | + * @scriptlet spoof-css.js |
| 30 | + * |
| 31 | + * @description |
| 32 | + * Spoof the value of CSS properties. |
| 33 | + * |
| 34 | + * @param selector |
| 35 | + * A CSS selector for the element(s) to target. |
| 36 | + * |
| 37 | + * @param [property, value, ...] |
| 38 | + * A list of property-value pairs of the style properties to spoof to the |
| 39 | + * specified values. |
| 40 | +* |
| 41 | + * */ |
| 42 | + |
| 43 | +export function spoofCSS( |
| 44 | + selector, |
| 45 | + ...args |
| 46 | +) { |
| 47 | + if ( typeof selector !== 'string' ) { return; } |
| 48 | + if ( selector === '' ) { return; } |
| 49 | + const toCamelCase = s => s.replace(/-[a-z]/g, s => s.charAt(1).toUpperCase()); |
| 50 | + const propToValueMap = new Map(); |
| 51 | + const privatePropToValueMap = new Map(); |
| 52 | + for ( let i = 0; i < args.length; i += 2 ) { |
| 53 | + const prop = toCamelCase(args[i+0]); |
| 54 | + if ( typeof prop !== 'string' ) { break; } |
| 55 | + if ( prop === '' ) { break; } |
| 56 | + const value = args[i+1]; |
| 57 | + if ( typeof value !== 'string' ) { break; } |
| 58 | + if ( prop.charCodeAt(0) === 0x5F /* _ */ ) { |
| 59 | + privatePropToValueMap.set(prop, value); |
| 60 | + } else { |
| 61 | + propToValueMap.set(toCamelCase(prop), value); |
| 62 | + } |
| 63 | + } |
| 64 | + const safe = safeSelf(); |
| 65 | + const logPrefix = safe.makeLogPrefix('spoof-css', selector, ...args); |
| 66 | + const instanceProperties = [ 'cssText', 'length', 'parentRule' ]; |
| 67 | + const spoofStyle = (prop, real) => { |
| 68 | + const normalProp = toCamelCase(prop); |
| 69 | + const shouldSpoof = propToValueMap.has(normalProp); |
| 70 | + const value = shouldSpoof ? propToValueMap.get(normalProp) : real; |
| 71 | + if ( shouldSpoof ) { |
| 72 | + safe.uboLog(logPrefix, `Spoofing ${prop} to ${value}`); |
| 73 | + } |
| 74 | + return value; |
| 75 | + }; |
| 76 | + const cloackFunc = (fn, thisArg, name) => { |
| 77 | + const trap = fn.bind(thisArg); |
| 78 | + Object.defineProperty(trap, 'name', { value: name }); |
| 79 | + Object.defineProperty(trap, 'toString', { |
| 80 | + value: ( ) => `function ${name}() { [native code] }` |
| 81 | + }); |
| 82 | + return trap; |
| 83 | + }; |
| 84 | + self.getComputedStyle = new Proxy(self.getComputedStyle, { |
| 85 | + apply: function(target, thisArg, args) { |
| 86 | + // eslint-disable-next-line no-debugger |
| 87 | + if ( privatePropToValueMap.has('_debug') ) { debugger; } |
| 88 | + const style = Reflect.apply(target, thisArg, args); |
| 89 | + const targetElements = new WeakSet(document.querySelectorAll(selector)); |
| 90 | + if ( targetElements.has(args[0]) === false ) { return style; } |
| 91 | + const proxiedStyle = new Proxy(style, { |
| 92 | + get(target, prop) { |
| 93 | + if ( typeof target[prop] === 'function' ) { |
| 94 | + if ( prop === 'getPropertyValue' ) { |
| 95 | + return cloackFunc(function getPropertyValue(prop) { |
| 96 | + return spoofStyle(prop, target[prop]); |
| 97 | + }, target, 'getPropertyValue'); |
| 98 | + } |
| 99 | + return cloackFunc(target[prop], target, prop); |
| 100 | + } |
| 101 | + if ( instanceProperties.includes(prop) ) { |
| 102 | + return Reflect.get(target, prop); |
| 103 | + } |
| 104 | + return spoofStyle(prop, Reflect.get(target, prop)); |
| 105 | + }, |
| 106 | + getOwnPropertyDescriptor(target, prop) { |
| 107 | + if ( propToValueMap.has(prop) ) { |
| 108 | + return { |
| 109 | + configurable: true, |
| 110 | + enumerable: true, |
| 111 | + value: propToValueMap.get(prop), |
| 112 | + writable: true, |
| 113 | + }; |
| 114 | + } |
| 115 | + return Reflect.getOwnPropertyDescriptor(target, prop); |
| 116 | + }, |
| 117 | + }); |
| 118 | + return proxiedStyle; |
| 119 | + }, |
| 120 | + get(target, prop) { |
| 121 | + if ( prop === 'toString' ) { |
| 122 | + return target.toString.bind(target); |
| 123 | + } |
| 124 | + return Reflect.get(target, prop); |
| 125 | + }, |
| 126 | + }); |
| 127 | + Element.prototype.getBoundingClientRect = new Proxy(Element.prototype.getBoundingClientRect, { |
| 128 | + apply: function(target, thisArg, args) { |
| 129 | + // eslint-disable-next-line no-debugger |
| 130 | + if ( privatePropToValueMap.has('_debug') ) { debugger; } |
| 131 | + const rect = Reflect.apply(target, thisArg, args); |
| 132 | + const targetElements = new WeakSet(document.querySelectorAll(selector)); |
| 133 | + if ( targetElements.has(thisArg) === false ) { return rect; } |
| 134 | + let { x, y, height, width } = rect; |
| 135 | + if ( privatePropToValueMap.has('_rectx') ) { |
| 136 | + x = parseFloat(privatePropToValueMap.get('_rectx')); |
| 137 | + } |
| 138 | + if ( privatePropToValueMap.has('_recty') ) { |
| 139 | + y = parseFloat(privatePropToValueMap.get('_recty')); |
| 140 | + } |
| 141 | + if ( privatePropToValueMap.has('_rectw') ) { |
| 142 | + width = parseFloat(privatePropToValueMap.get('_rectw')); |
| 143 | + } else if ( propToValueMap.has('width') ) { |
| 144 | + width = parseFloat(propToValueMap.get('width')); |
| 145 | + } |
| 146 | + if ( privatePropToValueMap.has('_recth') ) { |
| 147 | + height = parseFloat(privatePropToValueMap.get('_recth')); |
| 148 | + } else if ( propToValueMap.has('height') ) { |
| 149 | + height = parseFloat(propToValueMap.get('height')); |
| 150 | + } |
| 151 | + return new self.DOMRect(x, y, width, height); |
| 152 | + }, |
| 153 | + get(target, prop) { |
| 154 | + if ( prop === 'toString' ) { |
| 155 | + return target.toString.bind(target); |
| 156 | + } |
| 157 | + return Reflect.get(target, prop); |
| 158 | + }, |
| 159 | + }); |
| 160 | +} |
| 161 | +registerScriptlet(spoofCSS, { |
| 162 | + name: 'spoof-css.js', |
| 163 | + dependencies: [ |
| 164 | + safeSelf, |
| 165 | + ], |
| 166 | +}); |
0 commit comments