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

Side by Side Diff: include.preload.js

Issue 8678083: Topic 11337: Image placeholders aren`t always hidden (Closed)
Patch Set: Created Oct. 28, 2012, 4:48 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 | 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 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
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)
57 {
58 var remove = [];
59 var elements = document.getElementsByTagName(tagName);
60 for (var i = 0, l = elements.length; i < l; i++)
61 if (elements[i].src in urls)
62 remove.push(elements[i]);
63
64 for (var i = 0, l = remove.length; i < l; i++)
65 if (remove[i].parentNode)
66 remove[i].parentNode.removeChild(remove[i]);
67
68 return remove.length > 0;
69 }
70
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
56 function sendRequests() 87 function sendRequests()
57 { 88 {
58 // Make sure this is really an HTML page, as Chrome runs these scripts on just about everything 89 // Make sure this is really an HTML page, as Chrome runs these scripts on just about everything
59 if (!(document.documentElement instanceof HTMLElement)) 90 if (!(document.documentElement instanceof HTMLElement))
60 return; 91 return;
61 92
62 chrome.extension.onMessage.addListener(function(request, sender, sendResponse) 93 chrome.extension.onMessage.addListener(function(request, sender, sendResponse)
63 { 94 {
64 switch (request.reqtype) 95 switch (request.reqtype)
65 { 96 {
66 case "hide-element": 97 case "hide-element":
67 if (request.documentUrl != document.URL) 98 if (request.documentUrl != document.URL)
68 return; 99 return;
69 100
70 // We have little way of knowing which element was blocked - see 101 // We have little way of knowing which element was blocked - see
71 // http://code.google.com/p/chromium/issues/detail?id=97392. Have to 102 // http://code.google.com/p/chromium/issues/detail?id=97392. Have to
72 // look through all of them and try to find the right one. 103 // look through all of them and try to find the right one. And if we
73 var remove = []; 104 // don't find it then maybe it just wasn't added yet.
74 var elements = (request.type == "IMAGE" ? document.images : document.get ElementsByTagName("iframe")); 105 var info = removeMap[request.type];
75 for (var i = 0, l = elements.length; i < l; i++) 106 info.remove[request.url] = true;
76 if (elements[i].src == request.url) 107 if (info.handler())
77 remove.push(elements[i]); 108 delete info.remove[request.url];
78 109 else if (!info.onLoadHandler)
79 for (var i = 0, l = remove.length; i < l; i++) 110 {
80 if (remove[i].parentNode) 111 window.addEventListener("DOMContentLoaded", info.handler, false);
81 remove[i].parentNode.removeChild(remove[i]); 112 info.onLoadHandler = true;
113 }
82 } 114 }
83 }); 115 });
84 116
85 chrome.extension.sendRequest({reqtype: "get-settings", selectors: true, frameU rl: window.location.href}, function(response) 117 chrome.extension.sendRequest({reqtype: "get-settings", selectors: true, frameU rl: window.location.href}, function(response)
86 { 118 {
87 setElemhideCSSRules(response.selectors); 119 setElemhideCSSRules(response.selectors);
88 }); 120 });
89 } 121 }
90 122
91 // In Chrome 18 the document might not be initialized yet 123 // In Chrome 18 the document might not be initialized yet
92 if (document.documentElement) 124 if (document.documentElement)
93 sendRequests(); 125 sendRequests();
94 else 126 else
95 window.setTimeout(sendRequests, 0); 127 window.setTimeout(sendRequests, 0);
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld