1 { interfaces: Ci, classes: Cc, utils: Cu } = Components
3 HTMLInputElement = Ci.nsIDOMHTMLInputElement
4 HTMLTextAreaElement = Ci.nsIDOMHTMLTextAreaElement
5 HTMLSelectElement = Ci.nsIDOMHTMLSelectElement
6 XULDocument = Ci.nsIDOMXULDocument
7 XULElement = Ci.nsIDOMXULElement
8 HTMLDocument = Ci.nsIDOMHTMLDocument
9 HTMLElement = Ci.nsIDOMHTMLElement
10 Window = Ci.nsIDOMWindow
11 ChromeWindow = Ci.nsIDOMChromeWindow
13 _sss = Cc["@mozilla.org/content/style-sheet-service;1"].getService(Ci.nsIStyleSheetService)
14 _clip = Cc["@mozilla.org/widget/clipboard;1"].getService(Ci.nsIClipboard)
16 { getPref } = require 'prefs'
19 constructor: (@idFunc, @newFunc) ->
24 if container = @bucket[id]
27 return @bucket[id] = @newFunc obj
30 delete @bucket[id] if id = @idFunc obj
32 isRootWindow = (window) ->
33 window.location == "chrome://browser/content/browser.xul"
35 # Returns the `window` from the currently active tab.
36 getCurrentTabWindow = (event) ->
37 if window = getEventWindow event
38 if rootWindow = getRootWindow window
39 return rootWindow.gBrowser.selectedTab.linkedBrowser.contentWindow
41 # Returns the window associated with the event
42 getEventWindow = (event) ->
43 if event.originalTarget instanceof Window
44 return event.originalTarget
46 doc = event.originalTarget.ownerDocument or event.originalTarget
47 if doc instanceof HTMLDocument or doc instanceof XULDocument
48 return doc.defaultView
50 getEventRootWindow = (event) ->
51 if window = getEventWindow event
52 return getRootWindow window
54 getEventTabBrowser = (event) ->
55 cw.gBrowser if cw = getEventRootWindow event
57 getRootWindow = (window) ->
58 return window.QueryInterface(Ci.nsIInterfaceRequestor)
59 .getInterface(Ci.nsIWebNavigation)
60 .QueryInterface(Ci.nsIDocShellTreeItem)
62 .QueryInterface(Ci.nsIInterfaceRequestor)
63 .getInterface(Window);
65 isElementEditable = (element) ->
66 return element.isContentEditable or \
67 element instanceof HTMLInputElement or \
68 element instanceof HTMLTextAreaElement or \
69 element instanceof HTMLSelectElement or \
70 element.getAttribute('g_editable') == 'true' or \
71 element.getAttribute('contenteditable') == 'true'
73 getWindowId = (window) ->
74 return window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
75 .getInterface(Components.interfaces.nsIDOMWindowUtils)
79 Cc["@mozilla.org/browser/sessionstore;1"].getService(Ci.nsISessionStore);
81 # Function that returns a URI to the css file that's part of the extension
84 baseURI = Services.io.newURI __SCRIPT_URI_SPEC__, null, null
85 uri = Services.io.newURI "resources/#{ name }.css", null, baseURI
88 # Loads the css identified by the name in the StyleSheetService as User Stylesheet
89 # The stylesheet is then appended to every document, but it can be overwritten by
93 if !_sss.sheetRegistered(uri, _sss.AGENT_SHEET)
94 _sss.loadAndRegisterSheet(uri, _sss.AGENT_SHEET)
97 _sss.unregisterSheet(uri, _sss.AGENT_SHEET)
99 # Simulate mouse click with full chain of event
100 # Copied from Vimium codebase
101 simulateClick = (element, modifiers) ->
102 document = element.ownerDocument
103 window = document.defaultView
106 eventSequence = ["mouseover", "mousedown", "mouseup", "click"]
107 for event in eventSequence
108 mouseEvent = document.createEvent("MouseEvents")
109 mouseEvent.initMouseEvent(event, true, true, window, 1, 0, 0, 0, 0, modifiers.ctrlKey, false, false,
110 modifiers.metaKey, 0, null)
111 # Debugging note: Firefox will not execute the element's default action if we dispatch this click event,
112 # but Webkit will. Dispatching a click on an input box does not seem to focus it; we do that separately
113 element.dispatchEvent(mouseEvent)
115 # Write a string into system clipboard
116 writeToClipboard = (window, text) ->
117 str = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsString);
120 trans = Cc["@mozilla.org/widget/transferable;1"].createInstance(Ci.nsITransferable);
123 privacyContext = window.QueryInterface(Ci.nsIInterfaceRequestor)
124 .getInterface(Ci.nsIWebNavigation)
125 .QueryInterface(Ci.nsILoadContext);
126 trans.init(privacyContext);
128 trans.addDataFlavor("text/unicode");
129 trans.setTransferData("text/unicode", str, text.length * 2);
131 _clip.setData trans, null, Ci.nsIClipboard.kGlobalClipboard
133 # Write a string into system clipboard
134 readFromClipboard = (window) ->
135 trans = Cc["@mozilla.org/widget/transferable;1"].createInstance(Ci.nsITransferable);
138 privacyContext = window.QueryInterface(Ci.nsIInterfaceRequestor)
139 .getInterface(Ci.nsIWebNavigation)
140 .QueryInterface(Ci.nsILoadContext);
141 trans.init(privacyContext);
143 trans.addDataFlavor("text/unicode");
145 _clip.getData trans, Ci.nsIClipboard.kGlobalClipboard
150 trans.getTransferData("text/unicode", str, strLength)
153 str = str.value.QueryInterface(Ci.nsISupportsString);
154 return str.data.substring 0, strLength.value / 2
158 # Executes function `func` and mearues how much time it took
159 timeIt = (func, msg) ->
160 start = new Date().getTime()
162 end = new Date().getTime()
164 console.log msg, end - start
167 # Checks if the string provided matches one of the black list entries
168 # `blackList`: comma/space separated list of URLs with wildcards (* and !)
169 isBlacklisted = (str, blackList) ->
170 for rule in blackList.split(/[\s,]+/)
171 rule = rule.replace(/\*/g, '.*').replace(/\!/g, '.')
172 if str.match new RegExp("^#{ rule }$")
177 # Gets VimFx verions. AddonManager only provides async API to access addon data, so it's a bit tricky...
183 addonId = getPref 'addon_id'
184 Cu.import("resource://gre/modules/AddonManager.jsm", scope);
185 scope.AddonManager.getAddonByID addonId, ((addon) -> version = addon.version)
189 exports.Bucket = Bucket
190 exports.isRootWindow = isRootWindow
191 exports.getCurrentTabWindow = getCurrentTabWindow
192 exports.getEventWindow = getEventWindow
193 exports.getEventRootWindow = getEventRootWindow
194 exports.getEventTabBrowser = getEventTabBrowser
196 exports.getWindowId = getWindowId
197 exports.getRootWindow = getRootWindow
198 exports.isElementEditable = isElementEditable
199 exports.getSessionStore = getSessionStore
201 exports.loadCss = loadCss
203 exports.simulateClick = simulateClick
204 exports.readFromClipboard = readFromClipboard
205 exports.writeToClipboard = writeToClipboard
206 exports.timeIt = timeIt
207 exports.isBlacklisted = isBlacklisted
208 exports.getVersion = getVersion