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

Unified Diff: includes/include.preload.js

Issue 8482159: Initial element hiding functionality (Closed)
Patch Set: Created Oct. 9, 2012, 10:42 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') | « includes/cssFilter.js ('k') | index.html » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: includes/include.preload.js
===================================================================
new file mode 100644
--- /dev/null
+++ b/includes/include.preload.js
@@ -0,0 +1,72 @@
+/*
+ * This Source Code is subject to the terms of the Mozilla Public License
+ * version 2.0 (the "License"). You can obtain a copy of the License at
+ * http://mozilla.org/MPL/2.0/.
+ */
+
+// ==UserScript==
+// @include *
+// ==/UserScript==
+
+var SELECTOR_GROUP_SIZE = 20;
+
+var elemhideElt = null;
+
+// Sets the currently used CSS rules for elemhide filters
+function setElemhideCSSRules(selectors)
+{
+ if (elemhideElt && elemhideElt.parentNode)
+ elemhideElt.parentNode.removeChild(elemhideElt);
+
+ if (!selectors)
+ return;
+
+ elemhideElt = document.createElement("style");
+ elemhideElt.setAttribute("type", "text/css");
+ document.documentElement.appendChild(elemhideElt);
+
+ var elt = elemhideElt; // Use a local variable to avoid racing conditions
+ function setRules()
+ {
+ if (!elt.sheet)
+ {
+ // Stylesheet didn't initialize yet, wait a little longer
+ window.setTimeout(setRules, 0);
+ return;
+ }
+
+ // WebKit apparently chokes when the selector list in a CSS rule is huge.
+ // So we split the elemhide selectors into groups.
+ for (var i = 0, j = 0; i < selectors.length; i += SELECTOR_GROUP_SIZE, j++)
+ {
+ var selector = selectors.slice(i, i + SELECTOR_GROUP_SIZE).join(", ");
+ elt.sheet.insertRule(selector + " { display: none !important; }", j);
+ }
+ }
+ setRules();
+}
+
+function sendRequests()
Felix Dahlke 2012/10/10 14:21:42 I'm only reviewing past this point, since everythi
+{
+ // Make sure this is really an HTML page
+ if (!(document.documentElement instanceof window.HTMLElement))
+ return;
+
+ opera.extension.addEventListener("message", function(event)
+ {
+ var request = event.data;
+ switch (request.reqtype)
Felix Dahlke 2012/10/10 14:21:42 I'd prefer an if to this switch, since there's onl
+ {
+ case "get-settings-response":
+ setElemhideCSSRules(request.selectors);
+ break;
+ }
+ }, false);
+ opera.extension.postMessage({reqtype: "get-settings", frameUrl: window.location.href});
+}
+
+// Document might not be initialized yet
+if (document.documentElement)
+ sendRequests();
+else
+ window.setTimeout(sendRequests, 0);
« background.js ('K') | « includes/cssFilter.js ('k') | index.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld