Don't extend $.
This commit is contained in:
parent
d2eda107c4
commit
dcba887954
101
lib/$.coffee
101
lib/$.coffee
@ -6,19 +6,13 @@ $ = (selector, root=d.body) ->
|
|||||||
$$ = (selector, root=d.body) ->
|
$$ = (selector, root=d.body) ->
|
||||||
[root.querySelectorAll(selector)...]
|
[root.querySelectorAll(selector)...]
|
||||||
|
|
||||||
$.extend = (object, properties) ->
|
$.SECOND = 1000
|
||||||
for key, val of properties
|
$.MINUTE = 1000 * 60
|
||||||
object[key] = val
|
$.HOUR = 1000 * 60 * 60
|
||||||
return
|
$.DAY = 1000 * 60 * 60 * 24
|
||||||
|
$.id = (id) ->
|
||||||
$.extend $,
|
|
||||||
SECOND: 1000
|
|
||||||
MINUTE: 1000 * 60
|
|
||||||
HOUR : 1000 * 60 * 60
|
|
||||||
DAY : 1000 * 60 * 60 * 24
|
|
||||||
id: (id) ->
|
|
||||||
d.getElementById id
|
d.getElementById id
|
||||||
ready: (fc) ->
|
$.ready = (fc) ->
|
||||||
if d.readyState in ['interactive', 'complete']
|
if d.readyState in ['interactive', 'complete']
|
||||||
$.queueTask fc
|
$.queueTask fc
|
||||||
return
|
return
|
||||||
@ -26,7 +20,7 @@ $.extend $,
|
|||||||
$.off d, 'DOMContentLoaded', cb
|
$.off d, 'DOMContentLoaded', cb
|
||||||
fc()
|
fc()
|
||||||
$.on d, 'DOMContentLoaded', cb
|
$.on d, 'DOMContentLoaded', cb
|
||||||
formData: (form) ->
|
$.formData = (form) ->
|
||||||
if form instanceof HTMLFormElement
|
if form instanceof HTMLFormElement
|
||||||
return new FormData form
|
return new FormData form
|
||||||
fd = new FormData()
|
fd = new FormData()
|
||||||
@ -39,7 +33,11 @@ $.extend $,
|
|||||||
else
|
else
|
||||||
fd.append key, val
|
fd.append key, val
|
||||||
fd
|
fd
|
||||||
ajax: (url, callbacks, opts={}) ->
|
$.extend = (object, properties) ->
|
||||||
|
for key, val of properties
|
||||||
|
object[key] = val
|
||||||
|
return
|
||||||
|
$.ajax = (url, callbacks, opts={}) ->
|
||||||
{type, cred, headers, upCallbacks, form, sync} = opts
|
{type, cred, headers, upCallbacks, form, sync} = opts
|
||||||
r = new XMLHttpRequest()
|
r = new XMLHttpRequest()
|
||||||
type or= form and 'post' or 'get'
|
type or= form and 'post' or 'get'
|
||||||
@ -51,7 +49,7 @@ $.extend $,
|
|||||||
r.withCredentials = cred
|
r.withCredentials = cred
|
||||||
r.send form
|
r.send form
|
||||||
r
|
r
|
||||||
cache: do ->
|
$.cache = do ->
|
||||||
reqs = {}
|
reqs = {}
|
||||||
(url, cb) ->
|
(url, cb) ->
|
||||||
if req = reqs[url]
|
if req = reqs[url]
|
||||||
@ -69,78 +67,78 @@ $.extend $,
|
|||||||
onerror: rm
|
onerror: rm
|
||||||
req.callbacks = [cb]
|
req.callbacks = [cb]
|
||||||
reqs[url] = req
|
reqs[url] = req
|
||||||
cb:
|
$.cb =
|
||||||
checked: ->
|
checked: ->
|
||||||
$.set @name, @checked
|
$.set @name, @checked
|
||||||
Conf[@name] = @checked
|
Conf[@name] = @checked
|
||||||
value: ->
|
value: ->
|
||||||
$.set @name, @value.trim()
|
$.set @name, @value.trim()
|
||||||
Conf[@name] = @value
|
Conf[@name] = @value
|
||||||
asap: (test, cb) ->
|
$.asap = (test, cb) ->
|
||||||
if test()
|
if test()
|
||||||
cb()
|
cb()
|
||||||
else
|
else
|
||||||
setTimeout $.asap, 25, test, cb
|
setTimeout $.asap, 25, test, cb
|
||||||
addStyle: (css) ->
|
$.addStyle = (css) ->
|
||||||
style = $.el 'style',
|
style = $.el 'style',
|
||||||
textContent: css
|
textContent: css
|
||||||
$.asap (-> d.head), ->
|
$.asap (-> d.head), ->
|
||||||
$.add d.head, style
|
$.add d.head, style
|
||||||
style
|
style
|
||||||
x: (path, root=d.body) ->
|
$.x = (path, root=d.body) ->
|
||||||
# XPathResult.ANY_UNORDERED_NODE_TYPE === 8
|
# XPathResult.ANY_UNORDERED_NODE_TYPE === 8
|
||||||
d.evaluate(path, root, null, 8, null).singleNodeValue
|
d.evaluate(path, root, null, 8, null).singleNodeValue
|
||||||
addClass: (el, className) ->
|
$.addClass = (el, className) ->
|
||||||
el.classList.add className
|
el.classList.add className
|
||||||
rmClass: (el, className) ->
|
$.rmClass = (el, className) ->
|
||||||
el.classList.remove className
|
el.classList.remove className
|
||||||
hasClass: (el, className) ->
|
$.hasClass = (el, className) ->
|
||||||
el.classList.contains className
|
el.classList.contains className
|
||||||
rm: do ->
|
$.rm = do ->
|
||||||
if 'remove' of Element.prototype
|
if 'remove' of Element.prototype
|
||||||
(el) -> el.remove()
|
(el) -> el.remove()
|
||||||
else
|
else
|
||||||
(el) -> el.parentNode?.removeChild el
|
(el) -> el.parentNode?.removeChild el
|
||||||
rmAll: (root) ->
|
$.rmAll = (root) ->
|
||||||
# jsperf.com/emptify-element
|
# jsperf.com/emptify-element
|
||||||
while node = root.firstChild
|
while node = root.firstChild
|
||||||
# HTMLSelectElement.remove !== Element.remove
|
# HTMLSelectElement.remove !== Element.remove
|
||||||
root.removeChild node
|
root.removeChild node
|
||||||
return
|
return
|
||||||
tn: (s) ->
|
$.tn = (s) ->
|
||||||
d.createTextNode s
|
d.createTextNode s
|
||||||
nodes: (nodes) ->
|
$.nodes = (nodes) ->
|
||||||
unless nodes instanceof Array
|
unless nodes instanceof Array
|
||||||
return nodes
|
return nodes
|
||||||
frag = d.createDocumentFragment()
|
frag = d.createDocumentFragment()
|
||||||
for node in nodes
|
for node in nodes
|
||||||
frag.appendChild node
|
frag.appendChild node
|
||||||
frag
|
frag
|
||||||
add: (parent, el) ->
|
$.add = (parent, el) ->
|
||||||
parent.appendChild $.nodes el
|
parent.appendChild $.nodes el
|
||||||
prepend: (parent, el) ->
|
$.prepend = (parent, el) ->
|
||||||
parent.insertBefore $.nodes(el), parent.firstChild
|
parent.insertBefore $.nodes(el), parent.firstChild
|
||||||
after: (root, el) ->
|
$.after = (root, el) ->
|
||||||
root.parentNode.insertBefore $.nodes(el), root.nextSibling
|
root.parentNode.insertBefore $.nodes(el), root.nextSibling
|
||||||
before: (root, el) ->
|
$.before = (root, el) ->
|
||||||
root.parentNode.insertBefore $.nodes(el), root
|
root.parentNode.insertBefore $.nodes(el), root
|
||||||
replace: (root, el) ->
|
$.replace = (root, el) ->
|
||||||
root.parentNode.replaceChild $.nodes(el), root
|
root.parentNode.replaceChild $.nodes(el), root
|
||||||
el: (tag, properties) ->
|
$.el = (tag, properties) ->
|
||||||
el = d.createElement tag
|
el = d.createElement tag
|
||||||
$.extend el, properties if properties
|
$.extend el, properties if properties
|
||||||
el
|
el
|
||||||
on: (el, events, handler) ->
|
$.on = (el, events, handler) ->
|
||||||
for event in events.split ' '
|
for event in events.split ' '
|
||||||
el.addEventListener event, handler, false
|
el.addEventListener event, handler, false
|
||||||
return
|
return
|
||||||
off: (el, events, handler) ->
|
$.off = (el, events, handler) ->
|
||||||
for event in events.split ' '
|
for event in events.split ' '
|
||||||
el.removeEventListener event, handler, false
|
el.removeEventListener event, handler, false
|
||||||
return
|
return
|
||||||
event: (event, detail, root=d) ->
|
$.event = (event, detail, root=d) ->
|
||||||
root.dispatchEvent new CustomEvent event, {bubbles: true, detail}
|
root.dispatchEvent new CustomEvent event, {bubbles: true, detail}
|
||||||
open: do ->
|
$.open = do ->
|
||||||
if GM_openInTab?
|
if GM_openInTab?
|
||||||
(URL) ->
|
(URL) ->
|
||||||
# XXX fix GM opening file://// for protocol-less URLs.
|
# XXX fix GM opening file://// for protocol-less URLs.
|
||||||
@ -148,7 +146,7 @@ $.extend $,
|
|||||||
GM_openInTab a.href
|
GM_openInTab a.href
|
||||||
else
|
else
|
||||||
(URL) -> window.open URL, '_blank'
|
(URL) -> window.open URL, '_blank'
|
||||||
debounce: (wait, fn) ->
|
$.debounce = (wait, fn) ->
|
||||||
timeout = null
|
timeout = null
|
||||||
that = null
|
that = null
|
||||||
args = null
|
args = null
|
||||||
@ -166,7 +164,7 @@ $.extend $,
|
|||||||
|
|
||||||
# after wait, let next invocation execute immediately
|
# after wait, let next invocation execute immediately
|
||||||
timeout = setTimeout exec, wait
|
timeout = setTimeout exec, wait
|
||||||
queueTask: do ->
|
$.queueTask = do ->
|
||||||
# inspired by https://www.w3.org/Bugs/Public/show_bug.cgi?id=15007
|
# inspired by https://www.w3.org/Bugs/Public/show_bug.cgi?id=15007
|
||||||
taskQueue = []
|
taskQueue = []
|
||||||
execTask = ->
|
execTask = ->
|
||||||
@ -184,12 +182,12 @@ $.extend $,
|
|||||||
->
|
->
|
||||||
taskQueue.push arguments
|
taskQueue.push arguments
|
||||||
setTimeout execTask, 0
|
setTimeout execTask, 0
|
||||||
globalEval: (code) ->
|
$.globalEval = (code) ->
|
||||||
script = $.el 'script',
|
script = $.el 'script',
|
||||||
textContent: code
|
textContent: code
|
||||||
$.add (d.head or doc), script
|
$.add (d.head or doc), script
|
||||||
$.rm script
|
$.rm script
|
||||||
bytesToString: (size) ->
|
$.bytesToString = (size) ->
|
||||||
unit = 0 # Bytes
|
unit = 0 # Bytes
|
||||||
while size >= 1024
|
while size >= 1024
|
||||||
size /= 1024
|
size /= 1024
|
||||||
@ -204,8 +202,8 @@ $.extend $,
|
|||||||
# Round to an integer otherwise.
|
# Round to an integer otherwise.
|
||||||
Math.round size
|
Math.round size
|
||||||
"#{size} #{['B', 'KB', 'MB', 'GB'][unit]}"
|
"#{size} #{['B', 'KB', 'MB', 'GB'][unit]}"
|
||||||
syncing: {}
|
$.syncing = {}
|
||||||
sync: do ->
|
$.sync = do ->
|
||||||
<% if (type === 'crx') { %>
|
<% if (type === 'crx') { %>
|
||||||
chrome.storage.onChanged.addListener (changes) ->
|
chrome.storage.onChanged.addListener (changes) ->
|
||||||
for key of changes
|
for key of changes
|
||||||
@ -214,28 +212,27 @@ $.extend $,
|
|||||||
return
|
return
|
||||||
(key, cb) -> $.syncing[key] = cb
|
(key, cb) -> $.syncing[key] = cb
|
||||||
<% } else { %>
|
<% } else { %>
|
||||||
window.addEventListener 'storage', (e) ->
|
$.on window, 'storage', (e) ->
|
||||||
if cb = $.syncing[e.key]
|
if cb = $.syncing[e.key]
|
||||||
cb JSON.parse e.newValue
|
cb JSON.parse e.newValue
|
||||||
, false
|
|
||||||
(key, cb) -> $.syncing[g.NAMESPACE + key] = cb
|
(key, cb) -> $.syncing[g.NAMESPACE + key] = cb
|
||||||
<% } %>
|
<% } %>
|
||||||
item: (key, val) ->
|
$.item = (key, val) ->
|
||||||
item = {}
|
item = {}
|
||||||
item[key] = val
|
item[key] = val
|
||||||
item
|
item
|
||||||
<% if (type === 'crx') { %>
|
<% if (type === 'crx') { %>
|
||||||
# https://developer.chrome.com/extensions/storage.html
|
# https://developer.chrome.com/extensions/storage.html
|
||||||
delete: (keys) ->
|
$.delete = (keys) ->
|
||||||
chrome.storage.sync.remove keys
|
chrome.storage.sync.remove keys
|
||||||
get: (key, val, cb) ->
|
$.get = (key, val, cb) ->
|
||||||
if typeof cb is 'function'
|
if typeof cb is 'function'
|
||||||
items = $.item key, val
|
items = $.item key, val
|
||||||
else
|
else
|
||||||
items = key
|
items = key
|
||||||
cb = val
|
cb = val
|
||||||
chrome.storage.sync.get items, cb
|
chrome.storage.sync.get items, cb
|
||||||
set: (key, val) ->
|
$.set = (key, val) ->
|
||||||
items = if typeof key is 'string'
|
items = if typeof key is 'string'
|
||||||
$.item key, val
|
$.item key, val
|
||||||
else
|
else
|
||||||
@ -287,7 +284,7 @@ do ->
|
|||||||
return
|
return
|
||||||
<% } else { %>
|
<% } else { %>
|
||||||
# http://wiki.greasespot.net/Main_Page
|
# http://wiki.greasespot.net/Main_Page
|
||||||
delete: (keys) ->
|
$.delete = (keys) ->
|
||||||
unless keys instanceof Array
|
unless keys instanceof Array
|
||||||
keys = [keys]
|
keys = [keys]
|
||||||
for key in keys
|
for key in keys
|
||||||
@ -295,7 +292,7 @@ do ->
|
|||||||
localStorage.removeItem key
|
localStorage.removeItem key
|
||||||
GM_deleteValue key
|
GM_deleteValue key
|
||||||
return
|
return
|
||||||
get: (key, val, cb) ->
|
$.get = (key, val, cb) ->
|
||||||
if typeof cb is 'function'
|
if typeof cb is 'function'
|
||||||
items = $.item key, val
|
items = $.item key, val
|
||||||
else
|
else
|
||||||
@ -306,7 +303,7 @@ do ->
|
|||||||
if val = GM_getValue g.NAMESPACE + key
|
if val = GM_getValue g.NAMESPACE + key
|
||||||
items[key] = JSON.parse val
|
items[key] = JSON.parse val
|
||||||
cb items
|
cb items
|
||||||
set: do ->
|
$.set = do ->
|
||||||
set = (key, val) ->
|
set = (key, val) ->
|
||||||
key = g.NAMESPACE + key
|
key = g.NAMESPACE + key
|
||||||
val = JSON.stringify val
|
val = JSON.stringify val
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user