]> git.gir.st - VimFx.git/blob - extension/packages/utils.coffee
Remove chrome.coffee
[VimFx.git] / extension / packages / utils.coffee
1 { getPref
2 , getDefaultPref
3 } = require 'prefs'
4
5 { classes: Cc, interfaces: Ci, utils: Cu } = Components
6
7 HTMLInputElement = Ci.nsIDOMHTMLInputElement
8 HTMLTextAreaElement = Ci.nsIDOMHTMLTextAreaElement
9 HTMLSelectElement = Ci.nsIDOMHTMLSelectElement
10 XULDocument = Ci.nsIDOMXULDocument
11 XULElement = Ci.nsIDOMXULElement
12 HTMLDocument = Ci.nsIDOMHTMLDocument
13 HTMLElement = Ci.nsIDOMHTMLElement
14 Window = Ci.nsIDOMWindow
15 ChromeWindow = Ci.nsIDOMChromeWindow
16
17 _sss = Cc["@mozilla.org/content/style-sheet-service;1"].getService(Ci.nsIStyleSheetService)
18 _clip = Cc["@mozilla.org/widget/clipboard;1"].getService(Ci.nsIClipboard)
19
20 class Bucket
21 constructor: (@idFunc, @newFunc) ->
22 @bucket = {}
23
24 get: (obj) ->
25 id = @idFunc obj
26 if container = @bucket[id]
27 return container
28 else
29 return @bucket[id] = @newFunc obj
30
31 forget: (obj) ->
32 delete @bucket[id] if id = @idFunc obj
33
34 # Returns the `window` from the currently active tab.
35 getCurrentTabWindow = (event) ->
36 if window = getEventWindow event
37 if rootWindow = getRootWindow window
38 return rootWindow.gBrowser.selectedTab.linkedBrowser.contentWindow
39
40 # Returns the window associated with the event
41 getEventWindow = (event) ->
42 if event.originalTarget instanceof Window
43 return event.originalTarget
44 else
45 doc = event.originalTarget.ownerDocument or event.originalTarget
46 if doc instanceof HTMLDocument or doc instanceof XULDocument
47 return doc.defaultView
48
49 getEventRootWindow = (event) ->
50 if window = getEventWindow event
51 return getRootWindow window
52
53 getEventTabBrowser = (event) ->
54 cw.gBrowser if cw = getEventRootWindow event
55
56 getRootWindow = (window) ->
57 return window.QueryInterface(Ci.nsIInterfaceRequestor)
58 .getInterface(Ci.nsIWebNavigation)
59 .QueryInterface(Ci.nsIDocShellTreeItem)
60 .rootTreeItem
61 .QueryInterface(Ci.nsIInterfaceRequestor)
62 .getInterface(Window);
63
64 isTextInputElement = (element) ->
65 return element instanceof HTMLInputElement or \
66 element instanceof HTMLTextAreaElement
67
68 isElementEditable = (element) ->
69 return element.isContentEditable or \
70 element instanceof HTMLInputElement or \
71 element instanceof HTMLTextAreaElement or \
72 element instanceof HTMLSelectElement or \
73 element.getAttribute('g_editable') == 'true' or \
74 element.getAttribute('contenteditable')?.toLowerCase() == 'true' or \
75 element.ownerDocument?.designMode?.toLowerCase() == 'on'
76
77 getWindowId = (window) ->
78 return window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
79 .getInterface(Components.interfaces.nsIDOMWindowUtils)
80 .outerWindowID
81
82 getSessionStore = ->
83 Cc["@mozilla.org/browser/sessionstore;1"].getService(Ci.nsISessionStore);
84
85 # Function that returns a URI to the css file that's part of the extension
86 cssUri = do () ->
87 (name) ->
88 baseURI = Services.io.newURI __SCRIPT_URI_SPEC__, null, null
89 uri = Services.io.newURI "resources/#{ name }.css", null, baseURI
90 return uri
91
92 # Loads the css identified by the name in the StyleSheetService as User Stylesheet
93 # The stylesheet is then appended to every document, but it can be overwritten by
94 # any user css
95 loadCss = (name) ->
96 uri = cssUri(name)
97 if !_sss.sheetRegistered(uri, _sss.AGENT_SHEET)
98 _sss.loadAndRegisterSheet(uri, _sss.AGENT_SHEET)
99
100 unload ->
101 _sss.unregisterSheet(uri, _sss.AGENT_SHEET)
102
103 # Simulate mouse click with full chain of event
104 # Copied from Vimium codebase
105 simulateClick = (element, modifiers) ->
106 document = element.ownerDocument
107 window = document.defaultView
108 modifiers ||= {}
109
110 eventSequence = ["mouseover", "mousedown", "mouseup", "click"]
111 for event in eventSequence
112 mouseEvent = document.createEvent("MouseEvents")
113 mouseEvent.initMouseEvent(event, true, true, window, 1, 0, 0, 0, 0, modifiers.ctrlKey, false, false,
114 modifiers.metaKey, 0, null)
115 # Debugging note: Firefox will not execute the element's default action if we dispatch this click event,
116 # but Webkit will. Dispatching a click on an input box does not seem to focus it; we do that separately
117 element.dispatchEvent(mouseEvent)
118
119 # Write a string into system clipboard
120 writeToClipboard = (window, text) ->
121 str = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsString);
122 str.data = text
123
124 trans = Cc["@mozilla.org/widget/transferable;1"].createInstance(Ci.nsITransferable);
125
126 if trans.init
127 privacyContext = window.QueryInterface(Ci.nsIInterfaceRequestor)
128 .getInterface(Ci.nsIWebNavigation)
129 .QueryInterface(Ci.nsILoadContext);
130 trans.init(privacyContext);
131
132 trans.addDataFlavor("text/unicode");
133 trans.setTransferData("text/unicode", str, text.length * 2);
134
135 _clip.setData trans, null, Ci.nsIClipboard.kGlobalClipboard
136
137 # Write a string into system clipboard
138 readFromClipboard = (window) ->
139 trans = Cc["@mozilla.org/widget/transferable;1"].createInstance(Ci.nsITransferable);
140
141 if trans.init
142 privacyContext = window.QueryInterface(Ci.nsIInterfaceRequestor)
143 .getInterface(Ci.nsIWebNavigation)
144 .QueryInterface(Ci.nsILoadContext);
145 trans.init(privacyContext);
146
147 trans.addDataFlavor("text/unicode");
148
149 _clip.getData trans, Ci.nsIClipboard.kGlobalClipboard
150
151 str = {}
152 strLength = {}
153
154 trans.getTransferData("text/unicode", str, strLength)
155
156 if str
157 str = str.value.QueryInterface(Ci.nsISupportsString);
158 return str.data.substring 0, strLength.value / 2
159
160 return undefined
161
162 # Executes function `func` and mearues how much time it took
163 timeIt = (func, msg) ->
164 start = new Date().getTime()
165 result = func()
166 end = new Date().getTime()
167
168 console.log msg, end - start
169 return result
170
171 # Checks if the string provided matches one of the black list entries
172 # `blackList`: comma/space separated list of URLs with wildcards (* and !)
173 isBlacklisted = (str, blackList) ->
174 for rule in blackList.split(/[\s,]+/)
175 rule = rule.replace(/\*/g, '.*').replace(/\!/g, '.')
176 if str.match new RegExp("^#{ rule }$")
177 return true
178
179 return false
180
181 # Gets VimFx verions. AddonManager only provides async API to access addon data, so it's a bit tricky...
182 getVersion = do ->
183 version = null
184
185 if version == null
186 scope = {}
187 addonId = getPref 'addon_id'
188 Cu.import("resource://gre/modules/AddonManager.jsm", scope);
189 scope.AddonManager.getAddonByID addonId, ((addon) -> version = addon.version)
190
191 -> version
192
193 # Simulate smooth scrolling
194 smoothScroll = (window, dx, dy, msecs) ->
195 if msecs <= 0 || !Services.prefs.getBoolPref 'general.smoothScroll'
196 window.scrollBy dx, dy
197 else
198 # Callback
199 fn = (_x, _y) ->
200 window.scrollBy(_x, _y)
201 # Number of steps
202 delta = 10
203 l = Math.round(msecs / delta)
204 while l > 0
205 x = Math.round(dx / l)
206 y = Math.round(dy / l)
207 dx -= x
208 dy -= y
209
210 l -= 1
211 window.setTimeout fn, l * delta, x, y
212
213 parseHTML = (document, html) ->
214 parser = Cc["@mozilla.org/parserutils;1"].getService(Ci.nsIParserUtils)
215 flags = parser.SanitizerAllowStyle
216 return parser.parseFragment(html, flags, false, null, document.documentElement)
217
218 # Uses nsIIOService to parse a string as a URL and find out if it is a URL
219 isURL = (str) ->
220 try
221 url = Cc["@mozilla.org/network/io-service;1"]
222 .getService(Ci.nsIIOService)
223 .newURI(str, null, null)
224 .QueryInterface(Ci.nsIURL)
225 return true
226 catch err
227 return false
228
229 # Use Firefox services to search for a given string
230 browserSearchSubmission = (str) ->
231 ss = Cc["@mozilla.org/browser/search-service;1"]
232 .getService(Ci.nsIBrowserSearchService)
233
234 engine = ss.currentEngine or ss.defaultEngine
235 return engine.getSubmission(str, null)
236
237 # Get hint characters, convert them to lower case and fall back
238 # to default hint characters if there are less than 3 chars
239 getHintChars = do ->
240 # Remove duplicate characters from string (case insensitive)
241 removeDuplicateCharacters = (str) ->
242 seen = {}
243 return str.toLowerCase()
244 .split('')
245 .filter((char) -> if seen[char] then false else (seen[char] = true))
246 .join ''
247
248 return ->
249 hintChars = removeDuplicateCharacters(getPref('hint_chars'))
250 if hintChars.length < 3
251 hintChars = getDefaultPref('hint_chars')
252
253 return hintChars
254
255 exports.Bucket = Bucket
256 exports.getCurrentTabWindow = getCurrentTabWindow
257 exports.getEventWindow = getEventWindow
258 exports.getEventRootWindow = getEventRootWindow
259 exports.getEventTabBrowser = getEventTabBrowser
260
261 exports.getWindowId = getWindowId
262 exports.getRootWindow = getRootWindow
263 exports.isTextInputElement = isTextInputElement
264 exports.isElementEditable = isElementEditable
265 exports.getSessionStore = getSessionStore
266
267 exports.loadCss = loadCss
268
269 exports.simulateClick = simulateClick
270 exports.smoothScroll = smoothScroll
271 exports.readFromClipboard = readFromClipboard
272 exports.writeToClipboard = writeToClipboard
273 exports.timeIt = timeIt
274 exports.isBlacklisted = isBlacklisted
275 exports.getVersion = getVersion
276 exports.parseHTML = parseHTML
277 exports.isURL = isURL
278 exports.browserSearchSubmission = browserSearchSubmission
279 exports.getHintChars = getHintChars
Imprint / Impressum