]> git.gir.st - VimFx.git/blob - extension/packages/utils.coffee
Closes #18. Toolbar button that's been removed from the toolbar won't be restored...
[VimFx.git] / extension / packages / utils.coffee
1 { interfaces: Ci, classes: Cc, utils: Cu } = Components
2
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
13 _sss = Cc["@mozilla.org/content/style-sheet-service;1"].getService(Ci.nsIStyleSheetService)
14 _clip = Cc["@mozilla.org/widget/clipboard;1"].getService(Ci.nsIClipboard)
15
16 { getPref } = require 'prefs'
17
18 class Bucket
19 constructor: (@idFunc, @newFunc) ->
20 @bucket = {}
21
22 get: (obj) ->
23 id = @idFunc obj
24 if container = @bucket[id]
25 return container
26 else
27 return @bucket[id] = @newFunc obj
28
29 forget: (obj) ->
30 delete @bucket[id] if id = @idFunc obj
31
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
37
38 # Returns the window associated with the event
39 getEventWindow = (event) ->
40 if event.originalTarget instanceof Window
41 return event.originalTarget
42 else
43 doc = event.originalTarget.ownerDocument or event.originalTarget
44 if doc instanceof HTMLDocument or doc instanceof XULDocument
45 return doc.defaultView
46
47 getEventRootWindow = (event) ->
48 if window = getEventWindow event
49 return getRootWindow window
50
51 getEventTabBrowser = (event) ->
52 cw.gBrowser if cw = getEventRootWindow event
53
54 getRootWindow = (window) ->
55 return window.QueryInterface(Ci.nsIInterfaceRequestor)
56 .getInterface(Ci.nsIWebNavigation)
57 .QueryInterface(Ci.nsIDocShellTreeItem)
58 .rootTreeItem
59 .QueryInterface(Ci.nsIInterfaceRequestor)
60 .getInterface(Window);
61
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') == 'true'
69
70 getWindowId = (window) ->
71 return window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
72 .getInterface(Components.interfaces.nsIDOMWindowUtils)
73 .outerWindowID
74
75 getSessionStore = ->
76 Cc["@mozilla.org/browser/sessionstore;1"].getService(Ci.nsISessionStore);
77
78 # Function that returns a URI to the css file that's part of the extension
79 cssUri = do () ->
80 (name) ->
81 baseURI = Services.io.newURI __SCRIPT_URI_SPEC__, null, null
82 uri = Services.io.newURI "resources/#{ name }.css", null, baseURI
83 return uri
84
85 # Loads the css identified by the name in the StyleSheetService as User Stylesheet
86 # The stylesheet is then appended to every document, but it can be overwritten by
87 # any user css
88 loadCss = (name) ->
89 uri = cssUri(name)
90 if !_sss.sheetRegistered(uri, _sss.AGENT_SHEET)
91 _sss.loadAndRegisterSheet(uri, _sss.AGENT_SHEET)
92
93 unload ->
94 _sss.unregisterSheet(uri, _sss.AGENT_SHEET)
95
96 # Simulate mouse click with full chain of event
97 # Copied from Vimium codebase
98 simulateClick = (element, modifiers) ->
99 document = element.ownerDocument
100 window = document.defaultView
101 modifiers ||= {}
102
103 eventSequence = ["mouseover", "mousedown", "mouseup", "click"]
104 for event in eventSequence
105 mouseEvent = document.createEvent("MouseEvents")
106 mouseEvent.initMouseEvent(event, true, true, window, 1, 0, 0, 0, 0, modifiers.ctrlKey, false, false,
107 modifiers.metaKey, 0, null)
108 # Debugging note: Firefox will not execute the element's default action if we dispatch this click event,
109 # but Webkit will. Dispatching a click on an input box does not seem to focus it; we do that separately
110 element.dispatchEvent(mouseEvent)
111
112 # Write a string into system clipboard
113 writeToClipboard = (window, text) ->
114 str = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsString);
115 str.data = text
116
117 trans = Cc["@mozilla.org/widget/transferable;1"].createInstance(Ci.nsITransferable);
118
119 if trans.init
120 privacyContext = window.QueryInterface(Ci.nsIInterfaceRequestor)
121 .getInterface(Ci.nsIWebNavigation)
122 .QueryInterface(Ci.nsILoadContext);
123 trans.init(privacyContext);
124
125 trans.addDataFlavor("text/unicode");
126 trans.setTransferData("text/unicode", str, text.length * 2);
127
128 _clip.setData trans, null, Ci.nsIClipboard.kGlobalClipboard
129 #
130 # Write a string into system clipboard
131 readFromClipboard = (window) ->
132 trans = Cc["@mozilla.org/widget/transferable;1"].createInstance(Ci.nsITransferable);
133
134 if trans.init
135 privacyContext = window.QueryInterface(Ci.nsIInterfaceRequestor)
136 .getInterface(Ci.nsIWebNavigation)
137 .QueryInterface(Ci.nsILoadContext);
138 trans.init(privacyContext);
139
140 trans.addDataFlavor("text/unicode");
141
142 _clip.getData trans, Ci.nsIClipboard.kGlobalClipboard
143
144 str = {}
145 strLength = {}
146
147 trans.getTransferData("text/unicode", str, strLength)
148
149 if str
150 str = str.value.QueryInterface(Ci.nsISupportsString);
151 return str.data.substring 0, strLength.value / 2
152
153 return undefined
154
155 # Executes function `func` and mearues how much time it took
156 timeIt = (func, msg) ->
157 start = new Date().getTime()
158 result = func()
159 end = new Date().getTime()
160
161 console.log msg, end - start
162 return result
163
164 # Checks if the string provided matches one of the black list entries
165 # `blackList`: comma/space separated list of URLs with wildcards (* and !)
166 isBlacklisted = (str, blackList) ->
167 for rule in blackList.split(/[\s,]+/)
168 rule = rule.replace(/\*/g, '.*').replace(/\!/g, '.')
169 if str.match new RegExp("^#{ rule }$")
170 return true
171
172 return false
173
174 # Gets VimFx verions. AddonManager only provides async API to access addon data, so it's a bit tricky...
175 getVersion = do ->
176 version = null
177
178 if version == null
179 scope = {}
180 addonId = getPref 'addon_id'
181 Cu.import("resource://gre/modules/AddonManager.jsm", scope);
182 scope.AddonManager.getAddonByID addonId, ((addon) -> version = addon.version)
183
184 -> version
185
186 exports.Bucket = Bucket
187 exports.getCurrentTabWindow = getCurrentTabWindow
188 exports.getEventWindow = getEventWindow
189 exports.getEventRootWindow = getEventRootWindow
190 exports.getEventTabBrowser = getEventTabBrowser
191
192 exports.getWindowId = getWindowId
193 exports.getRootWindow = getRootWindow
194 exports.isElementEditable = isElementEditable
195 exports.getSessionStore = getSessionStore
196
197 exports.loadCss = loadCss
198
199 exports.simulateClick = simulateClick
200 exports.readFromClipboard = readFromClipboard
201 exports.writeToClipboard = writeToClipboard
202 exports.timeIt = timeIt
203 exports.isBlacklisted = isBlacklisted
204 exports.getVersion = getVersion
Imprint / Impressum