Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: lib/elemHide.js

Issue 6266581101314048: WIP: Have one global element hide stylesheet and inject the other ones per site (Closed)
Patch Set: rebased Created Aug. 19, 2014, 12:15 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | lib/elemHideHitRegistration.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <http://adblockplus.org/>, 2 * This file is part of Adblock Plus <http://adblockplus.org/>,
3 * Copyright (C) 2006-2014 Eyeo GmbH 3 * Copyright (C) 2006-2014 Eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
134 { 134 {
135 if (topic != "content-document-global-created") 135 if (topic != "content-document-global-created")
136 return; 136 return;
137 137
138 if (!Prefs.enabled) 138 if (!Prefs.enabled)
139 return; 139 return;
140 140
141 if (Policy.shouldNeverBlockWindow(subject)) 141 if (Policy.shouldNeverBlockWindow(subject))
142 return; 142 return;
143 143
144 let domain = null;
145 let filters = null;
146 try
147 {
148 domain = subject.document.documentURIObject.host;
149 if (domain)
150 filters = ElemHide.getFiltersWithKeyForDomain(domain, true);
151 } catch (e) {}
152
153 if (filters)
154 {
155 let list = Object.create(null);
156 for (let {key, filter} of filters)
157 list[key] = filter;
158
159 let css = "";
160 for (let line in this._generateCSSContent(list, false))
161 css += line + "\n";
162
163 let uri = Services.io.newURI("data:text/css;base64," + btoa(css), null, nu ll);
164 try
165 {
166 let utils = subject.QueryInterface(Ci.nsIInterfaceRequestor)
167 .getInterface(Ci.nsIDOMWindowUtils);
168 utils.loadSheet(uri, Ci.nsIStyleSheetService.USER_SHEET);
169 }
170 catch (e)
171 {
172 Cu.reportError(e);
173 }
174 }
175
144 if (styleSheet) 176 if (styleSheet)
145 { 177 {
146 try 178 try
147 { 179 {
148 let utils = subject.QueryInterface(Ci.nsIInterfaceRequestor) 180 let utils = subject.QueryInterface(Ci.nsIInterfaceRequestor)
149 .getInterface(Ci.nsIDOMWindowUtils); 181 .getInterface(Ci.nsIDOMWindowUtils);
150 utils.addSheet(styleSheet, Ci.nsIStyleSheetService.USER_SHEET); 182 utils.addSheet(styleSheet, Ci.nsIStyleSheetService.USER_SHEET);
151 } 183 }
152 catch (e) 184 catch (e)
153 { 185 {
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
294 else if (!Prefs.enabled && ElemHide.applied) 326 else if (!Prefs.enabled && ElemHide.applied)
295 { 327 {
296 ElemHide.unapply(); 328 ElemHide.unapply();
297 TimeLine.log("ElemHide.unapply() finished"); 329 TimeLine.log("ElemHide.unapply() finished");
298 } 330 }
299 331
300 TimeLine.leave("ElemHide.apply() done (no file changes)"); 332 TimeLine.leave("ElemHide.apply() done (no file changes)");
301 return; 333 return;
302 } 334 }
303 335
304 IO.writeToFile(styleURL.file, this._generateCSSContent(), function(e) 336 IO.writeToFile(styleURL.file, this._generateCSSContent(filterByKey, useNew), function(e)
305 { 337 {
306 TimeLine.enter("ElemHide.apply() write callback"); 338 TimeLine.enter("ElemHide.apply() write callback");
307 this._applying = false; 339 this._applying = false;
308 340
309 // _generateCSSContent is throwing NS_ERROR_NOT_AVAILABLE to indicate that 341 // _generateCSSContent is throwing NS_ERROR_NOT_AVAILABLE to indicate that
310 // there are no filters. If that exception is passed through XPCOM we will 342 // there are no filters. If that exception is passed through XPCOM we will
311 // see a proper exception here, otherwise a number. 343 // see a proper exception here, otherwise a number.
312 let noFilters = (e == Cr.NS_ERROR_NOT_AVAILABLE || (e && e.result == Cr.NS _ERROR_NOT_AVAILABLE)); 344 let noFilters = (e == Cr.NS_ERROR_NOT_AVAILABLE || (e && e.result == Cr.NS _ERROR_NOT_AVAILABLE));
313 if (noFilters) 345 if (noFilters)
314 { 346 {
(...skipping 23 matching lines...) Expand all
338 Utils.styleService.loadAndRegisterSheet(styleURL, Ci.nsIStyleSheet Service.USER_SHEET); 370 Utils.styleService.loadAndRegisterSheet(styleURL, Ci.nsIStyleSheet Service.USER_SHEET);
339 else 371 else
340 styleSheet = Utils.styleService.preloadSheet(styleURL, Ci.nsIStyle SheetService.USER_SHEET); 372 styleSheet = Utils.styleService.preloadSheet(styleURL, Ci.nsIStyle SheetService.USER_SHEET);
341 ElemHide.applied = true; 373 ElemHide.applied = true;
342 } 374 }
343 catch (e) 375 catch (e)
344 { 376 {
345 Cu.reportError(e); 377 Cu.reportError(e);
346 } 378 }
347 TimeLine.log("Applying stylesheet finished"); 379 TimeLine.log("Applying stylesheet finished");
380 Cu.reportError("applying finished");
348 } 381 }
349 382
350 FilterNotifier.triggerListeners("elemhideupdate"); 383 FilterNotifier.triggerListeners("elemhideupdate");
351 } 384 }
352 TimeLine.leave("ElemHide.apply() write callback done"); 385 TimeLine.leave("ElemHide.apply() write callback done");
353 }.bind(this), "ElemHideWrite"); 386 }.bind(this), "ElemHideWrite");
354 387
355 this._applying = true; 388 this._applying = true;
356 389
357 TimeLine.leave("ElemHide.apply() done", "ElemHideWrite"); 390 TimeLine.leave("ElemHide.apply() done", "ElemHideWrite");
358 }, 391 },
359 392
360 _generateCSSContent: function() 393 _generateCSSContent: function(filters, onlyGlobal)
361 { 394 {
362 // Grouping selectors by domains 395 // Grouping selectors by domains
363 TimeLine.log("start grouping selectors"); 396 TimeLine.log("start grouping selectors");
364 let domains = Object.create(null); 397 let domains = Object.create(null);
365 let hasFilters = false; 398 let hasFilters = false;
366 for (let key in filterByKey) 399 for (let key in filters)
367 { 400 {
368 let filter = filterByKey[key]; 401 let filter = filters[key];
369 let domain = filter.selectorDomain || ""; 402 let domain = filter.selectorDomain || "";
370 403
404 if (onlyGlobal && domain)
405 continue;
406
371 let list; 407 let list;
372 if (domain in domains) 408 if (domain in domains)
373 list = domains[domain]; 409 list = domains[domain];
374 else 410 else
375 { 411 {
376 list = Object.create(null); 412 list = Object.create(null);
377 domains[domain] = list; 413 domains[domain] = list;
378 } 414 }
379 list[filter.selector] = key; 415 list[filter.selector] = key;
380 hasFilters = true; 416 hasFilters = true;
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
456 // workaround WebKit bug 132872, also see #419 492 // workaround WebKit bug 132872, also see #419
457 let domains = filter.domains; 493 let domains = filter.domains;
458 494
459 if (specificOnly && (!domains || domains[""])) 495 if (specificOnly && (!domains || domains[""]))
460 continue; 496 continue;
461 497
462 if (filter.isActiveOnDomain(domain) && !this.getException(filter, domain)) 498 if (filter.isActiveOnDomain(domain) && !this.getException(filter, domain))
463 result.push(filter.selector); 499 result.push(filter.selector);
464 } 500 }
465 return result; 501 return result;
502 },
503
504 getFiltersWithKeyForDomain: function(/**String*/ domain)
505 {
506 let result = [];
507 for (let key in filterByKey)
508 {
509 let filter = filterByKey[key];
510
511 if (!filter.domains)
512 continue
513
514 if (filter.isActiveOnDomain(domain) && !this.getException(filter, domain))
515 result.push({key, filter});
516 }
517 return result;
466 } 518 }
519
467 }; 520 };
OLDNEW
« no previous file with comments | « no previous file | lib/elemHideHitRegistration.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld