Add set-attr scriptlet · gorhill/uBlock@786d9b2 (original) (raw)

`@@ -2557,11 +2557,46 @@ function m3uPrune(

`

2557

2557

`});

`

2558

2558

`}

`

2559

2559

``

2560

``

`-

/******************************************************************************/

`

``

2560

`+

/*******************************************************************************

`

``

2561

`+

*

`

``

2562

`+

`

``

2563

`+

*

`

``

2564

`+

`

``

2565

`` +

``

``

2566

`` +

``

``

2567

`+

*

`

``

2568

`+

`

``

2569

`+

*

`

``

2570


 * ```text

``

2571

`+

`

``

2572


 * ```

``

2573

`+

*

`

``

2574

`` +

``

``

2575

`` +

``

``

2576

`` +

``

``

2577

`` +

``

``

2578

`` +

``

``

2579

`` +

``

``

2580

`` +

``

``

2581

`+

`

``

2582

`` +

``

``

2583

`` +

``

``

2584

`+

*

`

``

2585

`+

`

``

2586

`+

*

`

``

2587

`+

`

``

2588

`+

`

``

2589

`+

`

``

2590

`+

*

`

``

2591

`+

`

2561

2592

``

2562

2593

`builtinScriptlets.push({

`

2563

2594

`name: 'href-sanitizer.js',

`

2564

2595

`fn: hrefSanitizer,

`

``

2596

`+

world: 'ISOLATED',

`

``

2597

`+

dependencies: [

`

``

2598

`+

'run-at.fn',

`

``

2599

`+

],

`

2565

2600

`});

`

2566

2601

`function hrefSanitizer(

`

2567

2602

`selector = '',

`

`@@ -2659,14 +2694,32 @@ function hrefSanitizer(

`

2659

2694

`childList: true,

`

2660

2695

`});

`

2661

2696

`};

`

2662

``

`-

if ( document.readyState === 'loading' ) {

`

2663

``

`-

document.addEventListener('DOMContentLoaded', start, { once: true });

`

2664

``

`-

} else {

`

2665

``

`-

start();

`

2666

``

`-

}

`

``

2697

`+

runAt(( ) => { start(); }, 'interactive');

`

2667

2698

`}

`

2668

2699

``

2669

``

`-

/******************************************************************************/

`

``

2700

`+

/*******************************************************************************

`

``

2701

`+

*

`

``

2702

`+

`

``

2703

`+

*

`

``

2704

`+

`

``

2705

`+

`

``

2706

`+

`

``

2707

`+

`

``

2708

`+

*

`

``

2709

`+

`

``

2710

`+

*

`

``

2711


 * ```text

``

2712

`+

`

``

2713


 * ```

``

2714

`+

*

`

``

2715

`` +

``

``

2716

`+

`

``

2717

`+

*

`

``

2718

`+

`

``

2719

`+

*

`

``

2720

`+

`

``

2721

`+

*

`

``

2722

`+

`

2670

2723

``

2671

2724

`builtinScriptlets.push({

`

2672

2725

`name: 'call-nothrow.js',

`

`@@ -2899,6 +2952,118 @@ function setSessionStorageItem(key = '', value = '') {

`

2899

2952

`setLocalStorageItemCore('session', false, key, value);

`

2900

2953

`}

`

2901

2954

``

``

2955

`+

/*******************************************************************************

`

``

2956

`+

*

`

``

2957

`+

`

``

2958

`+

*

`

``

2959

`+

`

``

2960

`+

`

``

2961

`+

`

``

2962

+

``

2963

`+

`

``

2964

`+

*

`

``

2965

`+

`

``

2966

`+

*

`

``

2967


 * ```text

``

2968

`+

`

``

2969


 * ```

``

2970

`+

*

`

``

2971

`` +

``

``

2972

`+

`

``

2973

`` +

``

``

2974

`` +

``

``

2975

`` +

``

``

2976

`` +

``

``

2977

`` +

``

``

2978

`+

`

``

2979

`` +

``

``

2980

`+

`

``

2981

+

``

2982

`+

builtinScriptlets.push({

`

``

2983

`+

name: 'set-attr.js',

`

