OLD | NEW |
1 /* | 1 /* |
2 * This file is part of the Adblock Plus extension, | 2 * This file is part of the Adblock Plus extension, |
3 * Copyright (C) 2006-2012 Eyeo GmbH | 3 * Copyright (C) 2006-2012 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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 // So we split the elemhide selectors into groups. | 46 // So we split the elemhide selectors into groups. |
47 for (var i = 0, j = 0; i < selectors.length; i += SELECTOR_GROUP_SIZE, j++) | 47 for (var i = 0, j = 0; i < selectors.length; i += SELECTOR_GROUP_SIZE, j++) |
48 { | 48 { |
49 var selector = selectors.slice(i, i + SELECTOR_GROUP_SIZE).join(", "); | 49 var selector = selectors.slice(i, i + SELECTOR_GROUP_SIZE).join(", "); |
50 elt.sheet.insertRule(selector + " { display: none !important; }", j); | 50 elt.sheet.insertRule(selector + " { display: none !important; }", j); |
51 } | 51 } |
52 } | 52 } |
53 setRules(); | 53 setRules(); |
54 } | 54 } |
55 | 55 |
56 function removeElements(tagName, urls) | 56 function checkCollapse(event) |
57 { | 57 { |
58 var remove = []; | 58 var target = event.target; |
59 var elements = document.getElementsByTagName(tagName); | 59 if ((event.type == "error" && target instanceof HTMLImageElement) || |
60 for (var i = 0, l = elements.length; i < l; i++) | 60 (event.type == "load" && target instanceof HTMLIFrameElement)) |
61 if (elements[i].src in urls) | 61 { |
62 remove.push(elements[i]); | 62 // This element failed loading, did we block it? |
| 63 var url = target.src; |
| 64 if (!url) |
| 65 return; |
63 | 66 |
64 for (var i = 0, l = remove.length; i < l; i++) | 67 var type = (target instanceof HTMLImageElement ? "IMAGE": "SUBDOCUMENT"); |
65 if (remove[i].parentNode) | 68 chrome.extension.sendRequest({reqtype: "should-collapse", url: url, document
Url: document.URL, type: type}, function(response) |
66 remove[i].parentNode.removeChild(remove[i]); | 69 { |
67 | 70 if (response && target.parentNode) |
68 return remove.length > 0; | 71 target.parentNode.removeChild(target); |
| 72 }); |
| 73 } |
69 } | 74 } |
70 | 75 |
71 var removeMap = | |
72 { | |
73 IMAGE: { | |
74 tag: "img", | |
75 remove: {}, | |
76 loadHandler: false | |
77 }, | |
78 SUBDOCUMENT: { | |
79 tag: "iframe", | |
80 remove: {}, | |
81 loadHandler: false | |
82 } | |
83 }; | |
84 removeMap.IMAGE.handler = removeElements.bind(null, removeMap.IMAGE.tag, removeM
ap.IMAGE.remove); | |
85 removeMap.SUBDOCUMENT.handler = removeElements.bind(null, removeMap.SUBDOCUMENT.
tag, removeMap.SUBDOCUMENT.remove); | |
86 | |
87 function sendRequests() | 76 function sendRequests() |
88 { | 77 { |
89 // Make sure this is really an HTML page, as Chrome runs these scripts on just
about everything | 78 // Make sure this is really an HTML page, as Chrome runs these scripts on just
about everything |
90 if (!(document.documentElement instanceof HTMLElement)) | 79 if (!(document.documentElement instanceof HTMLElement)) |
91 return; | 80 return; |
92 | 81 |
93 chrome.extension.onMessage.addListener(function(request, sender, sendResponse) | |
94 { | |
95 switch (request.reqtype) | |
96 { | |
97 case "hide-element": | |
98 if (request.documentUrl != document.URL) | |
99 return; | |
100 | |
101 // We have little way of knowing which element was blocked - see | |
102 // http://code.google.com/p/chromium/issues/detail?id=97392. Have to | |
103 // look through all of them and try to find the right one. And if we | |
104 // don't find it then maybe it just wasn't added yet. | |
105 var info = removeMap[request.type]; | |
106 info.remove[request.url] = true; | |
107 if (info.handler()) | |
108 delete info.remove[request.url]; | |
109 else if (!info.onLoadHandler) | |
110 { | |
111 window.addEventListener("DOMContentLoaded", info.handler, false); | |
112 info.onLoadHandler = true; | |
113 } | |
114 } | |
115 }); | |
116 | |
117 chrome.extension.sendRequest({reqtype: "get-settings", selectors: true, frameU
rl: window.location.href}, function(response) | 82 chrome.extension.sendRequest({reqtype: "get-settings", selectors: true, frameU
rl: window.location.href}, function(response) |
118 { | 83 { |
119 setElemhideCSSRules(response.selectors); | 84 setElemhideCSSRules(response.selectors); |
120 }); | 85 }); |
121 } | 86 } |
122 | 87 |
| 88 document.addEventListener("error", checkCollapse, true); |
| 89 document.addEventListener("load", checkCollapse, true); |
| 90 |
123 // In Chrome 18 the document might not be initialized yet | 91 // In Chrome 18 the document might not be initialized yet |
124 if (document.documentElement) | 92 if (document.documentElement) |
125 sendRequests(); | 93 sendRequests(); |
126 else | 94 else |
127 window.setTimeout(sendRequests, 0); | 95 window.setTimeout(sendRequests, 0); |
OLD | NEW |