diff options
author | Official Noob <[email protected]> | 2020-01-11 19:42:35 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2020-01-11 19:42:35 +0000 |
commit | 4de0ae51e83593ee0deb70b92f03e67b8e86ac86 (patch) | |
tree | c5566d390cf32be7031552474b07324a3a9848a3 | |
parent | f5a4ffabde3e29ea7c49ef73cf43ded9d2d43408 (diff) | |
download | SponsorBlock-4de0ae51e83593ee0deb70b92f03e67b8e86ac86.tar.gz SponsorBlock-4de0ae51e83593ee0deb70b92f03e67b8e86ac86.zip |
Formatting changes & proxy delete support
-rw-r--r-- | SB.js | 101 |
1 files changed, 53 insertions, 48 deletions
@@ -24,33 +24,33 @@ class MapIO { SB.config.handler.set(undefined, this.id, encodeStoredItem(this.map)); - return this.map; + return this.map; } - get(key) { - return this.map.get(key); - } + get(key) { + return this.map.get(key); + } - has(key) { - return this.map.has(key); + has(key) { + return this.map.has(key); } - deleteProperty(key) { - if (this.map.has(key)) { - this.map.delete(key); - return true; - } else { - return false; - } + deleteProperty(key) { + if (this.map.has(key)) { + this.map.delete(key); + return true; + } else { + return false; } + } - size() { - return this.map.size; + size() { + return this.map.size; } - delete(key) { - this.map.delete(key); - + delete(key) { + this.map.delete(key); + SB.config.handler.set(undefined, this.id, encodeStoredItem(this.map)); } } @@ -75,11 +75,11 @@ function encodeStoredItem(data) { function decodeStoredItem(data) { if(typeof data !== "string") return data; - try { + try { let str = JSON.parse(data); - if(!Array.isArray(str)) return data; - return new Map(str); + if(!Array.isArray(str)) return data; + return new Map(str); } catch(e) { // If all else fails, return the data @@ -99,18 +99,23 @@ function configProxy() { }); var handler = { - set: function(obj, prop, value) { + set(obj, prop, value) { SB.localConfig[prop] = value; chrome.storage.sync.set({ [prop]: encodeStoredItem(value) }); }, - get: function(obj, prop) { + + get(obj, prop) { let data = SB.localConfig[prop]; if(data instanceof Map) data = new MapIO(prop); - return obj[prop] || data; + return obj[prop] || data; + }, + + deleteProperty(obj, prop) { + chrome.storage.sync.remove(key); } }; @@ -131,34 +136,34 @@ function migrateOldFormats() { // Convert sponsorTimes format for (key in SB.localConfig) { if (key.startsWith("sponsorTimes") && key !== "sponsorTimes" && key !== "sponsorTimesContributed") { SB.config.sponsorTimes.set(key.substr(12), SB.config[key]); - chrome.storage.sync.remove(key); + delete SB.config[key]; } } } async function setupConfig() { await fetchConfig(); - addDefaults(); - convertJSON(); - SB.config = configProxy(); + addDefaults(); + convertJSON(); + SB.config = configProxy(); migrateOldFormats(); } SB.defaults = { - "sponsorTimes": new Map(), - "startSponsorKeybind": ";", - "submitKeybind": "'", - "minutesSaved": 0, - "skipCount": 0, - "sponsorTimesContributed": 0, - "disableSkipping": false, - "disableAutoSkip": false, - "trackViewCount": true, - "dontShowNotice": false, - "hideVideoPlayerControls": false, - "hideInfoButtonPlayerControls": false, - "hideDeleteButtonPlayerControls": false, - "hideDiscordLaunches": 0, + "sponsorTimes": new Map(), + "startSponsorKeybind": ";", + "submitKeybind": "'", + "minutesSaved": 0, + "skipCount": 0, + "sponsorTimesContributed": 0, + "disableSkipping": false, + "disableAutoSkip": false, + "trackViewCount": true, + "dontShowNotice": false, + "hideVideoPlayerControls": false, + "hideInfoButtonPlayerControls": false, + "hideDeleteButtonPlayerControls": false, + "hideDiscordLaunches": 0, "hideDiscordLink": false, "invidiousInstances": ["invidio.us", "invidiou.sh", "invidious.snopyta.org"], "invidiousUpdateInfoShowCount": 0, @@ -167,21 +172,21 @@ SB.defaults = { // Reset config function resetConfig() { - SB.config = SB.defaults; + SB.config = SB.defaults; }; function convertJSON() { - Object.keys(SB.defaults).forEach(key => { - SB.localConfig[key] = decodeStoredItem(SB.localConfig[key], key); - }); + Object.keys(SB.defaults).forEach(key => { + SB.localConfig[key] = decodeStoredItem(SB.localConfig[key], key); + }); } // Add defaults function addDefaults() { for (const key in SB.defaults) { if(!SB.localConfig.hasOwnProperty(key)) { - SB.localConfig[key] = SB.defaults[key]; - } + SB.localConfig[key] = SB.defaults[key]; + } } }; |