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 # Returns the `window` from the currently active tab.
33 getCurrentTabWindow = (event) ->
34 if window = getEventWindow event
35 if rootWindow = getRootWindow window
36 return rootWindow.gBrowser.selectedTab.linkedBrowser.contentWindow
38 # Returns the window associated with the event
39 getEventWindow = (event) ->
40 if event.originalTarget instanceof Window
41 return event.originalTarget
43 doc = event.originalTarget.ownerDocument or event.originalTarget
44 if doc instanceof HTMLDocument or doc instanceof XULDocument
45 return doc.defaultView
47 getEventRootWindow = (event) ->
48 if window = getEventWindow event
49 return getRootWindow window
51 getEventTabBrowser = (event) ->
52 cw.gBrowser if cw = getEventRootWindow event
54 getRootWindow = (window) ->
55 return window.QueryInterface(Ci.nsIInterfaceRequestor)
56 .getInterface(Ci.nsIWebNavigation)
57 .QueryInterface(Ci.nsIDocShellTreeItem)
59 .QueryInterface(Ci.nsIInterfaceRequestor)
60 .getInterface(Window);
62 isElementEditable = (element) ->
63 return element.isContentEditable or \
64 element instanceof HTMLInputElement or \
65 element instanceof HTMLTextAreaElement or \
66 element instanceof HTMLSelectElement or \
67 element.getAttribute('g_editable') == 'true' or \
68 element.getAttribute('contenteditable') == 'true'
70 getWindowId = (window) ->
71 return window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
72 .getInterface(Components.interfaces.nsIDOMWindowUtils)
76 Cc["@mozilla.org/browser/sessionstore;1"].getService(Ci.nsISessionStore);
78 # Function that returns a URI to the css file that's part of the extension
81 baseURI = Services.io.newURI __SCRIPT_URI_SPEC__, null, null
82 uri = Services.io.newURI "resources/#{ name }.css", null, baseURI
85 # Loads the css identified by the name in the StyleSheetService as User Stylesheet
86 # The stylesheet is then appended to every document, but it can be overwritten by
90 if !_sss.sheetRegistered(uri, _sss.AGENT_SHEET)
91 _sss.loadAndRegisterSheet(uri, _sss.AGENT_SHEET)
94 _sss.unregisterSheet(uri, _sss.AGENT_SHEET)
96 # Simulate mouse click with full chain of event
97 # Copied from Vimium codebase
98 simulateClick = (element, modifiers) ->
99 document = element.ownerDocument
100 window = document.defaultView
103 eventSequence = ["mouseover", "mousedown", "mouseup", "click"]
104 for event in eventSequence
105 mouseEvent = document.createEvent("MouseEvents")
106 mouseEvent.initMouseEvent(event, true, true, window, 1, 0, 0, 0, 0, modifiers.ctrlKey, false, false,
107 modifiers.metaKey, 0, null)
108 # Debugging note: Firefox will not execute the element's default action if we dispatch this click event,
109 # but Webkit will. Dispatching a click on an input box does not seem to focus it; we do that separately
110 element.dispatchEvent(mouseEvent)
112 # Write a string into system clipboard
113 writeToClipboard = (window, text) ->
114 str = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsString);
117 trans = Cc["@mozilla.org/widget/transferable;1"].createInstance(Ci.nsITransferable);
120 privacyContext = window.QueryInterface(Ci.nsIInterfaceRequestor)
121 .getInterface(Ci.nsIWebNavigation)
122 .QueryInterface(Ci.nsILoadContext);
123 trans.init(privacyContext);
125 trans.addDataFlavor("text/unicode");
126 trans.setTransferData("text/unicode", str, text.length * 2);
128 _clip.setData trans, null, Ci.nsIClipboard.kGlobalClipboard
130 # Write a string into system clipboard
131 readFromClipboard = (window) ->
132 trans = Cc["@mozilla.org/widget/transferable;1"].createInstance(Ci.nsITransferable);
135 privacyContext = window.QueryInterface(Ci.nsIInterfaceRequestor)
136 .getInterface(Ci.nsIWebNavigation)
137 .QueryInterface(Ci.nsILoadContext);
138 trans.init(privacyContext);
140 trans.addDataFlavor("text/unicode");
142 _clip.getData trans, Ci.nsIClipboard.kGlobalClipboard
147 trans.getTransferData("text/unicode", str, strLength)
150 str = str.value.QueryInterface(Ci.nsISupportsString);
151 return str.data.substring 0, strLength.value / 2
155 # Executes function `func` and mearues how much time it took
156 timeIt = (func, msg) ->
157 start = new Date().getTime()
159 end = new Date().getTime()
161 console.log msg, end - start
164 # Checks if the string provided matches one of the black list entries
165 # `blackList`: comma/space separated list of URLs with wildcards (* and !)
166 isBlacklisted = (str, blackList) ->
167 for rule in blackList.split(/[\s,]+/)
168 rule = rule.replace(/\*/g, '.*').replace(/\!/g, '.')
169 if str.match new RegExp("^#{ rule }$")
174 # Gets VimFx verions. AddonManager only provides async API to access addon data, so it's a bit tricky...
180 addonId = getPref 'addon_id'
181 Cu.import("resource://gre/modules/AddonManager.jsm", scope);
182 scope.AddonManager.getAddonByID addonId, ((addon) -> version = addon.version)
186 exports.Bucket = Bucket
187 exports.getCurrentTabWindow = getCurrentTabWindow
188 exports.getEventWindow = getEventWindow
189 exports.getEventRootWindow = getEventRootWindow
190 exports.getEventTabBrowser = getEventTabBrowser
192 exports.getWindowId = getWindowId
193 exports.getRootWindow = getRootWindow
194 exports.isElementEditable = isElementEditable
195 exports.getSessionStore = getSessionStore
197 exports.loadCss = loadCss
199 exports.simulateClick = simulateClick
200 exports.readFromClipboard = readFromClipboard
201 exports.writeToClipboard = writeToClipboard
202 exports.timeIt = timeIt
203 exports.isBlacklisted = isBlacklisted
204 exports.getVersion = getVersion