1 { interfaces: Ci, classes: Cc } = 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
12 KeyboardEvent = Ci.nsIDOMKeyEvent
14 _sss = Cc["@mozilla.org/content/style-sheet-service;1"].getService(Ci.nsIStyleSheetService)
15 _clip = Cc["@mozilla.org/widget/clipboard;1"].getService(Ci.nsIClipboard)
17 { getPref } = require 'prefs'
20 constructor: (@idFunc, @newFunc) ->
25 if container = @bucket[id]
28 return @bucket[id] = @newFunc obj
31 delete @bucket[id] if id = @idFunc obj
33 isRootWindow = (window) ->
34 window.location == "chrome://browser/content/browser.xul"
36 # Returns the `window` from the currently active tab.
37 getCurrentTabWindow = (event) ->
38 if window = getEventWindow event
39 if rootWindow = getRootWindow window
40 return rootWindow.gBrowser.selectedTab.linkedBrowser.contentWindow
42 # Returns the window associated with the event
43 getEventWindow = (event) ->
44 if event.originalTarget instanceof Window
45 return event.originalTarget
47 doc = event.originalTarget.ownerDocument or event.originalTarget
48 if doc instanceof HTMLDocument or doc instanceof XULDocument
49 return doc.defaultView
51 getEventRootWindow = (event) ->
52 if window = getEventWindow event
53 return getRootWindow window
55 getEventTabBrowser = (event) ->
56 cw.gBrowser if cw = getEventRootWindow event
58 getRootWindow = (window) ->
59 return window.QueryInterface(Ci.nsIInterfaceRequestor)
60 .getInterface(Ci.nsIWebNavigation)
61 .QueryInterface(Ci.nsIDocShellTreeItem)
63 .QueryInterface(Ci.nsIInterfaceRequestor)
64 .getInterface(Window);
66 isElementEditable = (element) ->
67 return element.isContentEditable or \
68 element instanceof HTMLInputElement or \
69 element instanceof HTMLTextAreaElement or \
70 element instanceof HTMLSelectElement
72 getWindowId = (window) ->
73 return window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
74 .getInterface(Components.interfaces.nsIDOMWindowUtils)
78 Cc["@mozilla.org/browser/sessionstore;1"].getService(Ci.nsISessionStore);
80 # Function that returns a URI to the css file that's part of the extension
83 baseURI = Services.io.newURI __SCRIPT_URI_SPEC__, null, null
84 uri = Services.io.newURI "resources/#{ name }.css", null, baseURI
87 # Loads the css identified by the name in the StyleSheetService as User Stylesheet
88 # The stylesheet is then appended to every document, but it can be overwritten by
92 _sss.loadAndRegisterSheet(uri, _sss.AGENT_SHEET)
95 _sss.unregisterSheet(uri, _sss.AGENT_SHEET)
97 # Processes the keyboard event and extracts string representation
98 # of the key *without modifiers*.
99 # Only processes that can be handled by the extension
101 # Currently we handle letters, Escape and Tab keys
102 keyboardEventChar = (keyboardEvent) ->
104 # Ignore the key if Meta of Alt are pressed
105 if keyboardEvent.metaKey or keyboardEvent.altKey
108 key = keyboardEvent.keyCode
109 char = String.fromCharCode(key)
110 HINTCHARS = getPref 'hint_chars'
112 if key >= KeyboardEvent.DOM_VK_A and key <= KeyboardEvent.DOM_VK_Z
113 if keyboardEvent.shiftKey
114 return char.toUpperCase()
116 return char.toLowerCase()
117 # Allow additional chars from the hint chars list
118 else if HINTCHARS.search(char) > -1
121 switch keyboardEvent.keyCode
122 when KeyboardEvent.DOM_VK_ESCAPE
124 when KeyboardEvent.DOM_VK_BACK_SPACE
127 # Extracts string representation of the KeyboardEvent and adds
128 # relevant modifiers (_ctrl_, _alt_, _meta_) in case they were pressed
129 keyboardEventKeyString = (keyboardEvent) ->
130 char = keyboardEventChar keyboardEvent
132 if char and keyboardEvent.ctrlKey
137 # Simulate mouse click with full chain of event
138 # Copied from Vimium codebase
139 simulateClick = (element, modifiers) ->
140 document = element.ownerDocument
141 window = document.defaultView
144 eventSequence = ["mouseover", "mousedown", "mouseup", "click"]
145 for event in eventSequence
146 mouseEvent = document.createEvent("MouseEvents")
147 mouseEvent.initMouseEvent(event, true, true, window, 1, 0, 0, 0, 0, modifiers.ctrlKey, false, false,
148 modifiers.metaKey, 0, null)
149 # Debugging note: Firefox will not execute the element's default action if we dispatch this click event,
150 # but Webkit will. Dispatching a click on an input box does not seem to focus it; we do that separately
151 element.dispatchEvent(mouseEvent)
153 # Write a string into system clipboard
154 writeToClipboard = (text) ->
155 str = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsString);
158 trans = Cc["@mozilla.org/widget/transferable;1"].createInstance(Ci.nsITransferable);
159 trans.addDataFlavor("text/unicode");
160 trans.setTransferData("text/unicode", str, text.length * 2);
162 _clip.setData trans, null, Ci.nsIClipboard.kGlobalClipboard
164 # Write a string into system clipboard
165 readFromClipboard = () ->
166 trans = Cc["@mozilla.org/widget/transferable;1"].createInstance(Ci.nsITransferable);
167 trans.addDataFlavor("text/unicode");
169 _clip.getData trans, Ci.nsIClipboard.kGlobalClipboard
174 trans.getTransferData("text/unicode", str, strLength)
177 str = str.value.QueryInterface(Ci.nsISupportsString);
178 return str.data.substring 0, strLength.value / 2
182 # Executes function `func` and mearues how much time it took
183 timeIt = (func, msg) ->
184 start = new Date().getTime()
186 end = new Date().getTime()
188 console.log msg, end - start
191 exports.Bucket = Bucket
192 exports.isRootWindow = isRootWindow
193 exports.getCurrentTabWindow = getCurrentTabWindow
194 exports.getEventWindow = getEventWindow
195 exports.getEventRootWindow = getEventRootWindow
196 exports.getEventTabBrowser = getEventTabBrowser
198 exports.getWindowId = getWindowId
199 exports.getRootWindow = getRootWindow
200 exports.isElementEditable = isElementEditable
201 exports.getSessionStore = getSessionStore
203 exports.loadCss = loadCss
205 exports.keyboardEventKeyString = keyboardEventKeyString
206 exports.simulateClick = simulateClick
207 exports.readFromClipboard = readFromClipboard
208 exports.writeToClipboard = writeToClipboard
209 exports.timeIt = timeIt