Use Tampermonkey's GM_addValueChangeListener.
This commit is contained in:
parent
56fb01add1
commit
0402752060
@ -290,6 +290,7 @@ module.exports = (grunt) ->
|
|||||||
GM_setValue: true
|
GM_setValue: true
|
||||||
GM_deleteValue: true
|
GM_deleteValue: true
|
||||||
GM_listValues: true
|
GM_listValues: true
|
||||||
|
GM_addValueChangeListener: true
|
||||||
GM_openInTab: true
|
GM_openInTab: true
|
||||||
GM_xmlhttpRequest: true
|
GM_xmlhttpRequest: true
|
||||||
cloneInto: true
|
cloneInto: true
|
||||||
|
|||||||
@ -421,10 +421,19 @@ do ->
|
|||||||
<% } else { %>
|
<% } else { %>
|
||||||
|
|
||||||
# http://wiki.greasespot.net/Main_Page
|
# http://wiki.greasespot.net/Main_Page
|
||||||
$.oldValue = {}
|
|
||||||
|
|
||||||
if GM_deleteValue?
|
if GM_deleteValue?
|
||||||
$.getValue = GM_getValue
|
$.getValue = GM_getValue
|
||||||
|
$.listValues = -> GM_listValues() # error when called if missing
|
||||||
|
else
|
||||||
|
$.getValue = (key) -> localStorage[key]
|
||||||
|
$.listValues = ->
|
||||||
|
key for key of localStorage when key[...g.NAMESPACE.length] is g.NAMESPACE
|
||||||
|
|
||||||
|
if GM_addValueChangeListener?
|
||||||
|
$.setValue = GM_setValue
|
||||||
|
$.deleteValue = GM_deleteValue
|
||||||
|
else if GM_deleteValue?
|
||||||
|
$.oldValue = {}
|
||||||
$.setValue = (key, val) ->
|
$.setValue = (key, val) ->
|
||||||
GM_setValue key, val
|
GM_setValue key, val
|
||||||
if key of $.syncing
|
if key of $.syncing
|
||||||
@ -435,41 +444,46 @@ if GM_deleteValue?
|
|||||||
if key of $.syncing
|
if key of $.syncing
|
||||||
delete $.oldValue[key]
|
delete $.oldValue[key]
|
||||||
delete localStorage[key] # for `storage` events
|
delete localStorage[key] # for `storage` events
|
||||||
$.listValues = -> GM_listValues() # error when called if missing
|
|
||||||
else
|
else
|
||||||
$.getValue = (key) -> localStorage[key]
|
$.oldValue = {}
|
||||||
$.setValue = (key, val) ->
|
$.setValue = (key, val) ->
|
||||||
$.oldValue[key] = val if key of $.syncing
|
$.oldValue[key] = val if key of $.syncing
|
||||||
localStorage[key] = val
|
localStorage[key] = val
|
||||||
$.deleteValue = (key) ->
|
$.deleteValue = (key) ->
|
||||||
delete $.oldValue[key] if key of $.syncing
|
delete $.oldValue[key] if key of $.syncing
|
||||||
delete localStorage[key]
|
delete localStorage[key]
|
||||||
$.listValues = ->
|
|
||||||
key for key of localStorage when key[...g.NAMESPACE.length] is g.NAMESPACE
|
|
||||||
|
|
||||||
$.sync = (key, cb) ->
|
if GM_addValueChangeListener?
|
||||||
key = g.NAMESPACE + key
|
$.sync = (key, cb) ->
|
||||||
$.syncing[key] = cb
|
$.syncing[key] = GM_addValueChangeListener g.NAMESPACE + key, (key2, oldValue, newValue, remote) ->
|
||||||
$.oldValue[key] = $.getValue key
|
if remote
|
||||||
|
newValue = JSON.parse newValue unless newValue is undefined
|
||||||
|
cb newValue, key
|
||||||
|
$.forceSync = ->
|
||||||
|
else
|
||||||
|
$.sync = (key, cb) ->
|
||||||
|
key = g.NAMESPACE + key
|
||||||
|
$.syncing[key] = cb
|
||||||
|
$.oldValue[key] = $.getValue key
|
||||||
|
|
||||||
do ->
|
do ->
|
||||||
onChange = (key) ->
|
onChange = (key) ->
|
||||||
return unless cb = $.syncing[key]
|
return unless cb = $.syncing[key]
|
||||||
newValue = $.getValue key
|
newValue = $.getValue key
|
||||||
return if newValue is $.oldValue[key]
|
return if newValue is $.oldValue[key]
|
||||||
if newValue?
|
if newValue?
|
||||||
$.oldValue[key] = newValue
|
$.oldValue[key] = newValue
|
||||||
cb JSON.parse(newValue), key
|
cb JSON.parse(newValue), key
|
||||||
else
|
else
|
||||||
delete $.oldValue[key]
|
delete $.oldValue[key]
|
||||||
cb undefined, key
|
cb undefined, key
|
||||||
$.on window, 'storage', ({key}) -> onChange key
|
$.on window, 'storage', ({key}) -> onChange key
|
||||||
|
|
||||||
$.forceSync = (key) ->
|
$.forceSync = (key) ->
|
||||||
# Storage events don't work across origins
|
# Storage events don't work across origins
|
||||||
# e.g. http://boards.4chan.org and https://boards.4chan.org
|
# e.g. http://boards.4chan.org and https://boards.4chan.org
|
||||||
# so force a check for changes to avoid lost data.
|
# so force a check for changes to avoid lost data.
|
||||||
onChange g.NAMESPACE + key
|
onChange g.NAMESPACE + key
|
||||||
|
|
||||||
$.delete = (keys) ->
|
$.delete = (keys) ->
|
||||||
unless keys instanceof Array
|
unless keys instanceof Array
|
||||||
|
|||||||
@ -22,6 +22,7 @@
|
|||||||
// @grant GM_setValue
|
// @grant GM_setValue
|
||||||
// @grant GM_deleteValue
|
// @grant GM_deleteValue
|
||||||
// @grant GM_listValues
|
// @grant GM_listValues
|
||||||
|
// @grant GM_addValueChangeListener
|
||||||
// @grant GM_openInTab
|
// @grant GM_openInTab
|
||||||
// @grant GM_xmlhttpRequest
|
// @grant GM_xmlhttpRequest
|
||||||
// @run-at document-start
|
// @run-at document-start
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user