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

Side by Side Diff: include.preload.js

Issue 5989914281771008: Use Shadow DOM for element hiding (Closed)
Patch Set: Fixed Shadow DOM usage and simplified code Created April 15, 2014, 7:06 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 | « background.js ('k') | no next file » | 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
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 var SELECTOR_GROUP_SIZE = 20; 18 var SELECTOR_GROUP_SIZE = 20;
19 19
20 var elemhideElt = null;
21
22 // Sets the currently used CSS rules for elemhide filters 20 // Sets the currently used CSS rules for elemhide filters
23 function setElemhideCSSRules(selectors) 21 function setElemhideCSSRules(selectors)
24 { 22 {
25 if (elemhideElt && elemhideElt.parentNode) 23 if (selectors.length == 0)
26 elemhideElt.parentNode.removeChild(elemhideElt);
27
28 if (!selectors)
29 return; 24 return;
30 25
31 elemhideElt = document.createElement("style"); 26 var style = document.createElement("style");
32 elemhideElt.setAttribute("type", "text/css"); 27 style.setAttribute("type", "text/css");
33 28
34 // Try to insert the style into the <head> tag, inserting directly under the 29 // Use Shadow DOM if available to don't mess with web pages
35 // document root breaks dev tools functionality: 30 // that rely on the order of their own <style> tags (#309)
36 // http://code.google.com/p/chromium/issues/detail?id=178109 31 if ("webkitCreateShadowRoot" in document.documentElement)
37 (document.head || document.documentElement).appendChild(elemhideElt); 32 {
33 var shadow = document.documentElement.webkitCreateShadowRoot();
34 shadow.appendChild(document.createElement("shadow"));
35 shadow.appendChild(style);
38 36
39 var elt = elemhideElt; // Use a local variable to avoid racing conditions 37 for (var i = 0; i < selectors.length; i++)
40 function setRules() 38 selectors[i] = "::-webkit-distributed(" + selectors[i] + ")";
39 }
40 else
41 { 41 {
42 if (!elt.sheet) 42 // Try to insert the style into the <head> tag, inserting directly under the
43 { 43 // document root breaks dev tools functionality:
44 // Stylesheet didn't initialize yet, wait a little longer 44 // http://code.google.com/p/chromium/issues/detail?id=178109
45 window.setTimeout(setRules, 0); 45 (document.head || document.documentElement).appendChild(style);
46 return; 46 }
47 }
48 47
49 // WebKit apparently chokes when the selector list in a CSS rule is huge. 48 // WebKit apparently chokes when the selector list in a CSS rule is huge.
50 // So we split the elemhide selectors into groups. 49 // So we split the elemhide selectors into groups.
51 for (var i = 0, j = 0; i < selectors.length; i += SELECTOR_GROUP_SIZE, j++) 50 for (var i = 0; selectors.length > 0; i++)
52 { 51 {
53 var selector = selectors.slice(i, i + SELECTOR_GROUP_SIZE).join(", "); 52 var selector = selectors.splice(0, SELECTOR_GROUP_SIZE).join(", ");
54 elt.sheet.insertRule(selector + " { display: none !important; }", j); 53 style.sheet.insertRule(selector + " { display: none !important; }", i);
55 }
56 } 54 }
57 setRules();
58 } 55 }
59 56
60 var typeMap = { 57 var typeMap = {
61 "img": "IMAGE", 58 "img": "IMAGE",
62 "input": "IMAGE", 59 "input": "IMAGE",
63 "audio": "MEDIA", 60 "audio": "MEDIA",
64 "video": "MEDIA", 61 "video": "MEDIA",
65 "frame": "SUBDOCUMENT", 62 "frame": "SUBDOCUMENT",
66 "iframe": "SUBDOCUMENT" 63 "iframe": "SUBDOCUMENT"
67 }; 64 };
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
114 ext.backgroundPage.sendMessage({type: "add-key-exception", token: attr}); 111 ext.backgroundPage.sendMessage({type: "add-key-exception", token: attr});
115 112
116 ext.backgroundPage.sendMessage({type: "get-selectors"}, setElemhideCSSRules); 113 ext.backgroundPage.sendMessage({type: "get-selectors"}, setElemhideCSSRules);
117 } 114 }
118 115
119 // In Chrome 18 the document might not be initialized yet 116 // In Chrome 18 the document might not be initialized yet
120 if (document.documentElement) 117 if (document.documentElement)
121 init(); 118 init();
122 else 119 else
123 window.setTimeout(init, 0); 120 window.setTimeout(init, 0);
OLDNEW
« no previous file with comments | « background.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld