spoof-css.js (6544B) - View raw
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163/******************************************************************************* uBlock Origin - a comprehensive, efficient content blocker 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 */ import { registerScriptlet } from './base.js'; import { safeSelf } from './safe-self.js'; /** * @scriptlet spoof-css.js * * @description * Spoof the value of CSS properties. * * @param selector * A CSS selector for the element(s) to target. * * @param [property, value, ...] * A list of property-value pairs of the style properties to spoof to the * specified values. * * */ export function spoofCSS( selector, ...args ) { if ( typeof selector !== 'string' ) { return; } if ( selector === '' ) { return; } const toCamelCase = s => s.replace(/-[a-z]/g, s => s.charAt(1).toUpperCase()); const propToValueMap = new Map(); const privatePropToValueMap = new Map(); for ( let i = 0; i < args.length; i += 2 ) { const prop = toCamelCase(args[i+0]); if ( prop === '' ) { break; } const value = args[i+1]; if ( typeof value !== 'string' ) { break; } if ( prop.charCodeAt(0) === 0x5F /* _ */ ) { privatePropToValueMap.set(prop, value); } else { propToValueMap.set(prop, value); } } const safe = safeSelf(); const logPrefix = safe.makeLogPrefix('spoof-css', selector, ...args); const instanceProperties = [ 'cssText', 'length', 'parentRule' ]; const spoofStyle = (prop, real) => { const normalProp = toCamelCase(prop); const shouldSpoof = propToValueMap.has(normalProp); const value = shouldSpoof ? propToValueMap.get(normalProp) : real; if ( shouldSpoof ) { safe.uboLog(logPrefix, `Spoofing ${prop} to ${value}`); } return value; }; const cloackFunc = (fn, thisArg, name) => { const trap = fn.bind(thisArg); Object.defineProperty(trap, 'name', { value: name }); Object.defineProperty(trap, 'toString', { value: ( ) => `function ${name}() { [native code] }` }); return trap; }; self.getComputedStyle = new Proxy(self.getComputedStyle, { apply: function(target, thisArg, args) { // eslint-disable-next-line no-debugger if ( privatePropToValueMap.has('_debug') ) { debugger; } const style = Reflect.apply(target, thisArg, args); const targetElements = new WeakSet(document.querySelectorAll(selector)); if ( targetElements.has(args[0]) === false ) { return style; } const proxiedStyle = new Proxy(style, { get(target, prop) { if ( typeof target[prop] === 'function' ) { if ( prop === 'getPropertyValue' ) { return cloackFunc(function getPropertyValue(prop) { return spoofStyle(prop, target[prop]); }, target, 'getPropertyValue'); } return cloackFunc(target[prop], target, prop); } if ( instanceProperties.includes(prop) ) { return Reflect.get(target, prop); } return spoofStyle(prop, Reflect.get(target, prop)); }, getOwnPropertyDescriptor(target, prop) { if ( propToValueMap.has(prop) ) { return { configurable: true, enumerable: true, value: propToValueMap.get(prop), writable: true, }; } return Reflect.getOwnPropertyDescriptor(target, prop); }, }); return proxiedStyle; }, get(target, prop) { if ( prop === 'toString' ) { return target.toString.bind(target); } return Reflect.get(target, prop); }, }); Element.prototype.getBoundingClientRect = new Proxy(Element.prototype.getBoundingClientRect, { apply: function(target, thisArg, args) { // eslint-disable-next-line no-debugger if ( privatePropToValueMap.has('_debug') ) { debugger; } const rect = Reflect.apply(target, thisArg, args); const targetElements = new WeakSet(document.querySelectorAll(selector)); if ( targetElements.has(thisArg) === false ) { return rect; } let { x, y, height, width } = rect; if ( privatePropToValueMap.has('_rectx') ) { x = parseFloat(privatePropToValueMap.get('_rectx')); } if ( privatePropToValueMap.has('_recty') ) { y = parseFloat(privatePropToValueMap.get('_recty')); } if ( privatePropToValueMap.has('_rectw') ) { width = parseFloat(privatePropToValueMap.get('_rectw')); } else if ( propToValueMap.has('width') ) { width = parseFloat(propToValueMap.get('width')); } if ( privatePropToValueMap.has('_recth') ) { height = parseFloat(privatePropToValueMap.get('_recth')); } else if ( propToValueMap.has('height') ) { height = parseFloat(propToValueMap.get('height')); } return new self.DOMRect(x, y, width, height); }, get(target, prop) { if ( prop === 'toString' ) { return target.toString.bind(target); } return Reflect.get(target, prop); }, }); } registerScriptlet(spoofCSS, { name: 'spoof-css.js', dependencies: [ safeSelf, ], });