2020-01-10 02:09:32 +01:00
|
|
|
SB = {
|
|
|
|
/**
|
|
|
|
* Callback function when an option is updated
|
|
|
|
*
|
|
|
|
* @type {CallableFunction}
|
|
|
|
*/
|
2020-01-10 02:29:20 +01:00
|
|
|
configListeners: []
|
2020-01-10 02:09:32 +01:00
|
|
|
};
|
2019-12-31 21:07:43 +01:00
|
|
|
|
2020-01-09 18:46:04 +01:00
|
|
|
// Function setup
|
|
|
|
|
2020-01-08 23:22:18 +01:00
|
|
|
Map.prototype.toJSON = function() {
|
|
|
|
return Array.from(this.entries());
|
|
|
|
};
|
|
|
|
|
2020-01-09 18:38:15 +01:00
|
|
|
class MapIO {
|
2020-01-09 17:39:23 +01:00
|
|
|
constructor(id) {
|
2020-01-16 18:57:54 +01:00
|
|
|
this.id = id;
|
|
|
|
this.map = SB.localConfig[this.id];
|
2020-01-09 00:16:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
set(key, value) {
|
2020-01-16 18:57:54 +01:00
|
|
|
// Proxy to map
|
2020-01-09 18:30:09 +01:00
|
|
|
this.map.set(key, value);
|
2020-01-16 18:57:54 +01:00
|
|
|
// Store updated map locally
|
2020-01-16 15:29:03 +01:00
|
|
|
chrome.storage.sync.set({
|
|
|
|
[this.id]: encodeStoredItem(this.map)
|
|
|
|
});
|
2020-01-11 20:42:35 +01:00
|
|
|
return this.map;
|
2020-01-09 00:16:02 +01:00
|
|
|
}
|
2020-01-16 18:57:54 +01:00
|
|
|
|
2020-01-11 20:42:35 +01:00
|
|
|
get(key) {
|
2020-01-16 18:57:54 +01:00
|
|
|
return this.map.get(key);
|
|
|
|
}
|
2020-01-09 17:39:23 +01:00
|
|
|
|
2020-01-11 20:42:35 +01:00
|
|
|
has(key) {
|
2020-01-16 18:57:54 +01:00
|
|
|
return this.map.has(key);
|
2020-01-09 00:16:02 +01:00
|
|
|
}
|
2020-01-09 17:39:23 +01:00
|
|
|
|
2020-01-11 20:42:35 +01:00
|
|
|
deleteProperty(key) {
|
|
|
|
if (this.map.has(key)) {
|
2020-01-16 18:57:54 +01:00
|
|
|
// Proxy to map
|
2020-01-11 20:42:35 +01:00
|
|
|
this.map.delete(key);
|
2020-01-16 18:57:54 +01:00
|
|
|
// Store updated map locally
|
|
|
|
chrome.storage.sync.set({
|
|
|
|
[this.id]: encodeStoredItem(this.map)
|
|
|
|
});
|
2020-01-11 20:42:35 +01:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
2020-01-09 17:39:23 +01:00
|
|
|
}
|
2020-01-11 20:42:35 +01:00
|
|
|
}
|
2020-01-09 17:39:23 +01:00
|
|
|
|
2020-01-11 20:42:35 +01:00
|
|
|
size() {
|
|
|
|
return this.map.size;
|
2020-01-09 00:16:02 +01:00
|
|
|
}
|
2020-01-09 17:39:23 +01:00
|
|
|
|
2020-01-11 20:42:35 +01:00
|
|
|
delete(key) {
|
2020-01-16 18:57:54 +01:00
|
|
|
// Proxy to map
|
2020-01-11 20:42:35 +01:00
|
|
|
this.map.delete(key);
|
2020-01-16 18:57:54 +01:00
|
|
|
// Store updated map locally
|
|
|
|
chrome.storage.sync.set({
|
|
|
|
[this.id]: encodeStoredItem(this.map)
|
|
|
|
});
|
2020-01-09 00:16:02 +01:00
|
|
|
}
|
2020-01-09 18:12:49 +01:00
|
|
|
}
|
|
|
|
|
2020-01-09 18:46:04 +01:00
|
|
|
/**
|
|
|
|
* A Map cannot be stored in the chrome storage.
|
|
|
|
* This data will be encoded into an array instead as specified by the toJSON function.
|
|
|
|
*
|
|
|
|
* @param {*} data
|
|
|
|
*/
|
|
|
|
function encodeStoredItem(data) {
|
2020-01-09 18:12:49 +01:00
|
|
|
if(!(data instanceof Map)) return data;
|
|
|
|
return JSON.stringify(data);
|
|
|
|
}
|
|
|
|
|
2020-01-09 18:38:15 +01:00
|
|
|
/**
|
|
|
|
* A Map cannot be stored in the chrome storage.
|
|
|
|
* This data will be decoded from the array it is stored in
|
|
|
|
*
|
|
|
|
* @param {*} data
|
|
|
|
*/
|
|
|
|
function decodeStoredItem(data) {
|
2020-01-09 18:30:09 +01:00
|
|
|
if(typeof data !== "string") return data;
|
|
|
|
|
2020-01-11 20:42:35 +01:00
|
|
|
try {
|
2020-01-09 18:38:15 +01:00
|
|
|
let str = JSON.parse(data);
|
|
|
|
|
2020-01-11 20:48:17 +01:00
|
|
|
if(!Array.isArray(str)) return data;
|
|
|
|
return new Map(str);
|
2020-01-09 18:12:49 +01:00
|
|
|
} catch(e) {
|
|
|
|
|
2020-01-09 18:38:15 +01:00
|
|
|
// If all else fails, return the data
|
|
|
|
return data;
|
|
|
|
}
|
2020-01-09 00:16:02 +01:00
|
|
|
}
|
|
|
|
|
2019-12-31 21:07:43 +01:00
|
|
|
function configProxy() {
|
|
|
|
chrome.storage.onChanged.addListener((changes, namespace) => {
|
2020-01-10 02:09:32 +01:00
|
|
|
for (const key in changes) {
|
2020-01-09 19:17:26 +01:00
|
|
|
SB.localConfig[key] = decodeStoredItem(changes[key].newValue);
|
2019-12-31 21:07:43 +01:00
|
|
|
}
|
2020-01-10 02:09:32 +01:00
|
|
|
|
|
|
|
for (const callback of SB.configListeners) {
|
|
|
|
callback(changes);
|
|
|
|
}
|
2019-12-31 21:07:43 +01:00
|
|
|
});
|
2020-01-09 17:39:23 +01:00
|
|
|
|
2019-12-31 21:07:43 +01:00
|
|
|
var handler = {
|
2020-01-11 20:42:35 +01:00
|
|
|
set(obj, prop, value) {
|
2020-01-09 19:17:26 +01:00
|
|
|
SB.localConfig[prop] = value;
|
2020-01-09 19:16:27 +01:00
|
|
|
|
2019-12-31 21:07:43 +01:00
|
|
|
chrome.storage.sync.set({
|
2020-01-09 18:46:04 +01:00
|
|
|
[prop]: encodeStoredItem(value)
|
2020-01-08 04:59:50 +01:00
|
|
|
});
|
2019-12-31 21:07:43 +01:00
|
|
|
},
|
2020-01-11 20:42:35 +01:00
|
|
|
|
|
|
|
get(obj, prop) {
|
2020-01-09 19:17:26 +01:00
|
|
|
let data = SB.localConfig[prop];
|
2020-01-09 18:38:15 +01:00
|
|
|
if(data instanceof Map) data = new MapIO(prop);
|
|
|
|
|
2020-01-11 20:42:35 +01:00
|
|
|
return obj[prop] || data;
|
|
|
|
},
|
|
|
|
|
|
|
|
deleteProperty(obj, prop) {
|
2020-01-11 20:50:11 +01:00
|
|
|
chrome.storage.sync.remove(prop);
|
2019-12-31 21:07:43 +01:00
|
|
|
}
|
2020-01-08 20:52:41 +01:00
|
|
|
|
2019-12-31 21:07:43 +01:00
|
|
|
};
|
2020-01-08 04:59:50 +01:00
|
|
|
|
2020-01-09 18:30:09 +01:00
|
|
|
return new Proxy({handler}, handler);
|
2019-12-31 21:07:43 +01:00
|
|
|
}
|
|
|
|
|
2020-01-09 18:46:04 +01:00
|
|
|
function fetchConfig() {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
chrome.storage.sync.get(null, function(items) {
|
2020-01-09 19:17:26 +01:00
|
|
|
SB.localConfig = items; // Data is ready
|
2020-01-09 18:46:04 +01:00
|
|
|
resolve();
|
|
|
|
});
|
2019-12-31 21:07:43 +01:00
|
|
|
});
|
2020-01-09 18:46:04 +01:00
|
|
|
}
|
2019-12-31 21:07:43 +01:00
|
|
|
|
2020-01-09 18:46:04 +01:00
|
|
|
function migrateOldFormats() { // Convert sponsorTimes format
|
2020-01-11 20:50:11 +01:00
|
|
|
for (const key in SB.localConfig) {
|
2020-01-07 00:12:48 +01:00
|
|
|
if (key.startsWith("sponsorTimes") && key !== "sponsorTimes" && key !== "sponsorTimesContributed") {
|
2020-01-06 22:17:28 +01:00
|
|
|
SB.config.sponsorTimes.set(key.substr(12), SB.config[key]);
|
2020-01-11 20:42:35 +01:00
|
|
|
delete SB.config[key];
|
2020-01-06 22:11:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-09 18:46:04 +01:00
|
|
|
async function setupConfig() {
|
2019-12-31 21:07:43 +01:00
|
|
|
await fetchConfig();
|
2020-01-11 20:42:35 +01:00
|
|
|
addDefaults();
|
|
|
|
convertJSON();
|
|
|
|
SB.config = configProxy();
|
2020-01-09 18:46:04 +01:00
|
|
|
migrateOldFormats();
|
2019-12-31 21:07:43 +01:00
|
|
|
}
|
|
|
|
|
2019-12-31 23:46:16 +01:00
|
|
|
SB.defaults = {
|
2020-01-11 20:42:35 +01:00
|
|
|
"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,
|
2020-01-10 03:23:25 +01:00
|
|
|
"hideDiscordLink": false,
|
2020-01-11 02:06:16 +01:00
|
|
|
"invidiousInstances": ["invidio.us", "invidiou.sh", "invidious.snopyta.org"],
|
2020-01-11 04:15:28 +01:00
|
|
|
"invidiousUpdateInfoShowCount": 0,
|
2020-01-11 02:06:16 +01:00
|
|
|
"autoUpvote": true
|
2019-12-31 23:46:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reset config
|
|
|
|
function resetConfig() {
|
2020-01-11 20:42:35 +01:00
|
|
|
SB.config = SB.defaults;
|
2019-12-31 23:46:16 +01:00
|
|
|
};
|
|
|
|
|
2020-01-09 18:46:04 +01:00
|
|
|
function convertJSON() {
|
2020-01-11 20:42:35 +01:00
|
|
|
Object.keys(SB.defaults).forEach(key => {
|
|
|
|
SB.localConfig[key] = decodeStoredItem(SB.localConfig[key], key);
|
|
|
|
});
|
2020-01-09 17:39:23 +01:00
|
|
|
}
|
2020-01-09 18:46:04 +01:00
|
|
|
|
2019-12-31 23:46:16 +01:00
|
|
|
// Add defaults
|
|
|
|
function addDefaults() {
|
2020-01-11 19:27:20 +01:00
|
|
|
for (const key in SB.defaults) {
|
|
|
|
if(!SB.localConfig.hasOwnProperty(key)) {
|
2020-01-11 20:42:35 +01:00
|
|
|
SB.localConfig[key] = SB.defaults[key];
|
|
|
|
}
|
2020-01-11 19:27:20 +01:00
|
|
|
}
|
2019-12-31 23:46:16 +01:00
|
|
|
};
|
|
|
|
|
2019-12-31 21:07:43 +01:00
|
|
|
// Sync config
|
2020-01-09 18:46:04 +01:00
|
|
|
setupConfig();
|