]> git.donarmstrong.com Git - dactyl.git/blob - common/content/disable-acr.jsm
Import r6948 from upstream hg supporting Firefox up to 24.*
[dactyl.git] / common / content / disable-acr.jsm
1 // By Kris Maglione. Public Domain.
2 // Please feel free to copy and use at will.
3
4 var ADDON_ID;
5
6 const OVERLAY_URLS = [
7     "about:addons",
8     "chrome://mozapps/content/extensions/extensions.xul"
9 ];
10
11 let { interfaces: Ci, utils: Cu } = Components;
12
13 Cu.import("resource://gre/modules/Services.jsm");
14 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
15
16 const TOPIC = "chrome-document-global-created";
17
18 function observe(window, topic, url) {
19     if (topic === TOPIC)
20         checkDocument(window.document);
21 }
22 function init(id) {
23     if (id)
24         ADDON_ID = id;
25
26     Services.obs[id ? "addObserver" : "removeObserver"](observe, TOPIC, false);
27     for (let doc in chromeDocuments())
28         checkDocument(doc, !id);
29 }
30 function cleanup() { init(null); }
31
32 function checkPopup(event) {
33     let doc = event.originalTarget.ownerDocument;
34     let binding = doc.getBindingParent(event.originalTarget);
35     if (binding && binding.addon && binding.addon.guid == ADDON_ID && !binding.addon.compatible) {
36         let elem = doc.getAnonymousElementByAttribute(binding, "anonid", "stillworks");
37         if (elem && elem.nextSibling) {
38             elem.nextSibling.disabled = true;
39             elem.nextSibling.setAttribute("tooltiptext", "Developer has opted out of incompatibility reports\n"+
40                                                          "Development versions are available with updated support");
41         }
42     }
43 }
44
45 function checkDocument(doc, disable, force) {
46     if (["interactive", "complete"].indexOf(doc.readyState) >= 0 || force && doc.readyState === "uninitialized") {
47         if (OVERLAY_URLS.indexOf(doc.documentURI) >= 0)
48             doc[disable ? "removeEventListener" : "addEventListener"]("popupshowing", checkPopup, false);
49     }
50     else {
51         doc.addEventListener("DOMContentLoaded", function listener() {
52             doc.removeEventListener("DOMContentLoaded", listener, false);
53             checkDocument(doc, disable, true);
54         }, false);
55     }
56 }
57
58 function chromeDocuments() {
59     let windows = Services.wm.getXULWindowEnumerator(null);
60     while (windows.hasMoreElements()) {
61         let window = windows.getNext().QueryInterface(Ci.nsIXULWindow);
62         for each (let type in ["typeChrome", "typeContent"]) {
63             let docShells = window.docShell.getDocShellEnumerator(Ci.nsIDocShellTreeItem[type],
64                                                                   Ci.nsIDocShell.ENUMERATE_FORWARDS);
65             while (docShells.hasMoreElements())
66                 try {
67                     yield docShells.getNext().QueryInterface(Ci.nsIDocShell).contentViewer.DOMDocument;
68                 }
69                 catch (e) {}
70         }
71     }
72 }
73
74 var EXPORTED_SYMBOLS = ["cleanup", "init"];
75
76 // vim: set fdm=marker sw=4 sts=4 ts=8 et ft=javascript: