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

Side by Side Diff: include.preload.js

Issue 29331619: Issue 2397 - Integrate CSS property filters into adblockpluschrome (Closed)
Patch Set: Adjust code now that CSSPropertyFilters is a class Created Nov. 30, 2015, 5:37 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 | « dependencies ('k') | metadata.common » ('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 <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2015 Eyeo GmbH 3 * Copyright (C) 2006-2015 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 282 matching lines...) Expand 10 before | Expand all | Expand 10 after
293 // have to create the shadow root before transistions might start (#452). 293 // have to create the shadow root before transistions might start (#452).
294 // 294 //
295 // Also, using shadow DOM causes issues on some Google websites, 295 // Also, using shadow DOM causes issues on some Google websites,
296 // including Google Docs and Gmail (#1770, #2602). 296 // including Google Docs and Gmail (#1770, #2602).
297 if ("createShadowRoot" in document.documentElement && !/\.google\.com$/.test(d ocument.domain)) 297 if ("createShadowRoot" in document.documentElement && !/\.google\.com$/.test(d ocument.domain))
298 { 298 {
299 shadow = document.documentElement.createShadowRoot(); 299 shadow = document.documentElement.createShadowRoot();
300 shadow.appendChild(document.createElement("shadow")); 300 shadow.appendChild(document.createElement("shadow"));
301 } 301 }
302 302
303 var updateStylesheet = function(reinject) 303 function addElemHideSelectors(selectors)
304 { 304 {
305 ext.backgroundPage.sendMessage({type: "get-selectors"}, function(selectors) 305 if (selectors.length == 0)
306 return;
307
308 if (!style)
306 { 309 {
310 // Create <style> element lazily, only if we add styles. Add it to
311 // the shadow DOM if possible. Otherwise fallback to the <head> or
312 // <html> element. If we have injected a style element before that
313 // has been removed (the sheet property is null), create a new one.
314 style = document.createElement("style");
315 (shadow || document.head || document.documentElement).appendChild(style);
316
317 // It can happen that the frame already navigated to a different
318 // document while we were waiting for the background page to respond.
319 // In that case the sheet property will stay null, after addind the
320 // <style> element to the shadow DOM.
321 if (!style.sheet)
322 return;
323
324 observer = reinjectRulesWhenRemoved(document, style);
325 }
326
327 // If using shadow DOM, we have to add the ::content pseudo-element
328 // before each selector, in order to match elements within the
329 // insertion point.
330 if (shadow)
331 selectors = convertSelectorsForShadowDOM(selectors);
332
333 // WebKit (and Blink?) apparently chokes when the selector list in a
334 // CSS rule is huge. So we split the elemhide selectors into groups.
335 while (selectors.length > 0)
336 {
337 var selector = selectors.splice(0, SELECTOR_GROUP_SIZE).join(", ");
338 style.sheet.addRule(selector, "display: none !important;");
339 }
340 };
341
342 var updateStylesheet = function(propertyFilters)
kzar 2015/11/30 17:57:32 I deliberately added a typo in the propertFilters
kzar 2015/12/01 10:46:04 OK I think I understand now, this function is only
343 {
344 var selectors = null;
345 var CSSPropertyFiltersLoaded = false;
346
347 var checkLoaded = function()
348 {
349 if (!selectors || !CSSPropertyFiltersLoaded)
350 return;
351
307 if (observer) 352 if (observer)
308 {
309 observer.disconnect(); 353 observer.disconnect();
310 observer = null; 354 observer = null;
311 }
312 355
313 if (style && style.parentElement) 356 if (style && style.parentElement)
314 {
315 style.parentElement.removeChild(style); 357 style.parentElement.removeChild(style);
316 style = null; 358 style = null;
317 }
318 359
319 if (selectors.length > 0) 360 addElemHideSelectors(selectors);
320 { 361 propertyFilters.apply();
321 // Create <style> element lazily, only if we add styles. Add it to 362 };
322 // the shadow DOM if possible. Otherwise fallback to the <head> or
323 // <html> element. If we have injected a style element before that
324 // has been removed (the sheet property is null), create a new one.
325 style = document.createElement("style");
326 (shadow || document.head || document.documentElement).appendChild(style) ;
327 363
328 // It can happen that the frame already navigated to a different 364 ext.backgroundPage.sendMessage({type: "get-selectors"}, function(response)
329 // document while we were waiting for the background page to respond. 365 {
330 // In that case the sheet property will stay null, after adding the 366 selectors = response;
331 // <style> element to the shadow DOM. 367 checkLoaded();
332 if (style.sheet) 368 });
333 {
334 // If using shadow DOM, we have to add the ::content pseudo-element
335 // before each selector, in order to match elements within the
336 // insertion point.
337 if (shadow)
338 selectors = convertSelectorsForShadowDOM(selectors);
339 369
340 // WebKit (and Blink?) apparently chokes when the selector list in a 370 propertyFilters.load(function()
341 // CSS rule is huge. So we split the elemhide selectors into groups. 371 {
342 for (var i = 0; selectors.length > 0; i++) 372 CSSPropertyFiltersLoaded = true;
343 { 373 checkLoaded();
344 var selector = selectors.splice(0, SELECTOR_GROUP_SIZE).join(", ");
345 style.sheet.insertRule(selector + " { display: none !important; }", i);
346 }
347 }
348
349 observer = reinjectRulesWhenRemoved(document, style);
350 }
351 }); 374 });
352 }; 375 };
353 376
354 updateStylesheet(); 377 ext.backgroundPage.sendMessage({type: "get-selectors"}, addElemHideSelectors);
Sebastian Noack 2015/12/10 10:30:49 Any reason we use different logic on initilization
kzar 2015/12/10 15:16:20 No reason that I know of, Done.
355 378
356 document.addEventListener("error", function(event) 379 document.addEventListener("error", function(event)
357 { 380 {
358 checkCollapse(event.target); 381 checkCollapse(event.target);
359 }, true); 382 }, true);
360 383
361 document.addEventListener("load", function(event) 384 document.addEventListener("load", function(event)
362 { 385 {
363 var element = event.target; 386 var element = event.target;
364 387
(...skipping 19 matching lines...) Expand all
384 // Moreover, "load" and "error" events aren't dispatched for elements 407 // Moreover, "load" and "error" events aren't dispatched for elements
385 // in dynamically created frames due to https://crbug.com/442107. 408 // in dynamically created frames due to https://crbug.com/442107.
386 // So we also have to apply element collpasing from the parent frame. 409 // So we also have to apply element collpasing from the parent frame.
387 if (!contentWindow.collapsing) 410 if (!contentWindow.collapsing)
388 [].forEach.call(contentDocument.querySelectorAll(Object.keys(typeMap ).join(",")), checkCollapse); 411 [].forEach.call(contentDocument.querySelectorAll(Object.keys(typeMap ).join(",")), checkCollapse);
389 } 412 }
390 } 413 }
391 } 414 }
392 }, true); 415 }, true);
393 416
394 return updateStylesheet; 417 if (window.document == document)
Sebastian Noack 2015/12/10 10:30:49 Why don't you just put this code below, outside th
kzar 2015/12/10 15:16:20 Done.
418 {
419 window.updateStylesheet = updateStylesheet.bind(
420 null, new CSSPropertyFilters(window, addElemHideSelectors)
421 );
422 }
395 } 423 }
396 424
397 if (document instanceof HTMLDocument) 425 if (document instanceof HTMLDocument)
398 { 426 {
399 checkSitekey(); 427 checkSitekey();
400 window.updateStylesheet = init(document); 428 init(document);
401 } 429 }
OLDNEW
« no previous file with comments | « dependencies ('k') | metadata.common » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld