Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * This Source Code is subject to the terms of the Mozilla Public License | |
3 * version 2.0 (the "License"). You can obtain a copy of the License at | |
4 * http://mozilla.org/MPL/2.0/. | |
5 */ | |
6 | |
7 // ==UserScript== | |
8 // @include * | |
9 // ==/UserScript== | |
10 | |
11 var SELECTOR_GROUP_SIZE = 20; | |
12 | |
13 var elemhideElt = null; | |
14 | |
15 // Sets the currently used CSS rules for elemhide filters | |
16 function setElemhideCSSRules(selectors) | |
17 { | |
18 if (elemhideElt && elemhideElt.parentNode) | |
19 elemhideElt.parentNode.removeChild(elemhideElt); | |
20 | |
21 if (!selectors) | |
22 return; | |
23 | |
24 elemhideElt = document.createElement("style"); | |
25 elemhideElt.setAttribute("type", "text/css"); | |
26 document.documentElement.appendChild(elemhideElt); | |
27 | |
28 var elt = elemhideElt; // Use a local variable to avoid racing conditions | |
29 function setRules() | |
30 { | |
31 if (!elt.sheet) | |
32 { | |
33 // Stylesheet didn't initialize yet, wait a little longer | |
34 window.setTimeout(setRules, 0); | |
35 return; | |
36 } | |
37 | |
38 // WebKit apparently chokes when the selector list in a CSS rule is huge. | |
39 // So we split the elemhide selectors into groups. | |
40 for (var i = 0, j = 0; i < selectors.length; i += SELECTOR_GROUP_SIZE, j++) | |
41 { | |
42 var selector = selectors.slice(i, i + SELECTOR_GROUP_SIZE).join(", "); | |
43 elt.sheet.insertRule(selector + " { display: none !important; }", j); | |
44 } | |
45 } | |
46 setRules(); | |
47 } | |
48 | |
49 function sendRequests() | |
Felix Dahlke
2012/10/10 14:21:42
I'm only reviewing past this point, since everythi
| |
50 { | |
51 // Make sure this is really an HTML page | |
52 if (!(document.documentElement instanceof window.HTMLElement)) | |
53 return; | |
54 | |
55 opera.extension.addEventListener("message", function(event) | |
56 { | |
57 var request = event.data; | |
58 switch (request.reqtype) | |
Felix Dahlke
2012/10/10 14:21:42
I'd prefer an if to this switch, since there's onl
| |
59 { | |
60 case "get-settings-response": | |
61 setElemhideCSSRules(request.selectors); | |
62 break; | |
63 } | |
64 }, false); | |
65 opera.extension.postMessage({reqtype: "get-settings", frameUrl: window.locatio n.href}); | |
66 } | |
67 | |
68 // Document might not be initialized yet | |
69 if (document.documentElement) | |
70 sendRequests(); | |
71 else | |
72 window.setTimeout(sendRequests, 0); | |
OLD | NEW |