SponsorBlock/SB.js

77 lines
1.7 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) {
SB.localconfig[key] = changes[key].newValue;
}
});
var handler = {
set: function(obj, prop, value) {
chrome.storage.sync.set({
[prop]: value
})
},
get: function(obj, prop) {
return SB.localconfig[prop]
}
};
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-06 22:13:50 +01:00
if (key.startsWith("sponsorTimes")) {
2020-01-06 22:14:31 +01:00
SB.config.sponsorTimes.set(key.substr(11), 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();
2020-01-06 22:11:37 +01:00
migrate();
2019-12-31 21:07:43 +01:00
SB.config = configProxy();
}
2019-12-31 23:46:16 +01:00
SB.defaults = {
2020-01-01 20:12:15 +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-01 13:35:54 +01:00
"trackViewCount": false,
2020-01-01 15:04:49 +01:00
"dontShowNotice": false,
"hideVideoPlayerControls": false,
"hideInfoButtonPlayerControls": false,
2020-01-06 19:59:38 +01:00
"hideDeleteButtonPlayerControls": 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();