fix data loss when HTTP and HTTPS pages are open in different tabs

This commit is contained in:
ccd0 2014-09-28 12:40:33 -07:00
parent 5d3d50ca07
commit 3f64823fc8
2 changed files with 23 additions and 3 deletions

View File

@ -295,6 +295,7 @@ $.sync = do ->
cb changes[key].newValue, key cb changes[key].newValue, key
return return
(key, cb) -> $.syncing[key] = cb (key, cb) -> $.syncing[key] = cb
$.forceSync = (key) -> return
$.localKeys = [ $.localKeys = [
# filters # filters
'name', 'name',
@ -407,11 +408,25 @@ do ->
<% } else { %> <% } else { %>
# http://wiki.greasespot.net/Main_Page # http://wiki.greasespot.net/Main_Page
$.sync = do -> do ->
$.on window, 'storage', ({key, newValue}) -> oldValue = {}
onChange = ({key, newValue}) ->
if cb = $.syncing[key] if cb = $.syncing[key]
oldValue[key] = newValue
cb JSON.parse(newValue), key cb JSON.parse(newValue), key
(key, cb) -> $.syncing[g.NAMESPACE + key] = cb $.on window, 'storage', onChange
$.sync = (key, cb) ->
key = g.NAMESPACE + key
$.syncing[key] = cb
oldValue[key] = GM_getValue key
$.forceSync = (key) ->
# Storage events don't work across origins
# e.g. http://boards.4chan.org and https://boards.4chan.org
# so force a check for changes to avoid lost data.
key = g.NAMESPACE + key
newValue = GM_getValue key
if newValue isnt oldValue[key]
onChange {key, newValue}
$.delete = (keys) -> $.delete = (keys) ->
unless keys instanceof Array unless keys instanceof Array

View File

@ -16,6 +16,7 @@ class DataBoard
save: -> $.set @key, @data save: -> $.set @key, @data
delete: ({boardID, threadID, postID}) -> delete: ({boardID, threadID, postID}) ->
$.forceSync @key
if postID if postID
return unless @data.boards[boardID]?[threadID] return unless @data.boards[boardID]?[threadID]
delete @data.boards[boardID][threadID][postID] delete @data.boards[boardID][threadID][postID]
@ -29,6 +30,7 @@ class DataBoard
@save() @save()
deleteIfEmpty: ({boardID, threadID}) -> deleteIfEmpty: ({boardID, threadID}) ->
$.forceSync @key
if threadID if threadID
unless Object.keys(@data.boards[boardID][threadID]).length unless Object.keys(@data.boards[boardID][threadID]).length
delete @data.boards[boardID][threadID] delete @data.boards[boardID][threadID]
@ -37,6 +39,7 @@ class DataBoard
delete @data.boards[boardID] delete @data.boards[boardID]
set: ({boardID, threadID, postID, val}) -> set: ({boardID, threadID, postID, val}) ->
$.forceSync @key
if postID isnt undefined if postID isnt undefined
((@data.boards[boardID] or= {})[threadID] or= {})[postID] = val ((@data.boards[boardID] or= {})[threadID] or= {})[postID] = val
else if threadID isnt undefined else if threadID isnt undefined
@ -46,6 +49,7 @@ class DataBoard
@save() @save()
get: ({boardID, threadID, postID, defaultValue}) -> get: ({boardID, threadID, postID, defaultValue}) ->
$.forceSync @key
if board = @data.boards[boardID] if board = @data.boards[boardID]
unless threadID unless threadID
if postID if postID
@ -63,6 +67,7 @@ class DataBoard
val or defaultValue val or defaultValue
clean: -> clean: ->
$.forceSync @key
for boardID, val of @data.boards for boardID, val of @data.boards
@deleteIfEmpty {boardID} @deleteIfEmpty {boardID}