]> git.gir.st - VimFx.git/blob - extension/packages/utils.coffee
Merge pull request #137 from ctype/delocale
[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 ///^#{ 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 return ->
192 return version
193
194 # Simulate smooth scrolling
195 smoothScroll = (window, dx, dy, msecs) ->
196 if msecs <= 0 || !Services.prefs.getBoolPref('general.smoothScroll')
197 window.scrollBy(dx, dy)
198 else
199 # Callback
200 fn = (_x, _y) ->
201 window.scrollBy(_x, _y)
202 # Number of steps
203 delta = 10
204 l = Math.round(msecs / delta)
205 while l > 0
206 x = Math.round(dx / l)
207 y = Math.round(dy / l)
208 dx -= x
209 dy -= y
210
211 l -= 1
212 window.setTimeout(fn, l * delta, x, y)
213
214 parseHTML = (document, html) ->
215 parser = Cc['@mozilla.org/parserutils;1'].getService(Ci.nsIParserUtils)
216 flags = parser.SanitizerAllowStyle
217 return parser.parseFragment(html, flags, false, null, document.documentElement)
218
219 # Uses nsIIOService to parse a string as a URL and find out if it is a URL
220 isURL = (str) ->
221 try
222 url = Cc['@mozilla.org/network/io-service;1']
223 .getService(Ci.nsIIOService)
224 .newURI(str, null, null)
225 .QueryInterface(Ci.nsIURL)
226 return true
227 catch err
228 return false
229
230 # Use Firefox services to search for a given string
231 browserSearchSubmission = (str) ->
232 ss = Cc['@mozilla.org/browser/search-service;1']
233 .getService(Ci.nsIBrowserSearchService)
234
235 engine = ss.currentEngine or ss.defaultEngine
236 return engine.getSubmission(str, null)
237
238 # Get hint characters, convert them to lower case and fall back
239 # to default hint characters if there are less than 3 chars
240 getHintChars = do ->
241 # Remove duplicate characters from string (case insensitive)
242 removeDuplicateCharacters = (str) ->
243 seen = {}
244 return str.toLowerCase()
245 .split('')
246 .filter((char) -> if seen[char] then false else (seen[char] = true))
247 .join('')
248
249 return ->
250 hintChars = removeDuplicateCharacters(getPref('hint_chars'))
251 if hintChars.length < 3
252 hintChars = getDefaultPref('hint_chars')
253
254 return hintChars
255
256 exports.Bucket = Bucket
257 exports.getCurrentTabWindow = getCurrentTabWindow
258 exports.getEventWindow = getEventWindow
259 exports.getEventRootWindow = getEventRootWindow
260 exports.getEventTabBrowser = getEventTabBrowser
261
262 exports.getWindowId = getWindowId
263 exports.getRootWindow = getRootWindow
264 exports.isTextInputElement = isTextInputElement
265 exports.isElementEditable = isElementEditable
266 exports.getSessionStore = getSessionStore
267
268 exports.loadCss = loadCss
269
270 exports.simulateClick = simulateClick
271 exports.smoothScroll = smoothScroll
272 exports.readFromClipboard = readFromClipboard
273 exports.writeToClipboard = writeToClipboard
274 exports.timeIt = timeIt
275 exports.isBlacklisted = isBlacklisted
276 exports.getVersion = getVersion
277 exports.parseHTML = parseHTML
278 exports.isURL = isURL
279 exports.browserSearchSubmission = browserSearchSubmission
280 exports.getHintChars = getHintChars
Imprint / Impressum