mirror of
https://github.com/ajayyy/SponsorBlock.git
synced 2025-12-10 13:37:04 +03:00
Switched to a listener map.
This commit is contained in:
59
SB.js
59
SB.js
@@ -1,21 +1,69 @@
|
|||||||
SB = {};
|
SB = {};
|
||||||
|
|
||||||
|
class ListenerMap extends Map {
|
||||||
|
constructor(name) {
|
||||||
|
super();
|
||||||
|
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
set(key, value) {
|
||||||
|
super.set(key, value);
|
||||||
|
|
||||||
|
this.updateListener(this.name, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
delete(key) {
|
||||||
|
this.updateListener(this.name, this);
|
||||||
|
|
||||||
|
return super.set(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
clear() {
|
||||||
|
return super.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
forEach(callbackfn) {
|
||||||
|
return super.forEach(callbackfn);
|
||||||
|
}
|
||||||
|
|
||||||
|
get(key) {
|
||||||
|
return super.get(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
has(key) {
|
||||||
|
return super.has(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function mapHandler(name, object) {
|
||||||
|
SB.config[name] = SB.config[name];
|
||||||
|
// chrome.storage.sync.set({
|
||||||
|
// [name]: object
|
||||||
|
// });
|
||||||
|
|
||||||
|
// console.log(name)
|
||||||
|
// console.log(object)
|
||||||
|
}
|
||||||
|
|
||||||
function configProxy() {
|
function configProxy() {
|
||||||
chrome.storage.onChanged.addListener((changes, namespace) => {
|
chrome.storage.onChanged.addListener((changes, namespace) => {
|
||||||
for (key in changes) {
|
for (key in changes) {
|
||||||
SB.localconfig[key] = changes[key].newValue;
|
SB.localconfig[key] = changes[key].newValue;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
var handler = {
|
var handler = {
|
||||||
set: function(obj, prop, value) {
|
set: function(obj, prop, value) {
|
||||||
chrome.storage.sync.set({
|
chrome.storage.sync.set({
|
||||||
[prop]: value
|
[prop]: value
|
||||||
})
|
});
|
||||||
},
|
},
|
||||||
get: function(obj, prop) {
|
get: function(obj, prop) {
|
||||||
return SB.localconfig[prop]
|
return SB.localconfig[prop];
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return new Proxy({}, handler);
|
return new Proxy({}, handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -38,12 +86,17 @@ function migrate() { // Convert sponsorTimes format
|
|||||||
async function config() {
|
async function config() {
|
||||||
await fetchConfig();
|
await fetchConfig();
|
||||||
addDefaults();
|
addDefaults();
|
||||||
|
// Setup sponsorTime listener
|
||||||
|
SB.localconfig.sponsorTimes.updateListener = mapHandler;
|
||||||
|
|
||||||
SB.config = configProxy();
|
SB.config = configProxy();
|
||||||
migrate();
|
migrate();
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SB.defaults = {
|
SB.defaults = {
|
||||||
"sponsorTimes": new Map(),
|
"sponsorTimes": new ListenerMap("sponsorTimes"),
|
||||||
"startSponsorKeybind": ";",
|
"startSponsorKeybind": ";",
|
||||||
"submitKeybind": "'",
|
"submitKeybind": "'",
|
||||||
"minutesSaved": 0,
|
"minutesSaved": 0,
|
||||||
|
|||||||
@@ -4,7 +4,9 @@ chrome.tabs.onUpdated.addListener(function(tabId) {
|
|||||||
}, () => void chrome.runtime.lastError ); // Suppress error on Firefox
|
}, () => void chrome.runtime.lastError ); // Suppress error on Firefox
|
||||||
});
|
});
|
||||||
|
|
||||||
chrome.runtime.onMessage.addListener(function (request, sender, callback) {
|
chrome.runtime.onMessage.addListener(async function (request, sender, callback) {
|
||||||
|
await wait(() => SB.config !== undefined);
|
||||||
|
|
||||||
switch(request.message) {
|
switch(request.message) {
|
||||||
case "submitTimes":
|
case "submitTimes":
|
||||||
submitTimes(request.videoID, callback);
|
submitTimes(request.videoID, callback);
|
||||||
|
|||||||
4
popup.js
4
popup.js
@@ -21,6 +21,8 @@ async function runThePopup() {
|
|||||||
inPopup = false;
|
inPopup = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await wait(() => SB.config !== undefined);
|
||||||
|
|
||||||
["sponsorStart",
|
["sponsorStart",
|
||||||
// Top toggles
|
// Top toggles
|
||||||
"whitelistChannel",
|
"whitelistChannel",
|
||||||
@@ -229,6 +231,8 @@ async function runThePopup() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//load video times for this video
|
//load video times for this video
|
||||||
|
console.log( SB.config.sponsorTimes.set)
|
||||||
|
setTimeout(()=> console.log( SB.config.sponsorTimes.set), 200 )
|
||||||
let sponsorTimesStorage = SB.config.sponsorTimes.get(currentVideoID);
|
let sponsorTimesStorage = SB.config.sponsorTimes.get(currentVideoID);
|
||||||
if (sponsorTimesStorage != undefined && sponsorTimesStorage.length > 0) {
|
if (sponsorTimesStorage != undefined && sponsorTimesStorage.length > 0) {
|
||||||
if (sponsorTimesStorage[sponsorTimesStorage.length - 1] != undefined && sponsorTimesStorage[sponsorTimesStorage.length - 1].length < 2) {
|
if (sponsorTimesStorage[sponsorTimesStorage.length - 1] != undefined && sponsorTimesStorage[sponsorTimesStorage.length - 1].length < 2) {
|
||||||
|
|||||||
Reference in New Issue
Block a user