mirror of
https://github.com/ajayyy/SponsorBlock.git
synced 2024-09-20 04:53:43 +02:00
commit
1ff7e69556
1 changed files with 33 additions and 24 deletions
57
SB.js
57
SB.js
|
@ -9,39 +9,37 @@ SB = {
|
||||||
|
|
||||||
// Function setup
|
// Function setup
|
||||||
|
|
||||||
|
// Allows a map to be conveted into json form
|
||||||
|
// Currently used for local storage
|
||||||
Map.prototype.toJSON = function() {
|
Map.prototype.toJSON = function() {
|
||||||
return Array.from(this.entries());
|
return Array.from(this.entries());
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Proxy Map changes to Map in SB.localConfig
|
||||||
|
// Saves the changes to chrome.storage in json form
|
||||||
class MapIO {
|
class MapIO {
|
||||||
constructor(id) {
|
constructor(id) {
|
||||||
this.id = id;
|
// The name of the item in the array
|
||||||
this.map = SB.localConfig[this.id];
|
this.id = id;
|
||||||
|
// A local copy of the map (SB.config.mapname.map)
|
||||||
|
this.map = SB.localConfig[this.id];
|
||||||
}
|
}
|
||||||
|
|
||||||
set(key, value) {
|
set(key, value) {
|
||||||
|
// Proxy to map
|
||||||
this.map.set(key, value);
|
this.map.set(key, value);
|
||||||
|
// Store updated map locally
|
||||||
SB.config.handler.set(undefined, this.id, encodeStoredItem(this.map));
|
chrome.storage.sync.set({
|
||||||
|
[this.id]: encodeStoredItem(this.map)
|
||||||
|
});
|
||||||
return this.map;
|
return this.map;
|
||||||
}
|
}
|
||||||
|
|
||||||
get(key) {
|
get(key) {
|
||||||
return this.map.get(key);
|
return this.map.get(key);
|
||||||
}
|
|
||||||
|
|
||||||
has(key) {
|
|
||||||
return this.map.has(key);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
deleteProperty(key) {
|
has(key) {
|
||||||
if (this.map.has(key)) {
|
return this.map.has(key);
|
||||||
this.map.delete(key);
|
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
size() {
|
size() {
|
||||||
|
@ -49,9 +47,19 @@ class MapIO {
|
||||||
}
|
}
|
||||||
|
|
||||||
delete(key) {
|
delete(key) {
|
||||||
|
// Proxy to map
|
||||||
this.map.delete(key);
|
this.map.delete(key);
|
||||||
|
// Store updated map locally
|
||||||
|
chrome.storage.sync.set({
|
||||||
|
[this.id]: encodeStoredItem(this.map)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
SB.config.handler.set(undefined, this.id, encodeStoredItem(this.map));
|
clear() {
|
||||||
|
this.map.clear();
|
||||||
|
chrome.storage.sync.set({
|
||||||
|
[this.id]: encodeStoredItem(this.map)
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -62,8 +70,9 @@ class MapIO {
|
||||||
* @param {*} data
|
* @param {*} data
|
||||||
*/
|
*/
|
||||||
function encodeStoredItem(data) {
|
function encodeStoredItem(data) {
|
||||||
if(!(data instanceof Map)) return data;
|
// if data is Map convert to json for storing
|
||||||
return JSON.stringify(data);
|
if(!(data instanceof Map)) return data;
|
||||||
|
return JSON.stringify(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -115,9 +124,9 @@ function configProxy() {
|
||||||
},
|
},
|
||||||
|
|
||||||
deleteProperty(obj, prop) {
|
deleteProperty(obj, prop) {
|
||||||
chrome.storage.sync.remove(prop);
|
chrome.storage.sync.remove(prop);
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return new Proxy({handler}, handler);
|
return new Proxy({handler}, handler);
|
||||||
|
|
Loading…
Reference in a new issue