Moved invidious code to functions and made it check permissions on init.

This commit is contained in:
Ajay Ramachandran
2019-12-31 02:15:10 -05:00
parent dcfdade927
commit df30f00347

View File

@@ -27,12 +27,75 @@ async function init() {
} }
} }
// See if anything extra should be run first time
switch (option) {
case "supportInvidious":
invidiousInit(checkbox, option);
break;
}
// Add click listener
checkbox.addEventListener("click", () =>{ checkbox.addEventListener("click", () =>{
setOptionValue(option, reverse ? !checkbox.checked : checkbox.checked); setOptionValue(option, reverse ? !checkbox.checked : checkbox.checked);
// See if anything extra must be run // See if anything extra must be run
switch (option) { switch (option) {
case "supportInvidious": case "supportInvidious":
invidiousOnClick(checkbox, option);
break;
}
});
checksLeft--;
});
checksLeft++;
break;
case "text-change":
let button = optionsElements[i].querySelector(".trigger-button");
button.addEventListener("click", () => activateTextChange(optionsElements[i]));
break;
case "keybind-change":
let keybindButton = optionsElements[i].querySelector(".trigger-button");
keybindButton.addEventListener("click", () => activateKeybindChange(optionsElements[i]));
break;
}
}
await wait(() => checksLeft == 0, 1000, 50);
optionsContainer.classList.remove("hidden");
optionsContainer.classList.add("animated");
}
/**
* Run when the invidious button is being initialized
*
* @param {HTMLElement} checkbox
* @param {string} option
*/
function invidiousInit(checkbox, option) {
chrome.permissions.contains({
origins: ["https://*.invidio.us/*", "https://*.invidiou.sh/*"],
permissions: ["declarativeContent"]
}, function (result) {
if (result != checkbox.checked) {
setOptionValue(option, result);
checkbox.checked = result;
}
});
}
/**
* Run whenever the invidious checkbox is clicked
*
* @param {HTMLElement} checkbox
* @param {string} option
*/
function invidiousOnClick(checkbox, option) {
if (checkbox.checked) { if (checkbox.checked) {
// Request permission // Request permission
chrome.permissions.request({ chrome.permissions.request({
@@ -85,33 +148,6 @@ async function init() {
origins: ["https://*.invidio.us/*"] origins: ["https://*.invidio.us/*"]
}); });
} }
break;
}
});
checksLeft--;
});
checksLeft++;
break;
case "text-change":
let button = optionsElements[i].querySelector(".trigger-button");
button.addEventListener("click", () => activateTextChange(optionsElements[i]));
break;
case "keybind-change":
let keybindButton = optionsElements[i].querySelector(".trigger-button");
keybindButton.addEventListener("click", () => activateKeybindChange(optionsElements[i]));
break;
}
}
await wait(() => checksLeft == 0, 1000, 50);
optionsContainer.classList.remove("hidden");
optionsContainer.classList.add("animated");
} }
/** /**