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

Unified Diff: include.preload.js

Issue 8741005: Topic 11337 - More reliable approach to hide image/frame placeholders (Closed)
Patch Set: Created Oct. 31, 2012, 9:14 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« background.js ('K') | « background.js ('k') | webrequest.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: include.preload.js
===================================================================
--- a/include.preload.js
+++ b/include.preload.js
@@ -48,80 +48,48 @@ function setElemhideCSSRules(selectors)
{
var selector = selectors.slice(i, i + SELECTOR_GROUP_SIZE).join(", ");
elt.sheet.insertRule(selector + " { display: none !important; }", j);
}
}
setRules();
}
-function removeElements(tagName, urls)
+function checkCollapse(event)
{
- var remove = [];
- var elements = document.getElementsByTagName(tagName);
- for (var i = 0, l = elements.length; i < l; i++)
- if (elements[i].src in urls)
- remove.push(elements[i]);
+ var target = event.target;
+ if ((event.type == "error" && target instanceof HTMLImageElement) ||
+ (event.type == "load" && target instanceof HTMLIFrameElement))
+ {
+ // This element failed loading, did we block it?
+ var url = target.src;
+ if (!url)
+ return;
- for (var i = 0, l = remove.length; i < l; i++)
- if (remove[i].parentNode)
- remove[i].parentNode.removeChild(remove[i]);
-
- return remove.length > 0;
+ var type = (target instanceof HTMLImageElement ? "IMAGE": "SUBDOCUMENT");
+ chrome.extension.sendRequest({reqtype: "should-collapse", url: url, documentUrl: document.URL, type: type}, function(response)
+ {
+ if (response && target.parentNode)
+ target.parentNode.removeChild(target);
+ });
+ }
}
-var removeMap =
-{
- IMAGE: {
- tag: "img",
- remove: {},
- loadHandler: false
- },
- SUBDOCUMENT: {
- tag: "iframe",
- remove: {},
- loadHandler: false
- }
-};
-removeMap.IMAGE.handler = removeElements.bind(null, removeMap.IMAGE.tag, removeMap.IMAGE.remove);
-removeMap.SUBDOCUMENT.handler = removeElements.bind(null, removeMap.SUBDOCUMENT.tag, removeMap.SUBDOCUMENT.remove);
-
function sendRequests()
{
// Make sure this is really an HTML page, as Chrome runs these scripts on just about everything
if (!(document.documentElement instanceof HTMLElement))
return;
- chrome.extension.onMessage.addListener(function(request, sender, sendResponse)
- {
- switch (request.reqtype)
- {
- case "hide-element":
- if (request.documentUrl != document.URL)
- return;
-
- // We have little way of knowing which element was blocked - see
- // http://code.google.com/p/chromium/issues/detail?id=97392. Have to
- // look through all of them and try to find the right one. And if we
- // don't find it then maybe it just wasn't added yet.
- var info = removeMap[request.type];
- info.remove[request.url] = true;
- if (info.handler())
- delete info.remove[request.url];
- else if (!info.onLoadHandler)
- {
- window.addEventListener("DOMContentLoaded", info.handler, false);
- info.onLoadHandler = true;
- }
- }
- });
-
chrome.extension.sendRequest({reqtype: "get-settings", selectors: true, frameUrl: window.location.href}, function(response)
{
setElemhideCSSRules(response.selectors);
});
}
+document.addEventListener("error", checkCollapse, true);
+document.addEventListener("load", checkCollapse, true);
+
// In Chrome 18 the document might not be initialized yet
if (document.documentElement)
sendRequests();
else
window.setTimeout(sendRequests, 0);
« background.js ('K') | « background.js ('k') | webrequest.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld