SponsorBlock/SB.js

83 lines
1.8 KiB
JavaScript
Raw Normal View History

2019-12-31 21:07:43 +01:00
SB = {};
function configProxy() {
chrome.storage.onChanged.addListener((changes, namespace) => {
for (key in changes) {
2020-01-08 20:52:41 +01:00
Reflect.set(SB.localconfig, key, changes[key].newValue);
2019-12-31 21:07:43 +01:00
}
});
var handler = {
set: function(obj, prop, value) {
chrome.storage.sync.set({
[prop]: value
2020-01-08 04:59:50 +01:00
});
2019-12-31 21:07:43 +01:00
},
get: function(obj, prop) {
2020-01-08 20:52:41 +01:00
return Reflect.get(SB.localconfig, 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
2019-12-31 21:07:43 +01:00
return new Proxy({}, handler);
}
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-06 19:59:38 +01:00
addDefaults();
2019-12-31 21:07:43 +01:00
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-08 20:52:41 +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;
};
// 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();