Merge pull request #226 from ajayyy/ajay-config-listener

Config callback updating
This commit is contained in:
Ajay Ramachandran
2020-01-09 20:41:18 -05:00
committed by GitHub
4 changed files with 49 additions and 55 deletions

15
SB.js
View File

@@ -1,4 +1,11 @@
SB = {}; SB = {
/**
* Callback function when an option is updated
*
* @type {CallableFunction}
*/
configListeners: []
};
// Function setup // Function setup
@@ -82,9 +89,13 @@ function decodeStoredItem(data) {
function configProxy() { function configProxy() {
chrome.storage.onChanged.addListener((changes, namespace) => { chrome.storage.onChanged.addListener((changes, namespace) => {
for (key in changes) { for (const key in changes) {
SB.localConfig[key] = decodeStoredItem(changes[key].newValue); SB.localConfig[key] = decodeStoredItem(changes[key].newValue);
} }
for (const callback of SB.configListeners) {
callback(changes);
}
}); });
var handler = { var handler = {

View File

@@ -135,39 +135,33 @@ function messageListener(request, sender, sendResponse) {
channelWhitelisted = request.value; channelWhitelisted = request.value;
sponsorsLookup(sponsorVideoID); sponsorsLookup(sponsorVideoID);
break;
case "dontShowNotice":
SB.config.dontShowNotice = true;
break; break;
case "changeStartSponsorButton": case "changeStartSponsorButton":
changeStartSponsorButton(request.showStartSponsor, request.uploadButtonVisible); changeStartSponsorButton(request.showStartSponsor, request.uploadButtonVisible);
break; break;
}
}
case "showNoticeAgain": /**
SB.config.dontShowNotice = true; * Called when the config is updated
break; *
* @param {String} changes
case "changeVideoPlayerControlsVisibility": */
SB.config.hideVideoPlayerControls = request.value; function configUpdateListener(changes) {
updateVisibilityOfPlayerControlsButton(); for (const key in changes) {
switch(key) {
break; case "hideVideoPlayerControls":
case "changeInfoButtonPlayerControlsVisibility": case "hideInfoButtonPlayerControls":
SB.config.hideInfoButtonPlayerControls = request.value; case "hideDeleteButtonPlayerControls":
updateVisibilityOfPlayerControlsButton(); updateVisibilityOfPlayerControlsButton()
break;
case "changeDeleteButtonPlayerControlsVisibility":
SB.config.hideDeleteButtonPlayerControls = request.value;
updateVisibilityOfPlayerControlsButton();
break;
case "trackViewCount":
SB.config.trackViewCount = request.value;
break; break;
} }
}
}
if (!SB.configListeners.includes(configUpdateListener)) {
SB.configListeners.push(configUpdateListener);
} }
//check for hotkey pressed //check for hotkey pressed
@@ -454,8 +448,6 @@ function whitelistCheck() {
//see if this is a whitelisted channel //see if this is a whitelisted channel
let whitelistedChannels = SB.config.whitelistedChannels; let whitelistedChannels = SB.config.whitelistedChannels;
console.log(channelURL)
if (whitelistedChannels != undefined && whitelistedChannels.includes(channelURL)) { if (whitelistedChannels != undefined && whitelistedChannels.includes(channelURL)) {
channelWhitelisted = true; channelWhitelisted = true;
} }
@@ -576,13 +568,6 @@ function reskipSponsorTime(UUID) {
} }
} }
function removePlayerControlsButton() {
if (!sponsorVideoID) return;
document.getElementById("startSponsorButton").style.display = "none";
document.getElementById("submitButton").style.display = "none";
}
function createButton(baseID, title, callback, imageName, isDraggable=false) { function createButton(baseID, title, callback, imageName, isDraggable=false) {
if (document.getElementById(baseID + "Button") != null) return; if (document.getElementById(baseID + "Button") != null) return;
@@ -633,13 +618,20 @@ async function updateVisibilityOfPlayerControlsButton() {
await createButtons(); await createButtons();
if (SB.config.hideDeleteButtonPlayerControls) { if (SB.config.hideVideoPlayerControls) {
removePlayerControlsButton(); document.getElementById("startSponsorButton").style.display = "none";
document.getElementById("submitButton").style.display = "none";
} else {
document.getElementById("startSponsorButton").style.removeProperty("display");
} }
//don't show the info button on embeds //don't show the info button on embeds
if (SB.config.hideInfoButtonPlayerControls || document.URL.includes("/embed/")) { if (SB.config.hideInfoButtonPlayerControls || document.URL.includes("/embed/")) {
document.getElementById("infoButton").style.display = "none"; document.getElementById("infoButton").style.display = "none";
} else {
document.getElementById("infoButton").style.removeProperty("display");
} }
if (SB.config.hideDeleteButtonPlayerControls) { if (SB.config.hideDeleteButtonPlayerControls) {
document.getElementById("deleteButton").style.display = "none"; document.getElementById("deleteButton").style.display = "none";
} }

View File

@@ -800,15 +800,6 @@ async function runThePopup() {
function showNoticeAgain() { function showNoticeAgain() {
SB.config.dontShowNotice = false; SB.config.dontShowNotice = false;
chrome.tabs.query({
active: true,
currentWindow: true
}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {
message: "showNoticeAgain"
});
});
SB.showNoticeAgain.style.display = "none"; SB.showNoticeAgain.style.display = "none";
} }