Merge pull request #235 from OfficialNoob/patch-2

Formatting changes & proxy delete support
This commit is contained in:
Ajay Ramachandran 2020-01-11 15:17:34 -05:00 committed by GitHub
commit 5348496768
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

109
SB.js
View file

@ -24,33 +24,33 @@ class MapIO {
SB.config.handler.set(undefined, this.id, encodeStoredItem(this.map)); SB.config.handler.set(undefined, this.id, encodeStoredItem(this.map));
return this.map; return this.map;
} }
get(key) { get(key) {
return this.map.get(key); return this.map.get(key);
}
has(key) {
return this.map.has(key);
}
deleteProperty(key) {
if (this.map.has(key)) {
this.map.delete(key);
return true;
} else {
return false;
}
} }
size() { has(key) {
return this.map.size; return this.map.has(key);
} }
delete(key) { deleteProperty(key) {
this.map.delete(key); if (this.map.has(key)) {
this.map.delete(key);
return true;
} else {
return false;
}
}
size() {
return this.map.size;
}
delete(key) {
this.map.delete(key);
SB.config.handler.set(undefined, this.id, encodeStoredItem(this.map)); SB.config.handler.set(undefined, this.id, encodeStoredItem(this.map));
} }
} }
@ -75,11 +75,11 @@ function encodeStoredItem(data) {
function decodeStoredItem(data) { function decodeStoredItem(data) {
if(typeof data !== "string") return data; if(typeof data !== "string") return data;
try { try {
let str = JSON.parse(data); let str = JSON.parse(data);
if(!Array.isArray(str)) return data; if(!Array.isArray(str)) return data;
return new Map(str); return new Map(str);
} catch(e) { } catch(e) {
// If all else fails, return the data // If all else fails, return the data
@ -99,18 +99,23 @@ function configProxy() {
}); });
var handler = { var handler = {
set: function(obj, prop, value) { set(obj, prop, value) {
SB.localConfig[prop] = value; SB.localConfig[prop] = value;
chrome.storage.sync.set({ chrome.storage.sync.set({
[prop]: encodeStoredItem(value) [prop]: encodeStoredItem(value)
}); });
}, },
get: function(obj, prop) {
get(obj, prop) {
let data = SB.localConfig[prop]; let data = SB.localConfig[prop];
if(data instanceof Map) data = new MapIO(prop); if(data instanceof Map) data = new MapIO(prop);
return obj[prop] || data; return obj[prop] || data;
},
deleteProperty(obj, prop) {
chrome.storage.sync.remove(prop);
} }
}; };
@ -128,37 +133,37 @@ function fetchConfig() {
} }
function migrateOldFormats() { // Convert sponsorTimes format function migrateOldFormats() { // Convert sponsorTimes format
for (key in SB.localConfig) { for (const key in SB.localConfig) {
if (key.startsWith("sponsorTimes") && key !== "sponsorTimes" && key !== "sponsorTimesContributed") { if (key.startsWith("sponsorTimes") && key !== "sponsorTimes" && key !== "sponsorTimesContributed") {
SB.config.sponsorTimes.set(key.substr(12), SB.config[key]); SB.config.sponsorTimes.set(key.substr(12), SB.config[key]);
chrome.storage.sync.remove(key); delete SB.config[key];
} }
} }
} }
async function setupConfig() { async function setupConfig() {
await fetchConfig(); await fetchConfig();
addDefaults(); addDefaults();
convertJSON(); convertJSON();
SB.config = configProxy(); SB.config = configProxy();
migrateOldFormats(); migrateOldFormats();
} }
SB.defaults = { SB.defaults = {
"sponsorTimes": new Map(), "sponsorTimes": new Map(),
"startSponsorKeybind": ";", "startSponsorKeybind": ";",
"submitKeybind": "'", "submitKeybind": "'",
"minutesSaved": 0, "minutesSaved": 0,
"skipCount": 0, "skipCount": 0,
"sponsorTimesContributed": 0, "sponsorTimesContributed": 0,
"disableSkipping": false, "disableSkipping": false,
"disableAutoSkip": false, "disableAutoSkip": false,
"trackViewCount": true, "trackViewCount": true,
"dontShowNotice": false, "dontShowNotice": false,
"hideVideoPlayerControls": false, "hideVideoPlayerControls": false,
"hideInfoButtonPlayerControls": false, "hideInfoButtonPlayerControls": false,
"hideDeleteButtonPlayerControls": false, "hideDeleteButtonPlayerControls": false,
"hideDiscordLaunches": 0, "hideDiscordLaunches": 0,
"hideDiscordLink": false, "hideDiscordLink": false,
"invidiousInstances": ["invidio.us", "invidiou.sh", "invidious.snopyta.org"], "invidiousInstances": ["invidio.us", "invidiou.sh", "invidious.snopyta.org"],
"invidiousUpdateInfoShowCount": 0, "invidiousUpdateInfoShowCount": 0,
@ -167,21 +172,21 @@ SB.defaults = {
// Reset config // Reset config
function resetConfig() { function resetConfig() {
SB.config = SB.defaults; SB.config = SB.defaults;
}; };
function convertJSON() { function convertJSON() {
Object.keys(SB.defaults).forEach(key => { Object.keys(SB.defaults).forEach(key => {
SB.localConfig[key] = decodeStoredItem(SB.localConfig[key], key); SB.localConfig[key] = decodeStoredItem(SB.localConfig[key], key);
}); });
} }
// Add defaults // Add defaults
function addDefaults() { function addDefaults() {
for (const key in SB.defaults) { for (const key in SB.defaults) {
if(!SB.localConfig.hasOwnProperty(key)) { if(!SB.localConfig.hasOwnProperty(key)) {
SB.localConfig[key] = SB.defaults[key]; SB.localConfig[key] = SB.defaults[key];
} }
} }
}; };