mirror of
https://github.com/ajayyy/SponsorBlock.git
synced 2025-12-10 13:37:04 +03:00
Moved invidious code to functions and made it check permissions on init.
This commit is contained in:
@@ -27,65 +27,21 @@ 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":
|
||||||
if (checkbox.checked) {
|
invidiousOnClick(checkbox, option);
|
||||||
// Request permission
|
|
||||||
chrome.permissions.request({
|
|
||||||
origins: ["https://*.invidio.us/*", "https://*.invidiou.sh/*"],
|
|
||||||
permissions: ["declarativeContent"]
|
|
||||||
}, function (granted) {
|
|
||||||
if (granted) {
|
|
||||||
chrome.declarativeContent.onPageChanged.removeRules(["invidious"], function() {
|
|
||||||
// Add page rule
|
|
||||||
let rule = {
|
|
||||||
id: "invidious",
|
|
||||||
conditions: [
|
|
||||||
new chrome.declarativeContent.PageStateMatcher({
|
|
||||||
pageUrl: { urlMatches: "https://*.invidio.us/*" }
|
|
||||||
}),
|
|
||||||
new chrome.declarativeContent.PageStateMatcher({
|
|
||||||
pageUrl: { urlMatches: "https://*.invidiou.sh/*" }
|
|
||||||
})
|
|
||||||
],
|
|
||||||
actions: [new chrome.declarativeContent.RequestContentScript({
|
|
||||||
allFrames: true,
|
|
||||||
js: [
|
|
||||||
"config.js",
|
|
||||||
"utils/previewBar.js",
|
|
||||||
"utils/skipNotice.js",
|
|
||||||
"utils.js",
|
|
||||||
"content.js",
|
|
||||||
"popup.js"
|
|
||||||
],
|
|
||||||
css: [
|
|
||||||
"content.css",
|
|
||||||
"./libs/Source+Sans+Pro.css",
|
|
||||||
"popup.css"
|
|
||||||
]
|
|
||||||
})]
|
|
||||||
};
|
|
||||||
|
|
||||||
chrome.declarativeContent.onPageChanged.addRules([rule]);
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
setOptionValue(option, false);
|
|
||||||
checkbox.checked = false;
|
|
||||||
|
|
||||||
chrome.declarativeContent.onPageChanged.removeRules(["invidious"]);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
chrome.declarativeContent.onPageChanged.removeRules(["invidious"]);
|
|
||||||
chrome.permissions.remove({
|
|
||||||
origins: ["https://*.invidio.us/*"]
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -114,6 +70,86 @@ async function init() {
|
|||||||
optionsContainer.classList.add("animated");
|
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) {
|
||||||
|
// Request permission
|
||||||
|
chrome.permissions.request({
|
||||||
|
origins: ["https://*.invidio.us/*", "https://*.invidiou.sh/*"],
|
||||||
|
permissions: ["declarativeContent"]
|
||||||
|
}, function (granted) {
|
||||||
|
if (granted) {
|
||||||
|
chrome.declarativeContent.onPageChanged.removeRules(["invidious"], function() {
|
||||||
|
// Add page rule
|
||||||
|
let rule = {
|
||||||
|
id: "invidious",
|
||||||
|
conditions: [
|
||||||
|
new chrome.declarativeContent.PageStateMatcher({
|
||||||
|
pageUrl: { urlMatches: "https://*.invidio.us/*" }
|
||||||
|
}),
|
||||||
|
new chrome.declarativeContent.PageStateMatcher({
|
||||||
|
pageUrl: { urlMatches: "https://*.invidiou.sh/*" }
|
||||||
|
})
|
||||||
|
],
|
||||||
|
actions: [new chrome.declarativeContent.RequestContentScript({
|
||||||
|
allFrames: true,
|
||||||
|
js: [
|
||||||
|
"config.js",
|
||||||
|
"utils/previewBar.js",
|
||||||
|
"utils/skipNotice.js",
|
||||||
|
"utils.js",
|
||||||
|
"content.js",
|
||||||
|
"popup.js"
|
||||||
|
],
|
||||||
|
css: [
|
||||||
|
"content.css",
|
||||||
|
"./libs/Source+Sans+Pro.css",
|
||||||
|
"popup.css"
|
||||||
|
]
|
||||||
|
})]
|
||||||
|
};
|
||||||
|
|
||||||
|
chrome.declarativeContent.onPageChanged.addRules([rule]);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
setOptionValue(option, false);
|
||||||
|
checkbox.checked = false;
|
||||||
|
|
||||||
|
chrome.declarativeContent.onPageChanged.removeRules(["invidious"]);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
chrome.declarativeContent.onPageChanged.removeRules(["invidious"]);
|
||||||
|
chrome.permissions.remove({
|
||||||
|
origins: ["https://*.invidio.us/*"]
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Will trigger the container to ask the user for a keybind.
|
* Will trigger the container to ask the user for a keybind.
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user