summaryrefslogtreecommitdiffstats
path: root/src/js/scriptlets/cosmetic-report.js
blob: a968d4de0285bb54fc5895f0f66407a701c52f59 (plain)
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
/*******************************************************************************

    uBlock Origin - a comprehensive, efficient content blocker
    Copyright (C) 2015-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
*/

'use strict';

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

(( ) => {
// >>>>>>>> start of private namespace

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

if ( typeof vAPI !== 'object' ) { return; }
if ( typeof vAPI.domFilterer !== 'object' ) { return; }
if ( vAPI.domFilterer === null ) { return; }

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

const rePseudoElements = /:(?::?after|:?before|:[a-z-]+)$/;

const hasSelector = selector => {
    try {
        return document.querySelector(selector) !== null;
    }
    catch(ex) {
    }
    return false;
};

const safeQuerySelector = selector => {
    const safeSelector = rePseudoElements.test(selector)
        ? selector.replace(rePseudoElements, '')
        : selector;
    try {
        return document.querySelector(safeSelector);
    }
    catch(ex) {
    }
    return null;
};

const safeGroupSelectors = selectors => {
    const arr = Array.isArray(selectors)
        ? selectors
        : Array.from(selectors);
    return arr.map(s => {
        return rePseudoElements.test(s)
            ? s.replace(rePseudoElements, '')
            : s;
    }).join(',\n');
};

const allSelectors = vAPI.domFilterer.getAllSelectors();
const matchedSelectors = [];

if ( Array.isArray(allSelectors.declarative) ) {
    const declarativeSet = new Set();
    for ( const block of allSelectors.declarative ) {
        for ( const selector of block.split(',\n') ) {
            declarativeSet.add(selector);
        }
    }
    if ( hasSelector(safeGroupSelectors(declarativeSet)) ) {
        for ( const selector of declarativeSet ) {
            if ( safeQuerySelector(selector) === null ) { continue; }
            matchedSelectors.push(`##${selector}`);
        }
    }
}

if (
    Array.isArray(allSelectors.procedural) &&
    allSelectors.procedural.length !== 0
) {
    for ( const pselector of allSelectors.procedural ) {
        if ( pselector.hit === false && pselector.exec().length === 0 ) { continue; }
        matchedSelectors.push(`##${pselector.raw}`);
    }
}

if ( Array.isArray(allSelectors.exceptions) ) {
    const exceptionDict = new Map();
    for ( const selector of allSelectors.exceptions ) {
        if ( selector.charCodeAt(0) !== 0x7B /* '{' */ ) {
            exceptionDict.set(selector, selector);
            continue;
        }
        const details = JSON.parse(selector);
        if (
            details.action !== undefined &&
            details.tasks === undefined &&
            details.action[0] === 'style'
        ) {
            exceptionDict.set(details.selector, details.raw);
            continue;
        }
        const pselector = vAPI.domFilterer.createProceduralFilter(details);
        if ( pselector.test() === false ) { continue; }
        matchedSelectors.push(`#@#${pselector.raw}`);
    }
    if (
        exceptionDict.size !== 0 &&
        hasSelector(safeGroupSelectors(exceptionDict.keys()))
    ) {
        for ( const [ selector, raw ] of exceptionDict ) {
            if ( safeQuerySelector(selector) === null ) { continue; }
            matchedSelectors.push(`#@#${raw}`);
        }
    }
}

if ( typeof self.uBO_scriptletsInjected === 'string' ) {
    matchedSelectors.push(...self.uBO_scriptletsInjected.split('\n'));
}

if ( matchedSelectors.length === 0 ) { return; }

return matchedSelectors;

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

// <<<<<<<< end of private namespace
})();