]> git.gir.st - VimFx.git/blob - packages/utils.coffee
Merge branch 'hints-and-markers' into develop
[VimFx.git] / packages / utils.coffee
1 { WindowTracker, isBrowserWindow } = require 'window-utils'
2
3 { interfaces: Ci, classes: Cc } = Components
4
5 HTMLInputElement = Ci.nsIDOMHTMLInputElement
6 HTMLTextAreaElement = Ci.nsIDOMHTMLTextAreaElement
7 HTMLSelectElement = Ci.nsIDOMHTMLSelectElement
8 XULDocument = Ci.nsIDOMXULDocument
9 XULElement = Ci.nsIDOMXULElement
10 HTMLDocument = Ci.nsIDOMHTMLDocument
11 HTMLElement = Ci.nsIDOMHTMLElement
12 Window = Ci.nsIDOMWindow
13 ChromeWindow = Ci.nsIDOMChromeWindow
14 KeyboardEvent = Ci.nsIDOMKeyEvent
15
16 _sss = Cc["@mozilla.org/content/style-sheet-service;1"].getService(Ci.nsIStyleSheetService)
17 _clip = Cc["@mozilla.org/widget/clipboard;1"].getService(Ci.nsIClipboard)
18
19 class Bucket
20 constructor: (@idFunc, @newFunc) ->
21 @bucket = {}
22
23 get: (obj) ->
24 id = @idFunc obj
25 if container = @bucket[id]
26 return container
27 else
28 return @bucket[id] = @newFunc obj
29
30 forget: (obj) ->
31 delete @bucket[id] if id = @idFunc obj
32
33
34 class WindowEventTracker
35 constructor: (events, eventFilter = null) ->
36
37 handlerFilter = (handler) ->
38 return (event) ->
39 if !eventFilter or eventFilter event
40 handler event
41
42 addEventListeners = (window) ->
43 for name, handler of events
44 window.addEventListener name, handlerFilter(handler), true
45
46 removeEventListeners = (window) ->
47 for name, handler of events
48 window.removeEventListener name, handlerFilter(handler), true
49
50 @windowTracker = new WindowTracker
51 track: (window) ->
52 if isBrowserWindow window
53 addEventListeners window
54
55 untrack: (window) ->
56 if isBrowserWindow window
57 removeEventListeners window
58
59 start: -> @windowTracker.start()
60 stop: -> @windowTracker.stop()
61
62 isRootWindow = (window) ->
63 window.location == "chrome://browser/content/browser.xul"
64
65 getEventWindow = (event) ->
66 if event.originalTarget instanceof Window
67 return event.originalTarget
68 else
69 doc = event.originalTarget.ownerDocument or event.originalTarget
70 if doc instanceof HTMLDocument or doc instanceof XULDocument
71 return doc.defaultView
72
73 getEventTabWindow = (event) ->
74 if window = getEventWindow event
75 if isRootWindow window
76 return window.gBrowser.tabs.selectedItem?.contentWindow.wrappedJSObject
77 else
78 return window
79
80 getEventRootWindow = (event) ->
81 if window = getEventWindow event
82 return getRootWindow window
83
84 getEventTabBrowser = (event) ->
85 cw.gBrowser if cw = getEventRootWindow event
86
87 getRootWindow = (window) ->
88 return window.QueryInterface(Ci.nsIInterfaceRequestor)
89 .getInterface(Ci.nsIWebNavigation)
90 .QueryInterface(Ci.nsIDocShellTreeItem)
91 .rootTreeItem
92 .QueryInterface(Ci.nsIInterfaceRequestor)
93 .getInterface(Window);
94
95 isElementEditable = (element) ->
96 return element.isContentEditable or \
97 element instanceof HTMLInputElement or \
98 element instanceof HTMLTextAreaElement or \
99 element instanceof HTMLSelectElement
100
101 getWindowId = (window) ->
102 return window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
103 .getInterface(Components.interfaces.nsIDOMWindowUtils)
104 .outerWindowID
105
106 getSessionStore = ->
107 Cc["@mozilla.org/browser/sessionstore;1"].getService(Ci.nsISessionStore);
108
109 # Function that returns a URI to the css file that's part of the extension
110 cssUri = do () ->
111 tools = {}
112 Cu.import "resource://gre/modules/Services.jsm", tools
113 (name) ->
114 baseURI = tools.Services.io.newURI __SCRIPT_URI_SPEC__, null, null
115 uri = tools.Services.io.newURI "resources/#{ name }.css", null, baseURI
116 return uri
117
118 # Loads the css identified by the name in the StyleSheetService as User Stylesheet
119 # The stylesheet is then appended to every document, but it can be overwritten by
120 # any user css
121 loadCss = (name) ->
122 _sss.loadAndRegisterSheet(cssUri(name), _sss.USER_SHEET)
123
124 # Unloads the css file that's been loaded earlier with `loadCss`
125 unloadCss = (name) ->
126 uri = cssUri(name)
127 if _sss.sheetRegistered(uri, _sss.USER_SHEET)
128 _sss.unregisterSheet(uri, _sss.USER_SHEET)
129
130 # processes the keyboard event and extracts string representation
131 # of the key *without modifiers* in case this is the kind of a key
132 # that can be handled by the extension
133 #
134 # Currently we handle letters, Escape and Tab keys
135 keyboardEventChar = (keyboardEvent) ->
136 if keyboardEvent.charCode > 0
137 char = String.fromCharCode(keyboardEvent.charCode)
138 if char.match /\s/
139 char = undefined
140 else
141 switch keyboardEvent.keyCode
142 when KeyboardEvent.DOM_VK_ESCAPE then char = 'Esc'
143 when KeyboardEvent.DOM_VK_BACK_SPACE then char = 'Backspace'
144 else char = undefined
145
146 return char
147
148 # extracts string representation of the KeyboardEvent and adds
149 # relevant modifiers (_ctrl_, _alt_, _meta_) in case they were pressed
150 keyboardEventKey = (keyboardEvent) ->
151 char = keyboardEventChar keyboardEvent
152
153 {
154 shiftKey: shift,
155 altKey: alt,
156 ctrlKey: ctrl,
157 metaKey: meta,
158 } = keyboardEvent
159
160 if alt or ctrl or meta
161 k = (a, b) -> if a then b else ''
162 return "<#{ k(ctrl, 'c') + k(alt, 'a') + k(meta, 'm') }-#{ char }>"
163 else
164 return char
165
166 # Simulate mouse click with full chain of event
167 # Copied from Vimium codebase
168 simulateClick = (element, modifiers) ->
169 document = element.ownerDocument
170 window = document.defaultView
171 modifiers ||= {}
172
173 eventSequence = ["mouseover", "mousedown", "mouseup", "click"]
174 for event in eventSequence
175 mouseEvent = document.createEvent("MouseEvents")
176 mouseEvent.initMouseEvent(event, true, true, window, 1, 0, 0, 0, 0, modifiers.ctrlKey, false, false,
177 modifiers.metaKey, 0, null)
178 # Debugging note: Firefox will not execute the element's default action if we dispatch this click event,
179 # but Webkit will. Dispatching a click on an input box does not seem to focus it; we do that separately
180 element.dispatchEvent(mouseEvent)
181
182 # Write a string into system clipboard
183 writeToClipboard = (text) ->
184 str = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsString);
185 str.data = text
186
187 trans = Cc["@mozilla.org/widget/transferable;1"].createInstance(Ci.nsITransferable);
188 trans.addDataFlavor("text/unicode");
189 trans.setTransferData("text/unicode", str, text.length * 2);
190
191 _clip.setData trans, null, Ci.nsIClipboard.kGlobalClipboard
192 #
193 # Write a string into system clipboard
194 readFromClipboard = () ->
195 trans = Cc["@mozilla.org/widget/transferable;1"].createInstance(Ci.nsITransferable);
196 trans.addDataFlavor("text/unicode");
197
198 _clip.getData trans, Ci.nsIClipboard.kGlobalClipboard
199
200 str = {}
201 strLength = {}
202
203 trans.getTransferData("text/unicode", str, strLength)
204
205 if str
206 str = str.value.QueryInterface(Ci.nsISupportsString);
207 return str.data.substring 0, strLength.value / 2
208
209 return undefined
210
211
212 exports.WindowEventTracker = WindowEventTracker
213 exports.Bucket = Bucket
214 exports.isRootWindow = isRootWindow
215 exports.getEventWindow = getEventWindow
216 exports.getEventTabWindow = getEventTabWindow
217 exports.getEventRootWindow = getEventRootWindow
218 exports.getEventTabBrowser = getEventTabBrowser
219
220 exports.getWindowId = getWindowId
221 exports.getRootWindow = getRootWindow
222 exports.isElementEditable = isElementEditable
223 exports.getSessionStore = getSessionStore
224
225 exports.loadCss = loadCss
226 exports.unloadCss = unloadCss
227
228 exports.keyboardEventKey = keyboardEventKey
229 exports.simulateClick = simulateClick
230 exports.readFromClipboard = readFromClipboard
231 exports.writeToClipboard = writeToClipboard
Imprint / Impressum