2121
2222// Injected into content pages
2323
24+ /******************************************************************************/
25+
26+ // ABP cosmetic filters
27+
28+ var CosmeticFiltering = function ( ) {
29+ this . classSelectors = null ;
30+ this . idSelectors = null ;
31+ this . classesFromNodeList ( document . querySelectorAll ( '*[class]' ) ) ;
32+ this . idsFromNodeList ( document . querySelectorAll ( '*[id]' ) ) ;
33+ this . retrieve ( ) ;
34+ } ;
35+
36+ CosmeticFiltering . prototype . retrieve = function ( ) {
37+ var a1 = this . classSelectors !== null ? Object . keys ( this . classSelectors ) : [ ] ;
38+ var a2 = this . idSelectors !== null ? this . idSelectors : [ ] ;
39+ var selectors = a1 . concat ( a2 ) ;
40+ if ( selectors . length > 0 ) {
41+ chrome . runtime . sendMessage ( {
42+ what : 'retrieveABPHideSelectors' ,
43+ selectors : selectors ,
44+ locationURL : window . location . href
45+ } , this . retrieveHandler ) ;
46+ }
47+ this . idSelectors = null ;
48+ this . classSelectors = null ;
49+ } ;
50+
51+ CosmeticFiltering . prototype . retrieveHandler = function ( selectors ) {
52+ if ( ! selectors ) {
53+ return ;
54+ }
55+ var styleText = [ ] ;
56+ if ( selectors . hide . length > 0 ) {
57+ var hideStyleText = '{{hideSelectors}} {display:none;}'
58+ . replace ( '{{hideSelectors}}' , selectors . hide ) ;
59+ styleText . push ( hideStyleText ) ;
60+ console . log ( 'HTTPSB> ABP cosmetic filters: injecting CSS rules:' , hideStyleText ) ;
61+ }
62+ if ( selectors . donthide . length > 0 ) {
63+ var dontHideStyleText = '{{donthideSelectors}} {display:initial;}'
64+ . replace ( '{{donthideSelectors}}' , selectors . donthide ) ;
65+ styleText . push ( donthideStyleText ) ;
66+ console . log ( 'HTTPSB> ABP cosmetic filters: injecting CSS rules:' , donthideStyleText ) ;
67+ }
68+ if ( styleText . length > 0 ) {
69+ var style = document . createElement ( 'style' ) ;
70+ style . appendChild ( document . createTextNode ( styleText . join ( '' ) ) ) ;
71+ document . documentElement . appendChild ( style ) ;
72+ }
73+ } ;
74+
75+ CosmeticFiltering . prototype . classesFromNodeList = function ( nodes ) {
76+ if ( ! nodes ) {
77+ return ;
78+ }
79+ if ( this . classSelectors === null ) {
80+ this . classSelectors = { } ;
81+ }
82+ var classNames , className , j ;
83+ var i = nodes . length ;
84+ while ( i -- ) {
85+ className = nodes [ i ] . className ;
86+ if ( typeof className !== 'string' ) {
87+ continue ;
88+ }
89+ className = className . trim ( ) ;
90+ if ( className === '' ) {
91+ continue ;
92+ }
93+ if ( className . indexOf ( ' ' ) < 0 ) {
94+ this . classSelectors [ '.' + className ] = true ;
95+ continue ;
96+ }
97+ classNames = className . trim ( ) . split ( / \s + / ) ;
98+ j = classNames . length ;
99+ while ( j -- ) {
100+ className = classNames [ j ] ;
101+ if ( className !== '' ) {
102+ this . classSelectors [ '.' + className ] = true ;
103+ }
104+ }
105+ }
106+ } ;
107+
108+ CosmeticFiltering . prototype . idsFromNodeList = function ( nodes ) {
109+ if ( ! nodes ) {
110+ return ;
111+ }
112+ if ( this . idSelectors === null ) {
113+ this . idSelectors = [ ] ;
114+ }
115+ var i = nodes . length ;
116+ while ( i -- ) {
117+ id = nodes [ i ] . id ;
118+ if ( typeof id !== 'string' ) {
119+ continue ;
120+ }
121+ id = id . trim ( ) ;
122+ if ( id === '' ) {
123+ continue ;
124+ }
125+ this . idSelectors [ i ] = '#' + id ;
126+ }
127+ } ;
128+
129+ CosmeticFiltering . prototype . allFromNodeList = function ( nodes ) {
130+ this . classesFromNodeList ( nodes ) ;
131+ this . idsFromNodeList ( nodes ) ;
132+ } ;
133+
134+
135+ var adbCosmeticFiltering = new CosmeticFiltering ( ) ;
136+
137+ /******************************************************************************/
24138/******************************************************************************/
25139/*------------[ Unrendered Noscript (because CSP) Workaround ]----------------*/
26140
@@ -50,6 +164,7 @@ var checkScriptBlacklisted = function() {
50164 } , checkScriptBlacklistedHandler ) ;
51165} ;
52166
167+ /******************************************************************************/
53168/******************************************************************************/
54169
55170var localStorageHandler = function ( mustRemove ) {
@@ -59,6 +174,7 @@ var localStorageHandler = function(mustRemove) {
59174 }
60175} ;
61176
177+ /******************************************************************************/
62178/******************************************************************************/
63179
64180var nodesAddedHandler = function ( nodeList , summary ) {
@@ -114,6 +230,9 @@ var nodesAddedHandler = function(nodeList, summary) {
114230 break ;
115231 }
116232 }
233+
234+ // ABP cosmetic filters:
235+ adbCosmeticFiltering . allFromNodeList ( nodeList ) ;
117236} ;
118237
119238/******************************************************************************/
@@ -141,6 +260,8 @@ var mutationObservedHandler = function(mutations) {
141260 if ( summary . mustReport ) {
142261 chrome . runtime . sendMessage ( summary ) ;
143262 }
263+
264+ adbCosmeticFiltering . retrieve ( ) ;
144265} ;
145266
146267/******************************************************************************/
@@ -196,6 +317,7 @@ var firstObservationHandler = function() {
196317 chrome . runtime . sendMessage ( summary ) ;
197318} ;
198319
320+ /******************************************************************************/
199321/******************************************************************************/
200322
201323var loadHandler = function ( ) {
@@ -239,3 +361,4 @@ if ( /^https?:\/\/./.test(window.location.href) ) {
239361 }
240362}
241363
364+ /******************************************************************************/
0 commit comments