From 3f64823fc89870ffbd4dd34f7875e398cc73511f Mon Sep 17 00:00:00 2001 From: ccd0 Date: Sun, 28 Sep 2014 12:40:33 -0700 Subject: [PATCH] fix data loss when HTTP and HTTPS pages are open in different tabs --- src/General/lib/$.coffee | 21 ++++++++++++++++++--- src/General/lib/databoard.class | 5 +++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/General/lib/$.coffee b/src/General/lib/$.coffee index 9d9a8c6aa..e4bc0d3dd 100755 --- a/src/General/lib/$.coffee +++ b/src/General/lib/$.coffee @@ -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 diff --git a/src/General/lib/databoard.class b/src/General/lib/databoard.class index 3545b5469..236d00b8f 100755 --- a/src/General/lib/databoard.class +++ b/src/General/lib/databoard.class @@ -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}