SponsorBlock/SB.js

163 lines
3.3 KiB
JavaScript
Raw Normal View History

2019-12-31 21:07:43 +01:00
SB = {};
2020-01-08 23:22:18 +01:00
Map.prototype.toJSON = function() {
return Array.from(this.entries());
};
class MapIO {
2020-01-09 17:39:23 +01:00
constructor(id) {
this.id = id;
this.map = SB.localconfig[this.id];
2020-01-09 00:16:02 +01:00
}
set(key, value) {
2020-01-09 18:30:09 +01:00
this.map.set(key, value);
SB.config.handler.set(undefined, this.id, storeEncode(this.map));
return this.map;
2020-01-09 00:16:02 +01:00
}
2020-01-09 17:39:23 +01:00
get(key) {
2020-01-09 18:30:09 +01:00
return this.map.get(key);
2020-01-09 00:16:02 +01:00
}
2020-01-09 17:39:23 +01:00
has(key) {
2020-01-09 18:30:09 +01:00
return this.map.has(key);
2020-01-09 00:16:02 +01:00
}
2020-01-09 17:39:23 +01:00
deleteProperty(key) {
if (this.map.has(key)) {
this.map.delete(key);
return true;
} else {
return false;
}
}
size() {
2020-01-09 18:30:09 +01:00
return this.map.size;
2020-01-09 00:16:02 +01:00
}
2020-01-09 17:39:23 +01:00
delete(key) {
this.map.delete(key);
2020-01-09 18:30:09 +01:00
SB.config.handler.set(undefined, this.id, storeEncode(this.map));
2020-01-09 00:16:02 +01:00
}
2020-01-09 18:12:49 +01:00
}
function storeEncode(data) {
if(!(data instanceof Map)) return data;
return JSON.stringify(data);
}
/**
* 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-09 18:12:49 +01:00
try {
let str = JSON.parse(data);
2020-01-09 18:12:49 +01:00
if(!Array.isArray(str)) return data;
return new Map(str);
} catch(e) {
// 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) => {
for (key in changes) {
SB.localconfig[key] = decodeStoredItem(changes[key].newValue);
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 = {
set: function(obj, prop, value) {
chrome.storage.sync.set({
2020-01-08 23:22:18 +01:00
[prop]: storeEncode(value)
2020-01-08 04:59:50 +01:00
});
2019-12-31 21:07:43 +01:00
},
get: function(obj, prop) {
let data = SB.localconfig[prop];
if(data instanceof Map) data = new MapIO(prop);
return obj[prop] || data;
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-06 19:56:37 +01:00
fetchConfig = () => new Promise((resolve, reject) => {
2019-12-31 21:07:43 +01:00
chrome.storage.sync.get(null, function(items) {
SB.localconfig = items; // Data is ready
resolve();
});
});
2020-01-06 22:11:37 +01:00
function migrate() { // Convert sponsorTimes format
for (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-06 22:11:37 +01:00
delete SB.config[key];
}
}
}
2019-12-31 21:07:43 +01:00
async function config() {
await fetchConfig();
2020-01-09 17:39:23 +01:00
addDefaults();
convertJson();
SB.config = configProxy();
2020-01-08 04:59:50 +01:00
migrate();
2019-12-31 21:07:43 +01:00
}
2019-12-31 23:46:16 +01:00
SB.defaults = {
2020-01-09 17:39:23 +01:00
"sponsorTimes": new Map(),
2019-12-31 23:46:16 +01:00
"startSponsorKeybind": ";",
"submitKeybind": "'",
"minutesSaved": 0,
2019-12-31 23:48:29 +01:00
"skipCount": 0,
2020-01-01 15:45:22 +01:00
"sponsorTimesContributed": 0,
2019-12-31 23:48:29 +01:00
"disableSkipping": false,
2019-12-31 23:54:02 +01:00
"disableAutoSkip": false,
2020-01-08 04:12:53 +01:00
"trackViewCount": true,
2020-01-01 15:04:49 +01:00
"dontShowNotice": false,
"hideVideoPlayerControls": false,
"hideInfoButtonPlayerControls": false,
2020-01-07 00:29:04 +01:00
"hideDeleteButtonPlayerControls": false,
2020-01-07 00:27:07 +01:00
"hideDiscordLaunches": 0,
"hideDiscordLink": false
2019-12-31 23:46:16 +01:00
}
// Reset config
function resetConfig() {
SB.config = SB.defaults;
};
2020-01-09 17:39:23 +01:00
function convertJson() {
Object.keys(SB.defaults).forEach(key => {
SB.localconfig[key] = decodeStoredItem(SB.localconfig[key], key);
2020-01-09 17:39:23 +01:00
});
}
2019-12-31 23:46:16 +01:00
// Add defaults
function addDefaults() {
Object.keys(SB.defaults).forEach(key => {
if(!SB.localconfig.hasOwnProperty(key)) {
2020-01-01 13:27:59 +01:00
SB.localconfig[key] = SB.defaults[key];
2019-12-31 23:46:16 +01:00
}
});
};
2019-12-31 21:07:43 +01:00
// Sync config
config();