mirror of
https://github.com/ajayyy/SponsorBlock.git
synced 2025-12-19 14:09:09 +03:00
Map support :D
This commit is contained in:
95
SB.js
95
SB.js
@@ -9,60 +9,77 @@ function storeEncode(data) {
|
|||||||
return JSON.stringify(data);
|
return JSON.stringify(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
function strParser(data) {
|
function mapDecode(data, key) {
|
||||||
|
if(typeof data !== "string") return data;
|
||||||
try {
|
try {
|
||||||
return new Map(JSON.parse(data));
|
let str = JSON.parse(data);
|
||||||
|
if(!Array.isArray(str)) return data;
|
||||||
|
return new Map(str);
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class ListenerMap extends Map {
|
function mapProxy(data, key) {
|
||||||
constructor(name) {
|
if(!(data instanceof Map)) return data;
|
||||||
super();
|
return new mapIO(key);
|
||||||
|
}
|
||||||
|
|
||||||
this.name = name;
|
class mapIO extends Map {
|
||||||
|
constructor(id) {
|
||||||
|
super();
|
||||||
|
this.id = id;
|
||||||
|
this.map = SB.localconfig[this.id];
|
||||||
}
|
}
|
||||||
|
|
||||||
set(key, value) {
|
set(key, value) {
|
||||||
super.set(key, value);
|
SB.localconfig[this.id].set(key, value);
|
||||||
|
chrome.storage.sync.set({
|
||||||
this.updateListener(this.name, this);
|
[this.id]: storeEncode(this.map)
|
||||||
|
});
|
||||||
|
return this.map
|
||||||
}
|
}
|
||||||
|
|
||||||
delete(key) {
|
get(key) {
|
||||||
this.updateListener(this.name, this);
|
return this.map.get(key)
|
||||||
|
|
||||||
return super.set(key);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
clear() {
|
has(key) {
|
||||||
return super.clear();
|
return this.map.has(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
forEach(callbackfn) {
|
toJSON() {
|
||||||
return super.forEach(callbackfn);
|
return Array.from(this.map.entries())
|
||||||
}
|
}
|
||||||
|
|
||||||
get(key) {
|
deleteProperty(key) {
|
||||||
return strParser(super.get(key));
|
if (this.map.has(key)) {
|
||||||
|
this.map.delete(key);
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
size() {
|
||||||
|
return this.map.size
|
||||||
}
|
}
|
||||||
|
|
||||||
has(key) {
|
delete(key) {
|
||||||
return super.has(key);
|
this.map.delete(key);
|
||||||
|
chrome.storage.sync.set({
|
||||||
|
[this.id]: storeEncode(this.map)
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function mapHandler(name, object) {
|
|
||||||
SB.config[name] = storeEncode(object.value);
|
|
||||||
}
|
|
||||||
|
|
||||||
function configProxy() {
|
function configProxy() {
|
||||||
chrome.storage.onChanged.addListener((changes, namespace) => {
|
chrome.storage.onChanged.addListener((changes, namespace) => {
|
||||||
for (key in changes) {
|
for (key in changes) {
|
||||||
Reflect.set(SB.localconfig, key, changes[key].newValue);
|
Reflect.set(SB.localconfig, key, mapDecode(changes[key].newValue, key));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
var handler = {
|
var handler = {
|
||||||
set: function(obj, prop, value) {
|
set: function(obj, prop, value) {
|
||||||
chrome.storage.sync.set({
|
chrome.storage.sync.set({
|
||||||
@@ -70,7 +87,7 @@ function configProxy() {
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
get: function(obj, prop) {
|
get: function(obj, prop) {
|
||||||
return strParser(Reflect.get(SB.localconfig, prop));
|
return mapProxy(Reflect.get(SB.localconfig, prop), prop);
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
@@ -96,15 +113,14 @@ function migrate() { // Convert sponsorTimes format
|
|||||||
|
|
||||||
async function config() {
|
async function config() {
|
||||||
await fetchConfig();
|
await fetchConfig();
|
||||||
addDefaults();
|
addDefaults();
|
||||||
// Setup sponsorTime listener
|
convertJson();
|
||||||
SB.localconfig.sponsorTimes.updateListener = mapHandler;
|
SB.config = configProxy();
|
||||||
SB.config = configProxy();
|
|
||||||
migrate();
|
migrate();
|
||||||
}
|
}
|
||||||
|
|
||||||
SB.defaults = {
|
SB.defaults = {
|
||||||
"sponsorTimes": new ListenerMap("sponsorTimes"),
|
"sponsorTimes": new Map(),
|
||||||
"startSponsorKeybind": ";",
|
"startSponsorKeybind": ";",
|
||||||
"submitKeybind": "'",
|
"submitKeybind": "'",
|
||||||
"minutesSaved": 0,
|
"minutesSaved": 0,
|
||||||
@@ -126,6 +142,11 @@ function resetConfig() {
|
|||||||
SB.config = SB.defaults;
|
SB.config = SB.defaults;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function convertJson() {
|
||||||
|
Object.keys(SB.defaults).forEach(key => {
|
||||||
|
SB.localconfig[key] = mapDecode(SB.localconfig[key], key);
|
||||||
|
});
|
||||||
|
}
|
||||||
// Add defaults
|
// Add defaults
|
||||||
function addDefaults() {
|
function addDefaults() {
|
||||||
Object.keys(SB.defaults).forEach(key => {
|
Object.keys(SB.defaults).forEach(key => {
|
||||||
|
|||||||
Reference in New Issue
Block a user