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
return
(key, cb) -> $.syncing[key] = cb
$.forceSync = (key) -> return
$.localKeys = [
# filters
'name',
@ -407,11 +408,25 @@ do ->
<% } else { %>
# http://wiki.greasespot.net/Main_Page
$.sync = do ->
$.on window, 'storage', ({key, newValue}) ->
do ->
oldValue = {}
onChange = ({key, newValue}) ->
if cb = $.syncing[key]
oldValue[key] = newValue
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) ->
unless keys instanceof Array

View File

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