Skip to content

Commit

Permalink
Merge pull request #7 from ajayyy/experimental
Browse files Browse the repository at this point in the history
Zero second sponsors and support for switching tabs
  • Loading branch information
ajayyy committed Jul 26, 2019
2 parents d9789c2 + a86e55a commit 250c75a
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 46 deletions.
42 changes: 27 additions & 15 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,38 @@ var previousVideoID = null
//the id of this user, randomly generated once per install
var userID = null;

chrome.tabs.onUpdated.addListener( // On tab update
function(tabId, changeInfo, tab) {
//when a new tab is highlighted
chrome.tabs.onActivated.addListener(
function(activeInfo) {
chrome.tabs.get(activeInfo.tabId, function(tab) {
let id = getYouTubeVideoID(tab.url);

//if this even is a YouTube tab
if (id) {
videoIDChange(id, activeInfo.tabId);
}
})
}
);

//when a tab changes URLs
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
if (changeInfo != undefined && changeInfo.url != undefined) {
let id = getYouTubeVideoID(changeInfo.url);
if (changeInfo.url && id) { // If URL changed and is youtube video message contentScript the video id
videoIDChange(id);

chrome.tabs.sendMessage( tabId, {
message: 'ytvideoid',
id: id
});
//if URL changed and is youtube video message contentScript the video id
if (changeInfo.url && id) {
videoIDChange(id, tabId);
}
}
}
);
});

