Added channel whitelisting.

Known issue: Does not work with 0 second sponsors.

Resolves https://github.com/ajayyy/SponsorBlock/issues/38
This commit is contained in:
Ajay Ramachandran 2019-08-03 21:35:41 -04:00
parent 809be3b2fb
commit a5580daebd
4 changed files with 220 additions and 1 deletions

View file

@ -17,6 +17,12 @@ if(id = getYouTubeVideoID(document.URL)){ // Direct Links
//the video
var v;
//the channel this video is about
var channelURL;
//is this channel whitelised from getting sponsors skipped
var channelWhitelisted = false;
//the last time looked at (used to see if this time is in the interval)
var lastTime = -1;
@ -109,6 +115,23 @@ function messageListener(request, sender, sendResponse) {
})
}
if (request.message == "getChannelURL") {
sendResponse({
channelURL: channelURL
})
}
if (request.message == "isChannelWhitelisted") {
sendResponse({
value: channelWhitelisted
})
}
if (request.message == "whitelistChange") {
channelWhitelisted = request.value;
sponsorsLookup(getYouTubeVideoID(document.URL));
}
if (request.message == "showNoticeAgain") {
dontShowNotice = false;
}
@ -246,6 +269,9 @@ function sponsorsLookup(id) {
sponsorTimes = JSON.parse(xmlhttp.responseText).sponsorTimes;
UUIDs = JSON.parse(xmlhttp.responseText).UUIDs;
getChannelID();
} else if (xmlhttp.readyState == 4) {
sponsorDataFound = false;
@ -270,6 +296,39 @@ function sponsorsLookup(id) {
};
}
function getChannelID() {
//get channel id
let channelContainers = document.querySelectorAll("#owner-name");
let channelURLContainer = null;
for (let i = 0; i < channelContainers.length; i++) {
if (channelContainers[i].firstElementChild != null) {
channelURLContainer = channelContainers[i].firstElementChild;
}
}
if (channelURLContainer == null) {
//try later
setTimeout(getChannelID, 100);
return;
}
channelURL = channelURLContainer.getAttribute("href");
//see if this is a whitelisted channel
chrome.storage.sync.get(["whitelistedChannels"], function(result) {
let whitelistedChannels = result.whitelistedChannels;
if (whitelistedChannels != undefined && whitelistedChannels.includes(channelURL)) {
//reset sponsor times to nothing
sponsorTimes = [];
UUIDs = [];
channelWhitelisted = true;
}
});
}
//video skipping
function sponsorCheck() {
let skipHappened = false;

View file

@ -86,6 +86,32 @@ h1.popupElement {
cursor: pointer;
}
.whitelistButton.popupElement {
background-color:#3acc3a;
-moz-border-radius:28px;
-webkit-border-radius:28px;
border-radius:28px;
border: none;
display:inline-block;
cursor:pointer;
color:#ffffff;
font-size:16px;
padding:8px 37px;
text-decoration:none;
text-shadow:0px 0px 0px #27663c;
}
.whitelistButton:hover.popupElement {
background-color:#218b26;
}
.whitelistButton:focus.popupElement {
outline: none;
background-color:#218b26;
}
.whitelistButton:active.popupElement {
position:relative;
top:1px;
}
.greenButton.popupElement {
background-color:#ec1c1c;
-moz-border-radius:28px;

View file

@ -25,7 +25,18 @@
<div id="downloadedSponsorMessageTimes" class="popupElement">
</div>
<br/>
<div>
<button id="whitelistChannel" class="whitelistButton popupElement">Whitelist Channel</button>
<button id="unwhitelistChannel" class="whitelistButton popupElement" style="display: none">Remove Channel From Whitelist</button>
</div>
<sub class="popupElement">
Whitelist the channels who do sponsorships ethically to encourage good behavior, or maybe if they are just entertaining and funny. Or don't, that's your call.
</sub>
<br/>
<br/>
<button id="reportAnIssue" class="dangerButton popupElement">Vote On A Sponsor Time</button>

125
popup.js
View file

@ -25,6 +25,8 @@ function runThePopup() {
var SB = {};
["sponsorStart",
"whitelistChannel",
"unwhitelistChannel",
"clearTimes",
"submitTimes",
"showNoticeAgain",
@ -63,6 +65,8 @@ function runThePopup() {
//setup click listeners
SB.sponsorStart.addEventListener("click", sendSponsorStartMessage);
SB.whitelistChannel.addEventListener("click", whitelistChannel);
SB.unwhitelistChannel.addEventListener("click", unwhitelistChannel);
SB.clearTimes.addEventListener("click", clearTimes);
SB.submitTimes.addEventListener("click", submitTimes);
SB.showNoticeAgain.addEventListener("click", showNoticeAgain);
@ -118,6 +122,26 @@ function runThePopup() {
});
}
});
//see if whitelist button should be swapped
chrome.tabs.query({
active: true,
currentWindow: true
}, tabs => {
chrome.tabs.sendMessage(
tabs[0].id,
{message: 'isChannelWhitelisted'},
function(response) {
if (response.value) {
SB.whitelistChannel.style.display = "none";
SB.unwhitelistChannel.style.display = "unset";
SB.downloadedSponsorMessageTimes.innerText = "Channel Whitelisted!";
SB.downloadedSponsorMessageTimes.style.fontWeight = "bold";
}
});
}
);
//if the don't show notice again letiable is true, an option to
// disable should be available
@ -343,7 +367,9 @@ function runThePopup() {
function displayDownloadedSponsorTimes(request) {
if (request.sponsorTimes != undefined) {
//set it to the message
SB.downloadedSponsorMessageTimes.innerText = getSponsorTimesMessage(request.sponsorTimes);
if (SB.downloadedSponsorMessageTimes.innerText != "Channel Whitelisted!") {
SB.downloadedSponsorMessageTimes.innerText = getSponsorTimesMessage(request.sponsorTimes);
}
//add them as buttons to the issue reporting container
let container = document.getElementById("issueReporterTimeButtons");
@ -966,6 +992,103 @@ function runThePopup() {
return formatted;
}
function whitelistChannel() {
//get the channel url
chrome.tabs.query({
active: true,
currentWindow: true
}, tabs => {
chrome.tabs.sendMessage(
tabs[0].id,
{message: 'getChannelURL'},
function(response) {
//get whitelisted channels
chrome.storage.sync.get(["whitelistedChannels"], function(result) {
let whitelistedChannels = result.whitelistedChannels;
if (whitelistedChannels == undefined) {
whitelistedChannels = [];
}
//add on this channel
whitelistedChannels.push(response.channelURL);
//change button
SB.whitelistChannel.style.display = "none";
SB.unwhitelistChannel.style.display = "unset";
SB.downloadedSponsorMessageTimes.innerText = "Channel Whitelisted!";
SB.downloadedSponsorMessageTimes.style.fontWeight = "bold";
//save this
chrome.storage.sync.set({whitelistedChannels: whitelistedChannels});
//send a message to the client
chrome.tabs.query({
active: true,
currentWindow: true
}, tabs => {
chrome.tabs.sendMessage(
tabs[0].id, {
message: 'whitelistChange',
value: true
});
}
);
});
}
);
});
}
function unwhitelistChannel() {
//get the channel url
chrome.tabs.query({
active: true,
currentWindow: true
}, tabs => {
chrome.tabs.sendMessage(
tabs[0].id,
{message: 'getChannelURL'},
function(response) {
//get whitelisted channels
chrome.storage.sync.get(["whitelistedChannels"], function(result) {
let whitelistedChannels = result.whitelistedChannels;
if (whitelistedChannels == undefined) {
whitelistedChannels = [];
}
//remove this channel
let index = whitelistedChannels.indexOf(response.channelURL);
whitelistedChannels.splice(index, 1);
//change button
SB.whitelistChannel.style.display = "unset";
SB.unwhitelistChannel.style.display = "none";
SB.downloadedSponsorMessageTimes.innerText = "";
SB.downloadedSponsorMessageTimes.style.fontWeight = "unset";
//save this
chrome.storage.sync.set({whitelistedChannels: whitelistedChannels});
//send a message to the client
chrome.tabs.query({
active: true,
currentWindow: true
}, tabs => {
chrome.tabs.sendMessage(
tabs[0].id, {
message: 'whitelistChange',
value: false
});
}
);
});
}
);
});
}
//converts time in seconds to minutes
function getTimeInMinutes(seconds) {