2020-02-09 03:30:57 +01:00
|
|
|
import * as CompileConfig from "../config.json";
|
|
|
|
|
2020-02-03 20:11:52 +01:00
|
|
|
interface SBConfig {
|
2020-02-04 08:10:59 +01:00
|
|
|
userID: string,
|
2020-02-03 20:11:52 +01:00
|
|
|
sponsorTimes: SBMap<string, any>,
|
2020-02-04 08:10:59 +01:00
|
|
|
whitelistedChannels: Array<any>,
|
2020-02-03 20:11:52 +01:00
|
|
|
startSponsorKeybind: string,
|
|
|
|
submitKeybind: string,
|
|
|
|
minutesSaved: number,
|
|
|
|
skipCount: number,
|
|
|
|
sponsorTimesContributed: number,
|
|
|
|
disableSkipping: boolean,
|
|
|
|
disableAutoSkip: boolean,
|
|
|
|
trackViewCount: boolean,
|
|
|
|
dontShowNotice: boolean,
|
|
|
|
hideVideoPlayerControls: boolean,
|
|
|
|
hideInfoButtonPlayerControls: boolean,
|
|
|
|
hideDeleteButtonPlayerControls: boolean,
|
2020-02-09 04:18:03 +01:00
|
|
|
hideUploadButtonPlayerControls: boolean,
|
2020-02-03 20:11:52 +01:00
|
|
|
hideDiscordLaunches: number,
|
|
|
|
hideDiscordLink: boolean,
|
|
|
|
invidiousInstances: string[],
|
|
|
|
invidiousUpdateInfoShowCount: number,
|
2020-02-04 08:10:59 +01:00
|
|
|
autoUpvote: boolean,
|
2020-02-09 01:31:18 +01:00
|
|
|
supportInvidious: boolean,
|
2020-02-09 03:30:57 +01:00
|
|
|
serverAddress: string,
|
2020-02-09 04:43:27 +01:00
|
|
|
minDuration: number,
|
|
|
|
checkForUnlistedVideos: boolean
|
2020-02-03 20:11:52 +01:00
|
|
|
}
|
|
|
|
|
2020-01-29 04:16:48 +01:00
|
|
|
interface SBObject {
|
|
|
|
configListeners: Array<Function>;
|
2020-02-03 20:11:52 +01:00
|
|
|
defaults: SBConfig;
|
2020-02-04 08:10:59 +01:00
|
|
|
localConfig: SBConfig;
|
|
|
|
config: SBConfig;
|
2020-01-29 04:16:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Allows a SBMap to be conveted into json form
|
|
|
|
// Currently used for local storage
|
|
|
|
class SBMap<T, U> extends Map {
|
2020-02-04 08:10:59 +01:00
|
|
|
id: string;
|
|
|
|
|
|
|
|
constructor(id: string, entries?: [T, U][]) {
|
2020-02-01 22:53:33 +01:00
|
|
|
super();
|
|
|
|
|
2020-02-04 08:10:59 +01:00
|
|
|
this.id = id;
|
|
|
|
|
2020-02-01 22:53:33 +01:00
|
|
|
// Import all entries if they were given
|
|
|
|
if (entries !== undefined) {
|
|
|
|
for (const item of entries) {
|
2020-02-09 16:34:18 +01:00
|
|
|
super.set(item[0], item[1])
|
2020-02-01 22:53:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-04 08:10:59 +01:00
|
|
|
set(key, value) {
|
|
|
|
const result = super.set(key, value);
|
|
|
|
|
|
|
|
// Store updated SBMap locally
|
|
|
|
chrome.storage.sync.set({
|
|
|
|
[this.id]: encodeStoredItem(this)
|
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(key) {
|
|
|
|
const result = super.delete(key);
|
|
|
|
|
|
|
|
// Store updated SBMap locally
|
|
|
|
chrome.storage.sync.set({
|
|
|
|
[this.id]: encodeStoredItem(this)
|
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
clear() {
|
|
|
|
const result = super.clear();
|
|
|
|
|
|
|
|
chrome.storage.sync.set({
|
|
|
|
[this.id]: encodeStoredItem(this)
|
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-01-29 04:16:48 +01:00
|
|
|
toJSON() {
|
|
|
|
return Array.from(this.entries());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-04 08:10:59 +01:00
|
|
|
var Config: SBObject = {
|
2020-01-10 02:09:32 +01:00
|
|
|
/**
|
|
|
|
* Callback function when an option is updated
|
|
|
|
*/
|
2020-01-29 04:16:48 +01:00
|
|
|
configListeners: [],
|
|
|
|
defaults: {
|
2020-02-04 08:10:59 +01:00
|
|
|
userID: null,
|
|
|
|
sponsorTimes: new SBMap("sponsorTimes"),
|
|
|
|
whitelistedChannels: [],
|
2020-02-03 20:11:52 +01:00
|
|
|
startSponsorKeybind: ";",
|
|
|
|
submitKeybind: "'",
|
|
|
|
minutesSaved: 0,
|
|
|
|
skipCount: 0,
|
|
|
|
sponsorTimesContributed: 0,
|
|
|
|
disableSkipping: false,
|
|
|
|
disableAutoSkip: false,
|
|
|
|
trackViewCount: true,
|
|
|
|
dontShowNotice: false,
|
|
|
|
hideVideoPlayerControls: false,
|
|
|
|
hideInfoButtonPlayerControls: false,
|
|
|
|
hideDeleteButtonPlayerControls: false,
|
2020-02-09 04:18:03 +01:00
|
|
|
hideUploadButtonPlayerControls: false,
|
2020-02-03 20:11:52 +01:00
|
|
|
hideDiscordLaunches: 0,
|
|
|
|
hideDiscordLink: false,
|
|
|
|
invidiousInstances: ["invidio.us", "invidiou.sh", "invidious.snopyta.org"],
|
|
|
|
invidiousUpdateInfoShowCount: 0,
|
2020-02-04 08:10:59 +01:00
|
|
|
autoUpvote: true,
|
2020-02-09 01:10:04 +01:00
|
|
|
supportInvidious: false,
|
2020-02-09 03:30:57 +01:00
|
|
|
serverAddress: CompileConfig.serverAddress,
|
2020-02-09 04:43:27 +01:00
|
|
|
minDuration: 0,
|
|
|
|
checkForUnlistedVideos: false
|
2020-01-29 04:16:48 +01:00
|
|
|
},
|
2020-02-02 01:18:53 +01:00
|
|
|
localConfig: null,
|
|
|
|
config: null
|
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-29 04:16:48 +01:00
|
|
|
* A SBMap cannot be stored in the chrome storage.
|
2020-01-09 18:46:04 +01:00
|
|
|
* This data will be encoded into an array instead as specified by the toJSON function.
|
|
|
|
*
|
2020-02-02 01:18:53 +01:00
|
|
|
* @param data
|
2020-01-09 18:46:04 +01:00
|
|
|
*/
|
|
|
|
function encodeStoredItem(data) {
|
2020-01-29 04:16:48 +01:00
|
|
|
// if data is SBMap convert to json for storing
|
|
|
|
if(!(data instanceof SBMap)) return data;
|
2020-01-16 19:27:29 +01:00
|
|
|
return JSON.stringify(data);
|
2020-01-09 18:12:49 +01:00
|
|
|
}
|
|
|
|
|
2020-01-09 18:38:15 +01:00
|
|
|
/**
|
2020-02-04 08:10:59 +01:00
|
|
|
* An SBMap cannot be stored in the chrome storage.
|
2020-01-09 18:38:15 +01:00
|
|
|
* This data will be decoded from the array it is stored in
|
|
|
|
*
|
|
|
|
* @param {*} data
|
|
|
|
*/
|
2020-02-04 08:10:59 +01:00
|
|
|
function decodeStoredItem(id: string, 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;
|
2020-02-04 08:10:59 +01:00
|
|
|
return new SBMap(id, 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
|
|
|
}
|
|
|
|
|
2020-02-02 01:25:40 +01:00
|
|
|
function configProxy(): any {
|
2019-12-31 21:07:43 +01:00
|
|
|
chrome.storage.onChanged.addListener((changes, namespace) => {
|
2020-01-10 02:09:32 +01:00
|
|
|
for (const key in changes) {
|
2020-02-04 08:10:59 +01:00
|
|
|
Config.localConfig[key] = decodeStoredItem(key, changes[key].newValue);
|
2019-12-31 21:07:43 +01:00
|
|
|
}
|
2020-01-10 02:09:32 +01:00
|
|
|
|
2020-02-04 08:10:59 +01:00
|
|
|
for (const callback of Config.configListeners) {
|
2020-01-10 02:09:32 +01:00
|
|
|
callback(changes);
|
|
|
|
}
|
2019-12-31 21:07:43 +01:00
|
|
|
});
|
2020-01-09 17:39:23 +01:00
|
|
|
|
2020-01-29 04:16:48 +01:00
|
|
|
var handler: ProxyHandler<any> = {
|
2020-01-11 20:42:35 +01:00
|
|
|
set(obj, prop, value) {
|
2020-02-04 08:10:59 +01:00
|
|
|
Config.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
|
|
|
});
|
2020-01-29 04:16:48 +01:00
|
|
|
|
|
|
|
return true;
|
2019-12-31 21:07:43 +01:00
|
|
|
},
|
2020-01-11 20:42:35 +01:00
|
|
|
|
2020-01-29 04:16:48 +01:00
|
|
|
get(obj, prop): any {
|
2020-02-04 08:10:59 +01:00
|
|
|
let data = Config.localConfig[prop];
|
2020-01-09 18:38:15 +01:00
|
|
|
|
2020-01-11 20:42:35 +01:00
|
|
|
return obj[prop] || data;
|
|
|
|
},
|
|
|
|
|
|
|
|
deleteProperty(obj, prop) {
|
2020-01-29 04:16:48 +01:00
|
|
|
chrome.storage.sync.remove(<string> prop);
|
|
|
|
|
|
|
|
return true;
|
2019-12-31 21:07:43 +01:00
|
|
|
}
|
2020-01-16 19:43:49 +01:00
|
|
|
|
2019-12-31 21:07:43 +01:00
|
|
|
};
|
2020-01-08 04:59:50 +01:00
|
|
|
|
2020-02-03 20:11:52 +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-02-04 08:10:59 +01:00
|
|
|
Config.localConfig = <SBConfig> <unknown> 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-02-04 08:10:59 +01:00
|
|
|
for (const key in Config.localConfig) {
|
2020-01-07 00:12:48 +01:00
|
|
|
if (key.startsWith("sponsorTimes") && key !== "sponsorTimes" && key !== "sponsorTimesContributed") {
|
2020-02-04 08:10:59 +01:00
|
|
|
Config.config.sponsorTimes.set(key.substr(12), Config.config[key]);
|
|
|
|
delete Config.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();
|
2020-02-04 08:10:59 +01:00
|
|
|
Config.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
|
|
|
// Reset config
|
|
|
|
function resetConfig() {
|
2020-02-04 08:10:59 +01:00
|
|
|
Config.config = Config.defaults;
|
2019-12-31 23:46:16 +01:00
|
|
|
};
|
|
|
|
|
2020-01-09 18:46:04 +01:00
|
|
|
function convertJSON() {
|
2020-02-09 02:05:31 +01:00
|
|
|
Object.keys(Config.localConfig).forEach(key => {
|
2020-02-04 08:10:59 +01:00
|
|
|
Config.localConfig[key] = decodeStoredItem(key, Config.localConfig[key]);
|
2020-01-11 20:42:35 +01:00
|
|
|
});
|
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-02-04 08:10:59 +01:00
|
|
|
for (const key in Config.defaults) {
|
|
|
|
if(!Config.localConfig.hasOwnProperty(key)) {
|
|
|
|
Config.localConfig[key] = Config.defaults[key];
|
2020-01-11 20:42:35 +01:00
|
|
|
}
|
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();
|
2020-01-29 04:16:48 +01:00
|
|
|
|
2020-02-04 08:10:59 +01:00
|
|
|
export default Config;
|