chrome.runtime.onMessage.addListener(function (request, sender, callback) {
if (request.message == "submitTimes") {
submitTimes(request.videoID, callback);

//this allows the callback to be called later by the submitTimes function
return true;
} else if (request.message == "ytvideoid") {
if (previousVideoID != request.videoID) {
videoIDChange(request.videoID);
}
} else if (request.message == "addSponsorTime") {
addSponsorTime(request.time);
} else if (request.message == "getSponsorTimes") {
Expand Down Expand Up @@ -147,7 +153,13 @@ function submitTimes(videoID, callback) {
});
}

function videoIDChange(currentVideoID) {
function videoIDChange(currentVideoID, tabId) {
//send a message to the content script
chrome.tabs.sendMessage(tabId, {
message: 'ytvideoid',
id: currentVideoID
});

//warn them if they had unsubmitted times
if (previousVideoID != null) {
//get the sponsor times from storage
Expand Down
87 changes: 58 additions & 29 deletions content.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
if(id = getYouTubeVideoID(document.URL)){ // Direct Links
videoIDChange(id);

//tell background.js about this
chrome.runtime.sendMessage({
message: "ytvideoid",
videoID: id
});
}

//was sponsor data found when doing SponsorsLookup
Expand All @@ -19,10 +13,10 @@ var UUIDs = undefined;
var v;

//the last time looked at (used to see if this time is in the interval)
var lastTime;
var lastTime = -1;

//the last time skipped to
var lastTimeSkippedTo = -1;
//the actual time (not video time) that the last skip happened
var lastUnixTimeSkipped = -1;

//the last time in the video a sponsor was skipped
//used for the go back button
Expand Down Expand Up @@ -104,6 +98,10 @@ chrome.runtime.onMessage.addListener( // Detect URL Changes
});

function videoIDChange(id) {
//reset last sponsor times
lastTime = -1;
lastUnixTimeSkipped = -1;

//reset sponsor data found check
sponsorDataFound = false;
sponsorsLookup(id);
Expand Down Expand Up @@ -170,24 +168,30 @@ function sponsorsLookup(id) {
function sponsorCheck(sponsorTimes) { // Video skipping
//see if any sponsor start time was just passed
for (let i = 0; i < sponsorTimes.length; i++) {
//the sponsor time is in between these times, skip it
//if the time difference is more than 1 second, than the there was probably a skip in time,
// and it's not due to playback
//also check if the last time skipped to is not too close to now, to make sure not to get too many
//this means part of the video was just skipped
if (Math.abs(v.currentTime - lastTime) > 1 && lastTime != -1) {
//make lastTime as if the video was playing normally
lastTime = v.currentTime - 0.0001;
}

let currentTime = Date.now();

//If the sponsor time is in between these times, skip it
//Checks if the last time skipped to is not too close to now, to make sure not to get too many
// sponsor times in a row (from one troll)
if (Math.abs(v.currentTime - lastTime) < 1 && sponsorTimes[i][0] >= lastTime && sponsorTimes[i][0] <= v.currentTime &&
(lastTimeSkippedTo == -1 || Math.abs(v.currentTime - lastTimeSkippedTo) > 1)) {
//the last term makes 0 second start times possible
if ((Math.abs(v.currentTime - sponsorTimes[i][0]) < 0.3 && sponsorTimes[i][0] >= lastTime && sponsorTimes[i][0] <= v.currentTime
&& (lastUnixTimeSkipped == -1 || currentTime - lastUnixTimeSkipped > 500)) || (lastTime == -1 && sponsorTimes[i][0] == 0)) {
//skip it
v.currentTime = sponsorTimes[i][1];
lastTimeSkippedTo = sponsorTimes[i][1];

lastSponsorTimeSkipped = sponsorTimes[i][0];

let currentUUID = UUIDs[i];
lastSponsorTimeSkippedUUID = currentUUID;

//send out the message saying that a sponsor message was skipped
openSkipNotice();
openSkipNotice(currentUUID);

setTimeout(() => closeSkipNotice(currentUUID), 7000);

Expand All @@ -197,7 +201,11 @@ function sponsorCheck(sponsorTimes) { // Video skipping
}
}
}
lastTime = v.currentTime;

//don't keep track until they are loaded in
if (sponsorTimes.length > 0) {
lastTime = v.currentTime;
}
}

function goBackToPreviousTime(UUID) {
Expand Down Expand Up @@ -313,12 +321,35 @@ function addSubmitButton() {
}

//Opens the notice that tells the user that a sponsor was just skipped
function openSkipNotice(){
function openSkipNotice(UUID){
if (dontShowNotice) {
//don't show, return
return;
}

//check if page is loaded yet (for 0 second sponsors, the page might not be loaded yet)
//it looks for the view count div and sees if it is full yet
//querySelectorAll is being used like findElementById for multiple objects, because for
//some reason YouTube has put more than one object with one ID.
let viewCountNode = document.querySelectorAll("#count");
//check to see if the length is over zero, otherwise it's a different YouTube theme probably
if (viewCountNode.length > 0) {
//check if any of these have text
let viewCountVisible = false;
for (let i = 0; i < viewCountNode.length; i++) {
if (viewCountNode[i].innerText != null) {
viewCountVisible = true;
break;
}
}
if (!viewCountVisible) {
//this is the new YouTube layout and it is still loading
//wait a bit for opening the notice
setTimeout(() => openSkipNotice(UUID), 200);
return;
}
}

let amountOfPreviousNotices = document.getElementsByClassName("sponsorSkipNotice").length;

if (amountOfPreviousNotices > 0) {
Expand All @@ -328,45 +359,43 @@ function openSkipNotice(){
previousNotice.classList.add("secondSkipNotice")
}

let UUID = lastSponsorTimeSkippedUUID;

let noticeElement = document.createElement("div");
//what sponsor time this is about
noticeElement.id = "sponsorSkipNotice" + lastSponsorTimeSkippedUUID;
noticeElement.id = "sponsorSkipNotice" + UUID;
noticeElement.classList.add("sponsorSkipObject");
noticeElement.classList.add("sponsorSkipNotice");
noticeElement.style.zIndex = 1 + amountOfPreviousNotices;
noticeElement.style.zIndex = 5 + amountOfPreviousNotices;

let logoElement = document.createElement("img");
logoElement.id = "sponsorSkipLogo" + lastSponsorTimeSkippedUUID;
logoElement.id = "sponsorSkipLogo" + UUID;
logoElement.className = "sponsorSkipLogo";
logoElement.src = chrome.extension.getURL("icons/LogoSponsorBlocker256px.png");

let noticeMessage = document.createElement("div");
noticeMessage.id = "sponsorSkipMessage" + lastSponsorTimeSkippedUUID;
noticeMessage.id = "sponsorSkipMessage" + UUID;
noticeMessage.classList.add("sponsorSkipMessage");
noticeMessage.classList.add("sponsorSkipObject");
noticeMessage.innerText = "Hey, you just skipped a sponsor!";

let noticeInfo = document.createElement("p");
noticeInfo.id = "sponsorSkipInfo" + lastSponsorTimeSkippedUUID;
noticeInfo.id = "sponsorSkipInfo" + UUID;
noticeInfo.classList.add("sponsorSkipInfo");
noticeInfo.classList.add("sponsorSkipObject");
noticeInfo.innerText = "This message will disapear in 7 seconds";

//thumbs up and down buttons
let voteButtonsContainer = document.createElement("div");
voteButtonsContainer.id = "sponsorTimesVoteButtonsContainer" + lastSponsorTimeSkippedUUID;
voteButtonsContainer.id = "sponsorTimesVoteButtonsContainer" + UUID;
voteButtonsContainer.setAttribute("align", "center");

let upvoteButton = document.createElement("img");
upvoteButton.id = "sponsorTimesUpvoteButtonsContainer" + lastSponsorTimeSkippedUUID;
upvoteButton.id = "sponsorTimesUpvoteButtonsContainer" + UUID;
upvoteButton.className = "sponsorSkipObject voteButton";
upvoteButton.src = chrome.extension.getURL("icons/upvote.png");
upvoteButton.addEventListener("click", () => vote(1, UUID));

let downvoteButton = document.createElement("img");
downvoteButton.id = "sponsorTimesDownvoteButtonsContainer" + lastSponsorTimeSkippedUUID;
downvoteButton.id = "sponsorTimesDownvoteButtonsContainer" + UUID;
downvoteButton.className = "sponsorSkipObject voteButton";
downvoteButton.src = chrome.extension.getURL("icons/downvote.png");
downvoteButton.addEventListener("click", () => vote(0, UUID));
Expand Down
2 changes: 1 addition & 1 deletion firefox_manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "SponsorBlock - YouTube Sponsorship Blocker",
"short_name": "SponsorBlock",
"version": "1.0.2",
"version": "1.0.4",
"description": "Skip over sponsorship on YouTube videos. Report sponsors on videos you watch to save the time of others.",
"content_scripts": [
{
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "SponsorBlock - YouTube Sponsorship Blocker",
"short_name": "SponsorBlock",
"version": "1.0.2",
"version": "1.0.4",
"description": "Skip over sponsorship on YouTube videos. Report sponsors on videos you watch to save the time of others.",
"content_scripts": [
{
Expand Down

0 comments on commit 250c75a

Please sign in to comment.