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')?.toLowerCase() == 'true' or \
69 element.ownerDocument?.designMode?.toLowerCase() == 'on'
71 getWindowId = (window) ->
72 return window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
73 .getInterface(Components.interfaces.nsIDOMWindowUtils)
77 Cc["@mozilla.org/browser/sessionstore;1"].getService(Ci.nsISessionStore);
79 # Function that returns a URI to the css file that's part of the extension
82 baseURI = Services.io.newURI __SCRIPT_URI_SPEC__, null, null
83 uri = Services.io.newURI "resources/#{ name }.css", null, baseURI
86 # Loads the css identified by the name in the StyleSheetService as User Stylesheet
87 # The stylesheet is then appended to every document, but it can be overwritten by
91 if !_sss.sheetRegistered(uri, _sss.AGENT_SHEET)
92 _sss.loadAndRegisterSheet(uri, _sss.AGENT_SHEET)
95 _sss.unregisterSheet(uri, _sss.AGENT_SHEET)
97 # Simulate mouse click with full chain of event
98 # Copied from Vimium codebase
99 simulateClick = (element, modifiers) ->
100 document = element.ownerDocument
101 window = document.defaultView
104 eventSequence = ["mouseover", "mousedown", "mouseup", "click"]
105 for event in eventSequence
106 mouseEvent = document.createEvent("MouseEvents")
107 mouseEvent.initMouseEvent(event, true, true, window, 1, 0, 0, 0, 0, modifiers.ctrlKey, false, false,
108 modifiers.metaKey, 0, null)
109 # Debugging note: Firefox will not execute the element's default action if we dispatch this click event,
110 # but Webkit will. Dispatching a click on an input box does not seem to focus it; we do that separately
111 element.dispatchEvent(mouseEvent)
113 # Write a string into system clipboard
114 writeToClipboard = (window, text) ->
115 str = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsString);
118 trans = Cc["@mozilla.org/widget/transferable;1"].createInstance(Ci.nsITransferable);
121 privacyContext = window.QueryInterface(Ci.nsIInterfaceRequestor)
122 .getInterface(Ci.nsIWebNavigation)
123 .QueryInterface(Ci.nsILoadContext);
124 trans.init(privacyContext);
126 trans.addDataFlavor("text/unicode");
127 trans.setTransferData("text/unicode", str, text.length * 2);
129 _clip.setData trans, null, Ci.nsIClipboard.kGlobalClipboard
131 # Write a string into system clipboard
132 readFromClipboard = (window) ->
133 trans = Cc["@mozilla.org/widget/transferable;1"].createInstance(Ci.nsITransferable);
136 privacyContext = window.QueryInterface(Ci.nsIInterfaceRequestor)
137 .getInterface(Ci.nsIWebNavigation)
138 .QueryInterface(Ci.nsILoadContext);
139 trans.init(privacyContext);
141 trans.addDataFlavor("text/unicode");
143 _clip.getData trans, Ci.nsIClipboard.kGlobalClipboard
148 trans.getTransferData("text/unicode", str, strLength)
151 str = str.value.QueryInterface(Ci.nsISupportsString);
152 return str.data.substring 0, strLength.value / 2
156 # Executes function `func` and mearues how much time it took
157 timeIt = (func, msg) ->
158 start = new Date().getTime()
160 end = new Date().getTime()
162 console.log msg, end - start
165 # Checks if the string provided matches one of the black list entries
166 # `blackList`: comma/space separated list of URLs with wildcards (* and !)
167 isBlacklisted = (str, blackList) ->
168 for rule in blackList.split(/[\s,]+/)
169 rule = rule.replace(/\*/g, '.*').replace(/\!/g, '.')
170 if str.match new RegExp("^#{ rule }$")
175 # Gets VimFx verions. AddonManager only provides async API to access addon data, so it's a bit tricky...
181 addonId = getPref 'addon_id'
182 Cu.import("resource://gre/modules/AddonManager.jsm", scope);
183 scope.AddonManager.getAddonByID addonId, ((addon) -> version = addon.version)
187 # Simulate smooth scrolling
188 smoothScroll = (window, dx, dy, msecs) ->
189 if msecs <= 0 || !Services.prefs.getBoolPref 'general.smoothScroll'
190 window.scrollBy dx, dy
194 window.scrollBy(_x, _y)
197 l = Math.round(msecs / delta)
199 x = Math.round(dx / l)
200 y = Math.round(dy / l)
205 window.setTimeout fn, l * delta, x, y
207 parseHTML = (document, html) ->
208 parser = Cc["@mozilla.org/parserutils;1"].getService(Ci.nsIParserUtils)
209 flags = parser.SanitizerAllowStyle
210 return parser.parseFragment(html, flags, false, null, document.documentElement)
212 regexpEscape = (s) -> return s and s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')
214 exports.Bucket = Bucket
215 exports.getCurrentTabWindow = getCurrentTabWindow
216 exports.getEventWindow = getEventWindow
217 exports.getEventRootWindow = getEventRootWindow
218 exports.getEventTabBrowser = getEventTabBrowser
220 exports.getWindowId = getWindowId
221 exports.getRootWindow = getRootWindow
222 exports.isElementEditable = isElementEditable
223 exports.getSessionStore = getSessionStore
225 exports.loadCss = loadCss
227 exports.simulateClick = simulateClick
228 exports.smoothScroll = smoothScroll
229 exports.readFromClipboard = readFromClipboard
230 exports.writeToClipboard = writeToClipboard
231 exports.timeIt = timeIt
232 exports.isBlacklisted = isBlacklisted
233 exports.getVersion = getVersion
234 exports.parseHTML = parseHTML
235 exports.regexpEscape = regexpEscape