aboutsummaryrefslogtreecommitdiffstats
path: root/src/js/contentscript-start.js
blob: 39c592650deb9ec8bd17e5e419d2eb793c57862c (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
143
144
145
146
147
148
149
150
151
152
/*******************************************************************************

    µBlock - a Chromium browser extension to block requests.
    Copyright (C) 2014 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
*/

/* jshint multistr: true */
/* global chrome */

// Injected into content pages

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

// OK, I keep changing my mind whether a closure should be used or not. This
// will be the rule: if there are any variables directly accessed on a regular
// basis, use a closure so that they are cached. Otherwise I don't think the
// overhead of a closure is worth it. That's my understanding.

(function() {

// because Safari
if (location.protocol !== "http:" && location.protocol !== "https:") {
    return;
}

var localMessager = vAPI.messaging.channel('contentscript-start.js');


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

// Domain-based ABP cosmetic filters.
// These can be inserted before the DOM is loaded.

var cosmeticFilters = function(details) {
    // Maybe uBlock's style tag was already injected?
    var style = document.getElementById('ublock-preload-1ae7a5f130fc79b4fdb8a4272d9426b5');
    if ( style !== null ) {
        return;
    }
    style = document.createElement('style');
    style.setAttribute('id', 'ublock-preload-1ae7a5f130fc79b4fdb8a4272d9426b5');
    var donthide = details.cosmeticDonthide;
    var hide = details.cosmeticHide;
    if ( donthide.length !== 0 ) {
        donthide = donthide.length !== 1 ? donthide.join(',\n') : donthide[0];
        donthide = donthide.split(',\n');
        style.setAttribute('data-ublock-exceptions', JSON.stringify(donthide));
        // https://github.com/gorhill/uBlock/issues/143
        if ( hide.length !== 0 ) {
            // I chose to use Array.indexOf() instead of converting the array to
            // a map, then deleting whitelisted selectors, and then converting
            // back the map into an array, because there are typically very few
            // exception filters, if any.
            hide = hide.length !== 1 ? hide.join(',\n') : hide[0];
            hide = hide.split(',\n');
            var i = donthide.length, j;
            while ( i-- ) {
                j = hide.indexOf(donthide[i]);
                if ( j !== -1 ) {
                    hide.splice(j, 1);
                }
            }
        }
    }
    if ( hide.length !== 0 ) {
        var text = hide.join(',\n');
        hideElements(text);
        // The linefeed before the style block is very important: do not remove!
        style.appendChild(document.createTextNode(text + '\n{display:none !important;}'));
        //console.debug('µBlock> "%s" cosmetic filters: injecting %d CSS rules:', details.domain, details.hide.length, hideStyleText);
    }
    var parent = document.head || document.documentElement;
    if ( parent ) {
        parent.appendChild(style);
    }
};

var netFilters = function(details) {
    var parent = document.head || document.documentElement;
    if ( !parent ) {
        return;
    }
    var style = document.createElement('style');
    style.setAttribute('class', 'ublock-preload-1ae7a5f130fc79b4fdb8a4272d9426b5');
    var text = details.netHide.join(',\n');
    var css = details.netCollapse ?
        '\n{display:none !important;}' :
        '\n{visibility:hidden !important;}';
    style.appendChild(document.createTextNode(text + css));
    parent.appendChild(style);
    //console.debug('document.querySelectorAll("%s") = %o', text, document.querySelectorAll(text));
};

var filteringHandler = function(details) {
    if ( details ) {
        if ( details.cosmeticHide.length !== 0 || details.cosmeticDonthide.length !== 0 ) {
            cosmeticFilters(details);
        }
        if ( details.netHide.length !== 0 ) {
            netFilters(details);
        }
        // The port will never be used again at this point, disconnecting allows
        // the browser to flush this script from memory.
    }
    // Do not close the port before we are done.
    localMessager.close();
};

var hideElements = function(selectors) {
    if ( document.body === null ) {
        return;
    }
    // https://github.com/gorhill/uBlock/issues/158
    // Using CSSStyleDeclaration.setProperty is more reliable
    var elems = document.querySelectorAll(selectors);
    var i = elems.length;
    while ( i-- ) {
        elems[i].style.setProperty('display', 'none', 'important');
    }
};

localMessager.send(
    {
        what: 'retrieveDomainCosmeticSelectors',
        pageURL: window.location.href,
        locationURL: window.location.href
    },
    filteringHandler
);

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

})();

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