aboutsummaryrefslogtreecommitdiffstats
path: root/lib/utils.coffee
diff options
context:
space:
mode:
Diffstat (limited to 'lib/utils.coffee')
-rw-r--r--lib/utils.coffee38
1 files changed, 28 insertions, 10 deletions
diff --git a/lib/utils.coffee b/lib/utils.coffee
index babb5f96..6f38be8f 100644
--- a/lib/utils.coffee
+++ b/lib/utils.coffee
@@ -1,4 +1,27 @@
+# Only pass events to the handler if they are marked as trusted by the browser.
+# This is kept in the global namespace for brevity and ease of use.
+window.forTrusted ?= (handler) -> (event) ->
+ if event?.isTrusted
+ handler.apply this, arguments
+ else
+ true
+
+browserInfo = browser?.runtime?.getBrowserInfo?()
+
Utils =
+ isFirefox: do ->
+ # NOTE(mrmr1993): This test only works in the background page, this is overwritten by isEnabledForUrl for
+ # content scripts.
+ isFirefox = false
+ browserInfo?.then? (browserInfo) ->
+ isFirefox = browserInfo?.name == "Firefox"
+ -> isFirefox
+ firefoxVersion: do ->
+ # NOTE(mrmr1993): This only works in the background page.
+ ffVersion = undefined
+ browserInfo?.then? (browserInfo) ->
+ ffVersion = browserInfo?.version
+ -> ffVersion
getCurrentVersion: ->
chrome.runtime.getManifest().version
@@ -9,14 +32,6 @@ Utils =
# Returns true whenever the current page is the extension's background page.
isBackgroundPage: -> @isExtensionPage() and chrome.extension.getBackgroundPage?() == window
- # Takes a dot-notation object string and calls the function that it points to with the correct value for
- # 'this'.
- invokeCommandString: (str, args...) ->
- [names..., name] = str.split '.'
- obj = window
- obj = obj[component] for component in names
- obj[name].apply obj, args
-
# Escape all special characters, so RegExp will parse the string 'as is'.
# Taken from http://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex
escapeRegexSpecialCharacters: do ->
@@ -41,7 +56,7 @@ Utils =
url.startsWith "javascript:"
hasFullUrlPrefix: do ->
- urlPrefix = new RegExp "^[a-z]{3,}://."
+ urlPrefix = new RegExp "^[a-z][-+.a-z0-9]{2,}://."
(url) -> urlPrefix.test url
# Decode valid escape sequences in a URI. This is intended to mimic the best-effort decoding
@@ -312,8 +327,11 @@ class JobRunner
onReady: (callback) ->
@fetcher.use callback
-root = exports ? window
+root = exports ? (window.root ?= {})
root.Utils = Utils
root.SimpleCache = SimpleCache
root.AsyncDataFetcher = AsyncDataFetcher
root.JobRunner = JobRunner
+unless exports?
+ root.extend = extend
+ extend window, root