attribute.js (9293B) - 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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305/******************************************************************************* 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 { runAt } from './run-at.js'; import { safeSelf } from './safe-self.js'; /******************************************************************************/ export function setAttrFn( trusted = false, logPrefix, selector = '', attr = '', value = '' ) { if ( selector === '' ) { return; } if ( attr === '' ) { return; } const safe = safeSelf(); const copyFrom = trusted === false && /^\[.+\]$/.test(value) ? value.slice(1, -1) : ''; const extractValue = elem => copyFrom !== '' ? elem.getAttribute(copyFrom) || '' : value; const applySetAttr = ( ) => { let elems; try { elems = document.querySelectorAll(selector); } catch { return false; } for ( const elem of elems ) { const before = elem.getAttribute(attr); const after = extractValue(elem); if ( after === before ) { continue; } if ( after !== '' && /^on/i.test(attr) ) { if ( attr.toLowerCase() in elem ) { continue; } } elem.setAttribute(attr, after); safe.uboLog(logPrefix, `${attr}="${after}"`); } return true; }; let observer, timer; const onDomChanged = mutations => { if ( timer !== undefined ) { return; } let shouldWork = false; for ( const mutation of mutations ) { if ( mutation.addedNodes.length === 0 ) { continue; } for ( const node of mutation.addedNodes ) { if ( node.nodeType !== 1 ) { continue; } shouldWork = true; break; } if ( shouldWork ) { break; } } if ( shouldWork === false ) { return; } timer = self.requestAnimationFrame(( ) => { timer = undefined; applySetAttr(); }); }; const start = ( ) => { if ( applySetAttr() === false ) { return; } observer = new MutationObserver(onDomChanged); observer.observe(document.body, { subtree: true, childList: true, }); }; runAt(( ) => { start(); }, 'idle'); } registerScriptlet(setAttrFn, { name: 'set-attr.fn', dependencies: [ runAt, safeSelf, ], }); /** * @scriptlet set-attr * * @description * Sets the specified attribute on the specified elements. This scriptlet runs * once when the page loads then afterward on DOM mutations. * * Reference: https://github.com/AdguardTeam/Scriptlets/blob/master/src/scriptlets/set-attr.js * * @param selector * A CSS selector for the elements to target. * * @param attr * The name of the attribute to modify. * * @param value * The new value of the attribute. Supported values: * - `''`: empty string (default) * - `true` * - `false` * - positive decimal integer 0 <= value < 32768 * - `[other]`: copy the value from attribute `other` on the same element * * */ export function setAttr( selector = '', attr = '', value = '' ) { const safe = safeSelf(); const logPrefix = safe.makeLogPrefix('set-attr', selector, attr, value); const validValues = [ '', 'false', 'true' ]; if ( validValues.includes(value.toLowerCase()) === false ) { if ( /^\d+$/.test(value) ) { const n = parseInt(value, 10); if ( n >= 32768 ) { return; } value = `${n}`; } else if ( /^\[.+\]$/.test(value) === false ) { return; } } setAttrFn(false, logPrefix, selector, attr, value); } registerScriptlet(setAttr, { name: 'set-attr.js', dependencies: [ safeSelf, setAttrFn, ], world: 'ISOLATED', }); /** * @trustedScriptlet trusted-set-attr * * @description * Sets the specified attribute on the specified elements. This scriptlet runs * once when the page loads then afterward on DOM mutations. * * Reference: https://github.com/AdguardTeam/Scriptlets/blob/master/wiki/about-trusted-scriptlets.md#-%EF%B8%8F-trusted-set-attr * * @param selector * A CSS selector for the elements to target. * * @param attr * The name of the attribute to modify. * * @param value * The new value of the attribute. Since the scriptlet requires a trusted * source, the value can be anything. * * */ export function trustedSetAttr( selector = '', attr = '', value = '' ) { const safe = safeSelf(); const logPrefix = safe.makeLogPrefix('trusted-set-attr', selector, attr, value); setAttrFn(true, logPrefix, selector, attr, value); } registerScriptlet(trustedSetAttr, { name: 'trusted-set-attr.js', requiresTrust: true, dependencies: [ safeSelf, setAttrFn, ], world: 'ISOLATED', }); /** * @scriptlet remove-attr * * @description * Remove one or more attributes from a set of elements. * * @param attribute * The name of the attribute(s) to remove. This can be a list of space- * separated attribute names. * * @param [selector] * Optional. A CSS selector for the elements to target. Default to * `[attribute]`, or `[attribute1],[attribute2],...` if more than one * attribute name is specified. * * @param [behavior] * Optional. Space-separated tokens which modify the default behavior. * - `asap`: Try to remove the attribute as soon as possible. Default behavior * is to remove the attribute(s) asynchronously. * - `stay`: Keep trying to remove the specified attribute(s) on DOM mutations. * */ export function removeAttr( rawToken = '', rawSelector = '', behavior = '' ) { if ( typeof rawToken !== 'string' ) { return; } if ( rawToken === '' ) { return; } const safe = safeSelf(); const logPrefix = safe.makeLogPrefix('remove-attr', rawToken, rawSelector, behavior); const tokens = safe.String_split.call(rawToken, /\s*\|\s*/); const selector = tokens .map(a => `${rawSelector}[${CSS.escape(a)}]`) .join(','); if ( safe.logLevel > 1 ) { safe.uboLog(logPrefix, `Target selector:\n\t${selector}`); } const asap = /\basap\b/.test(behavior); let timerId; const rmattrAsync = ( ) => { if ( timerId !== undefined ) { return; } timerId = safe.onIdle(( ) => { timerId = undefined; rmattr(); }, { timeout: 17 }); }; const rmattr = ( ) => { if ( timerId !== undefined ) { safe.offIdle(timerId); timerId = undefined; } try { const nodes = document.querySelectorAll(selector); for ( const node of nodes ) { for ( const attr of tokens ) { if ( node.hasAttribute(attr) === false ) { continue; } node.removeAttribute(attr); safe.uboLog(logPrefix, `Removed attribute '${attr}'`); } } } catch { } }; const mutationHandler = mutations => { if ( timerId !== undefined ) { return; } let skip = true; for ( let i = 0; i < mutations.length && skip; i++ ) { const { type, addedNodes, removedNodes } = mutations[i]; if ( type === 'attributes' ) { skip = false; } for ( let j = 0; j < addedNodes.length && skip; j++ ) { if ( addedNodes[j].nodeType === 1 ) { skip = false; break; } } for ( let j = 0; j < removedNodes.length && skip; j++ ) { if ( removedNodes[j].nodeType === 1 ) { skip = false; break; } } } if ( skip ) { return; } asap ? rmattr() : rmattrAsync(); }; const start = ( ) => { rmattr(); if ( /\bstay\b/.test(behavior) === false ) { return; } const observer = new MutationObserver(mutationHandler); observer.observe(document, { attributes: true, attributeFilter: tokens, childList: true, subtree: true, }); }; runAt(( ) => { start(); }, safe.String_split.call(behavior, /\s+/)); } registerScriptlet(removeAttr, { name: 'remove-attr.js', aliases: [ 'ra.js', ], dependencies: [ runAt, safeSelf, ], }); /******************************************************************************/