``

2984

`+

fn: setAttr,

`

``

2985

`+

world: 'ISOLATED',

`

``

2986

`+

dependencies: [

`

``

2987

`+

'run-at.fn',

`

``

2988

`+

],

`

``

2989

`+

});

`

``

2990

`+

function setAttr(

`

``

2991

`+

selector = '',

`

``

2992

`+

attr = '',

`

``

2993

`+

value = ''

`

``

2994

`+

) {

`

``

2995

`+

if ( typeof selector !== 'string' ) { return; }

`

``

2996

`+

if ( selector === '' ) { return; }

`

``

2997

`+

if ( value === '' ) { return; }

`

``

2998

+

``

2999

`+

const validValues = [ '', 'false', 'true' ];

`

``

3000

`+

let copyFrom = '';

`

``

3001

+

``

3002

`+

if ( validValues.includes(value) === false ) {

`

``

3003

`+

if ( /^\d+$/.test(value) ) {

`

``

3004

`+

const n = parseInt(value, 10);

`

``

3005

`+

if ( n >= 32768 ) { return; }

`

``

3006

`` +

value = ${n};

``

``

3007

`+

} else if ( /^[.+]$/.test(value) ) {

`

``

3008

`+

copyFrom = value.slice(1, -1);

`

``

3009

`+

} else {

`

``

3010

`+

return;

`

``

3011

`+

}

`

``

3012

`+

}

`

``

3013

+

``

3014

`+

const extractValue = elem => {

`

``

3015

`+

if ( copyFrom !== '' ) {

`

``

3016

`+

return elem.getAttribute(copyFrom) || '';

`

``

3017

`+

}

`

``

3018

`+

return value;

`

``

3019

`+

};

`

``

3020

+

``

3021

`+

const applySetAttr = ( ) => {

`

``

3022

`+

const elems = [];

`

``

3023

`+

try {

`

``

3024

`+

elems.push(...document.querySelectorAll(selector));

`

``

3025

`+

}

`

``

3026

`+

catch(ex) {

`

``

3027

`+

return false;

`

``

3028

`+

}

`

``

3029

`+

for ( const elem of elems ) {

`

``

3030

`+

const before = elem.getAttribute(attr);

`

``

3031

`+

const after = extractValue(elem);

`

``

3032

`+

if ( after === before ) { continue; }

`

``

3033

`+

elem.setAttribute(attr, after);

`

``

3034

`+

}

`

``

3035

`+

return true;

`

``

3036

`+

};

`

``

3037

`+

let observer, timer;

`

``

3038

`+

const onDomChanged = mutations => {

`

``

3039

`+

if ( timer !== undefined ) { return; }

`

``

3040

`+

let shouldWork = false;

`

``

3041

`+

for ( const mutation of mutations ) {

`

``

3042

`+

if ( mutation.addedNodes.length === 0 ) { continue; }

`

``

3043

`+

for ( const node of mutation.addedNodes ) {

`

``

3044

`+

if ( node.nodeType !== 1 ) { continue; }

`

``

3045

`+

shouldWork = true;

`

``

3046

`+

break;

`

``

3047

`+

}

`

``

3048

`+

if ( shouldWork ) { break; }

`

``

3049

`+

}

`

``

3050

`+

if ( shouldWork === false ) { return; }

`

``

3051

`+

timer = self.requestAnimationFrame(( ) => {

`

``

3052

`+

timer = undefined;

`

``

3053

`+

applySetAttr();

`

``

3054

`+

});

`

``

3055

`+

};

`

``

3056

`+

const start = ( ) => {

`

``

3057

`+

if ( applySetAttr() === false ) { return; }

`

``

3058

`+

observer = new MutationObserver(onDomChanged);

`

``

3059

`+

observer.observe(document.body, {

`

``

3060

`+

subtree: true,

`

``

3061

`+

childList: true,

`

``

3062

`+

});

`

``

3063

`+

};

`

``

3064

`+

runAt(( ) => { start(); }, 'idle');

`

``

3065

`+

}

`

``

3066

+

2902

3067

``

2903

3068

`/*******************************************************************************

`

2904

3069

` *

`