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