Fixed firefox support.

This commit is contained in:
Ajay Ramachandran
2019-12-31 18:13:51 -05:00
parent b441cc2123
commit ea3f9f246e
3 changed files with 61 additions and 36 deletions

View File

@@ -1,3 +1,6 @@
// Used only on Firefox, which does not support non persistent background pages.
var contentScriptRegistrations = {};
chrome.tabs.onUpdated.addListener(function(tabId) {
chrome.tabs.sendMessage(tabId, {
message: 'update',
@@ -6,38 +9,53 @@ chrome.tabs.onUpdated.addListener(function(tabId) {
chrome.runtime.onMessage.addListener(function (request, sender, callback) {
switch(request.message) {
case "submitTimes":
submitTimes(request.videoID, callback);
case "submitTimes":
submitTimes(request.videoID, callback);
//this allows the callback to be called later by the submitTimes function
return true;
case "addSponsorTime":
addSponsorTime(request.time, request.videoID, callback);
//this allows the callback to be called later by the submitTimes function
return true;
case "addSponsorTime":
addSponsorTime(request.time, request.videoID, callback);
//this allows the callback to be called later
return true;
case "getSponsorTimes":
getSponsorTimes(request.videoID, function(sponsorTimes) {
callback({
sponsorTimes: sponsorTimes
})
});
//this allows the callback to be called later
return true;
case "getSponsorTimes":
getSponsorTimes(request.videoID, function(sponsorTimes) {
callback({
sponsorTimes: sponsorTimes
})
});
//this allows the callback to be called later
return true;
case "submitVote":
submitVote(request.type, request.UUID, callback);
//this allows the callback to be called later
return true;
case "submitVote":
submitVote(request.type, request.UUID, callback);
//this allows the callback to be called later
return true;
case "alertPrevious":
chrome.notifications.create("stillThere" + Math.random(), {
type: "basic",
title: chrome.i18n.getMessage("wantToSubmit") + " " + request.previousVideoID + "?",
message: chrome.i18n.getMessage("leftTimes"),
iconUrl: "./icons/LogoSponsorBlocker256px.png"
});
}
//this allows the callback to be called later
return true;
case "alertPrevious":
chrome.notifications.create("stillThere" + Math.random(), {
type: "basic",
title: chrome.i18n.getMessage("wantToSubmit") + " " + request.previousVideoID + "?",
message: chrome.i18n.getMessage("leftTimes"),
iconUrl: "./icons/LogoSponsorBlocker256px.png"
});
return false;
case "registerContentScript":
browser.contentScripts.register({
allFrames: request.allFrames,
js: request.js,
css: request.css,
matches: request.matches
}).then(() => void (contentScriptRegistrations[request.id] = registration));
return false;
case "unregisterContentScript":
contentScriptRegistrations[request.id].unregister();
delete contentScriptRegistrations[request.id];
return false;
}
});
//add help page on install

View File

@@ -2,7 +2,8 @@ window.addEventListener('DOMContentLoaded', init);
var invidiousInstancesRegex = [];
for (const url of supportedInvidiousInstances) {
invidiousInstancesRegex.push("*://*." + url + "/*");
invidiousInstancesRegex.push("https://*." + url + "/*");
invidiousInstancesRegex.push("http://*." + url + "/*");
}
async function init() {
@@ -19,6 +20,7 @@ async function init() {
switch (optionsElements[i].getAttribute("option-type")) {
case "toggle":
let option = optionsElements[i].getAttribute("sync-option");
chrome.storage.sync.get([option], function(result) {
let optionResult = result[option];
let checkbox = optionsElements[i].querySelector("input");
@@ -141,7 +143,9 @@ function invidiousOnClick(checkbox, option) {
firefoxCSS.push({file});
}
let registration = await browser.contentScripts.register({
chrome.runtime.sendMessage({
message: "registerContentScript",
id: "invidious",
allFrames: true,
js: firefoxJS,
css: firefoxCSS,
@@ -178,7 +182,10 @@ function invidiousOnClick(checkbox, option) {
});
} else {
if (isFirefox()) {
// Nothing for now
chrome.runtime.sendMessage({
message: "unregisterContentScript",
id: "invidious"
});
} else {
chrome.declarativeContent.onPageChanged.removeRules(["invidious"]);
}