mirror of
https://github.com/ajayyy/SponsorBlock.git
synced 2025-12-09 21:17:20 +03:00
Added firefox support and support for multiple invidious instances.
This commit is contained in:
@@ -47,7 +47,7 @@
|
|||||||
"https://sponsor.ajay.app/*"
|
"https://sponsor.ajay.app/*"
|
||||||
],
|
],
|
||||||
"optional_permissions": [
|
"optional_permissions": [
|
||||||
"https://*/*",
|
"*://*/*",
|
||||||
"declarativeContent"
|
"declarativeContent"
|
||||||
],
|
],
|
||||||
"browser_action": {
|
"browser_action": {
|
||||||
|
|||||||
@@ -1,5 +1,10 @@
|
|||||||
window.addEventListener('DOMContentLoaded', init);
|
window.addEventListener('DOMContentLoaded', init);
|
||||||
|
|
||||||
|
var invidiousInstancesRegex = [];
|
||||||
|
for (const url of supportedInvidiousInstances) {
|
||||||
|
invidiousInstancesRegex.push("*://*." + url + "/*");
|
||||||
|
}
|
||||||
|
|
||||||
async function init() {
|
async function init() {
|
||||||
localizeHtmlPage();
|
localizeHtmlPage();
|
||||||
|
|
||||||
@@ -65,7 +70,7 @@ async function init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Don't wait on chrome
|
// Don't wait on chrome
|
||||||
if (typeof(browser) == "undefined") {
|
if (isFirefox()) {
|
||||||
await wait(() => checksLeft == 0, 1000, 50);
|
await wait(() => checksLeft == 0, 1000, 50);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,9 +85,12 @@ async function init() {
|
|||||||
* @param {string} option
|
* @param {string} option
|
||||||
*/
|
*/
|
||||||
function invidiousInit(checkbox, option) {
|
function invidiousInit(checkbox, option) {
|
||||||
|
let permissions = ["declarativeContent"];
|
||||||
|
if (isFirefox()) permissions = [];
|
||||||
|
|
||||||
chrome.permissions.contains({
|
chrome.permissions.contains({
|
||||||
origins: ["https://*.invidio.us/*", "https://*.invidiou.sh/*"],
|
origins: invidiousInstancesRegex,
|
||||||
permissions: ["declarativeContent"]
|
permissions: permissions
|
||||||
}, function (result) {
|
}, function (result) {
|
||||||
if (result != checkbox.checked) {
|
if (result != checkbox.checked) {
|
||||||
setOptionValue(option, result);
|
setOptionValue(option, result);
|
||||||
@@ -101,43 +109,66 @@ function invidiousInit(checkbox, option) {
|
|||||||
function invidiousOnClick(checkbox, option) {
|
function invidiousOnClick(checkbox, option) {
|
||||||
if (checkbox.checked) {
|
if (checkbox.checked) {
|
||||||
// Request permission
|
// Request permission
|
||||||
|
let permissions = ["declarativeContent"];
|
||||||
|
if (isFirefox()) permissions = [];
|
||||||
|
|
||||||
chrome.permissions.request({
|
chrome.permissions.request({
|
||||||
origins: ["https://*.invidio.us/*", "https://*.invidiou.sh/*"],
|
origins: invidiousInstancesRegex,
|
||||||
permissions: ["declarativeContent"]
|
permissions: permissions
|
||||||
}, function (granted) {
|
}, async function (granted) {
|
||||||
if (granted) {
|
if (granted) {
|
||||||
chrome.declarativeContent.onPageChanged.removeRules(["invidious"], function() {
|
let js = [
|
||||||
// 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",
|
"config.js",
|
||||||
"utils/previewBar.js",
|
"utils/previewBar.js",
|
||||||
"utils/skipNotice.js",
|
"utils/skipNotice.js",
|
||||||
"utils.js",
|
"utils.js",
|
||||||
"content.js",
|
"content.js",
|
||||||
"popup.js"
|
"popup.js"
|
||||||
],
|
];
|
||||||
css: [
|
let css = [
|
||||||
"content.css",
|
"content.css",
|
||||||
"./libs/Source+Sans+Pro.css",
|
"./libs/Source+Sans+Pro.css",
|
||||||
"popup.css"
|
"popup.css"
|
||||||
]
|
];
|
||||||
|
|
||||||
|
if (isFirefox()) {
|
||||||
|
let firefoxJS = [];
|
||||||
|
for (const file of js) {
|
||||||
|
firefoxJS.push({file});
|
||||||
|
}
|
||||||
|
let firefoxCSS = [];
|
||||||
|
for (const file of css) {
|
||||||
|
firefoxCSS.push({file});
|
||||||
|
}
|
||||||
|
|
||||||
|
let registration = await browser.contentScripts.register({
|
||||||
|
allFrames: true,
|
||||||
|
js: firefoxJS,
|
||||||
|
css: firefoxCSS,
|
||||||
|
matches: invidiousInstancesRegex
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
chrome.declarativeContent.onPageChanged.removeRules(["invidious"], function() {
|
||||||
|
let conditions = [];
|
||||||
|
for (const regex of invidiousInstancesRegex) {
|
||||||
|
conditions.push(new chrome.declarativeContent.PageStateMatcher({
|
||||||
|
pageUrl: { urlMatches: regex }
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
// Add page rule
|
||||||
|
let rule = {
|
||||||
|
id: "invidious",
|
||||||
|
conditions,
|
||||||
|
actions: [new chrome.declarativeContent.RequestContentScript({
|
||||||
|
allFrames: true,
|
||||||
|
js,
|
||||||
|
css
|
||||||
})]
|
})]
|
||||||
};
|
};
|
||||||
|
|
||||||
chrome.declarativeContent.onPageChanged.addRules([rule]);
|
chrome.declarativeContent.onPageChanged.addRules([rule]);
|
||||||
});
|
});
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
setOptionValue(option, false);
|
setOptionValue(option, false);
|
||||||
checkbox.checked = false;
|
checkbox.checked = false;
|
||||||
@@ -145,10 +176,15 @@ function invidiousOnClick(checkbox, option) {
|
|||||||
chrome.declarativeContent.onPageChanged.removeRules(["invidious"]);
|
chrome.declarativeContent.onPageChanged.removeRules(["invidious"]);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
} else {
|
||||||
|
if (isFirefox()) {
|
||||||
|
// Nothing for now
|
||||||
} else {
|
} else {
|
||||||
chrome.declarativeContent.onPageChanged.removeRules(["invidious"]);
|
chrome.declarativeContent.onPageChanged.removeRules(["invidious"]);
|
||||||
|
}
|
||||||
|
|
||||||
chrome.permissions.remove({
|
chrome.permissions.remove({
|
||||||
origins: ["https://*.invidio.us/*"]
|
origins: invidiousInstancesRegex
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
9
utils.js
9
utils.js
@@ -1,5 +1,5 @@
|
|||||||
var onInvidious = false;
|
var onInvidious = false;
|
||||||
var supportedInvidiousInstances = ["invidio.us", "invidiou.sh"];
|
var supportedInvidiousInstances = ["invidio.us", "invidiou.sh", "invidious.snopyta.org"];
|
||||||
|
|
||||||
// Function that can be used to wait for a condition before returning
|
// Function that can be used to wait for a condition before returning
|
||||||
async function wait(condition, timeout = 5000, check = 100) {
|
async function wait(condition, timeout = 5000, check = 100) {
|
||||||
@@ -125,3 +125,10 @@ function getErrorMessage(statusCode) {
|
|||||||
|
|
||||||
return errorMessage;
|
return errorMessage;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Is this Firefox (web-extensions)
|
||||||
|
*/
|
||||||
|
function isFirefox() {
|
||||||
|
return typeof(browser) !== "undefined";
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user