From 44c467197787223822a2ca2aa8b034222847fa7c Mon Sep 17 00:00:00 2001 From: Official Noob <31563761+OfficialNoob@users.noreply.github.com> Date: Mon, 12 Aug 2019 17:41:26 +0100 Subject: [PATCH 01/11] Added switch for chrome.runtime.onMessage --- background.js | 55 +++++++++++++++++++++------------------------------ 1 file changed, 23 insertions(+), 32 deletions(-) diff --git a/background.js b/background.js index a0e0c798..bf5c2136 100644 --- a/background.js +++ b/background.js @@ -5,38 +5,29 @@ chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) { }); 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 == "addSponsorTime") { - addSponsorTime(request.time, request.videoID, callback); - - //this allows the callback to be called later - return true; - } else if (request.message == "getSponsorTimes") { - getSponsorTimes(request.videoID, function(sponsorTimes) { - callback({ - sponsorTimes: sponsorTimes - }) - }); - - //this allows the callback to be called later - return true; - } else if (request.message == "submitVote") { - submitVote(request.type, request.UUID, callback); - - //this allows the callback to be called later - return true; - } else if (request.message == "alertPrevious") { - chrome.notifications.create("stillThere" + Math.random(), { - type: "basic", - title: "Do you want to submit the sponsor times for video id " + request.previousVideoID + "?", - message: "You seem to have left some sponsor times unsubmitted. Go back to that page to submit them (they are not deleted).", - iconUrl: "./icons/LogoSponsorBlocker256px.png" - }); - } + switch(request.message) { + case "submitTimes": + submitTimes(request.videoID, callback); + return true; //this allows the callback to be called later by the submitTimes function + case "addSponsorTime": + addSponsorTime(request.time, request.videoID, callback); + return true; //this allows the callback to be called later + case "getSponsorTimes": + getSponsorTimes(request.videoID, function(sponsorTimes) { + callback({sponsorTimes: sponsorTimes}) + }); + return true; //this allows the callback to be called later + case "submitVote": + submitVote(request.type, request.UUID, callback); + return true; + case "alertPrevious": + chrome.notifications.create("stillThere" + Math.random(), { + type: "basic", + title: "Do you want to submit the sponsor times for video id " + request.previousVideoID + "?", + message: "You seem to have left some sponsor times unsubmitted. Go back to that page to submit them (they are not deleted).", + iconUrl: "./icons/LogoSponsorBlocker256px.png" + }); + } }); //add help page on install From 5feed6bfccbc50378be53f9c337cb1d3a9bf8d76 Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Mon, 12 Aug 2019 19:34:44 -0400 Subject: [PATCH 02/11] Added preview bar with the sponsors --- content.css | 18 ++++++++++ content.js | 8 +++++ manifest.json | 1 + utils/previewBar.js | 86 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 113 insertions(+) create mode 100644 utils/previewBar.js diff --git a/content.css b/content.css index 1d118fcf..c05f2f16 100644 --- a/content.css +++ b/content.css @@ -1,3 +1,21 @@ +#previewbar { + overflow: visible; + padding: 0; + margin: 0; + position: absolute; + width: 100%; + pointer-events: none; + + height: 100%; + transform: scaleY(0.6) translateY(-30%) translateY(1.5px); + z-index: 40; +} + +.previewbar { + display: inline-block; + height: 100%; +} + .popup { z-index: 10; width: 100%; diff --git a/content.js b/content.js index 3ee26aeb..1e1d5d2a 100644 --- a/content.js +++ b/content.js @@ -19,6 +19,10 @@ var channelURL; //is this channel whitelised from getting sponsors skipped var channelWhitelisted = false; +// create preview bar +let progressBar = document.getElementsByClassName("ytp-progress-bar-container")[0] || document.getElementsByClassName("no-model cue-range-markers")[0]; +var previewBar = new PreviewBar(progressBar); + if(id = getYouTubeVideoID(document.URL)){ // Direct Links videoIDChange(id); } @@ -310,6 +314,10 @@ function sponsorsLookup(id) { sponsorTimes = JSON.parse(xmlhttp.responseText).sponsorTimes; UUIDs = JSON.parse(xmlhttp.responseText).UUIDs; + //update the preview bar + //leave the type blank for now until categories are added + previewBar.set(sponsorTimes, [], v.duration); + getChannelID(); sponsorLookupRetries = 0; diff --git a/manifest.json b/manifest.json index 23204e4b..cc9f4000 100644 --- a/manifest.json +++ b/manifest.json @@ -11,6 +11,7 @@ "all_frames": true, "js": [ "config.js", + "utils/previewBar.js", "utils.js", "content.js", "popup.js" diff --git a/utils/previewBar.js b/utils/previewBar.js new file mode 100644 index 00000000..95454e9d --- /dev/null +++ b/utils/previewBar.js @@ -0,0 +1,86 @@ +/* + This is based on code from VideoSegments. + https://github.com/videosegments/videosegments/commits/f1e111bdfe231947800c6efdd51f62a4e7fef4d4/segmentsbar/segmentsbar.js +*/ + +'use strict'; + +let barTypes = { + "undefined": { + color: "#00d400", + opacity: "0.5" + }, + "sponsor": { + color: "#00d400", + opacity: "0.5" + } +}; + +class PreviewBar { + constructor(parent) { + this.container = document.createElement('ul'); + this.container.id = 'previewbar'; + this.parent = parent; + this.bars = [] + + this.updatePosition(); + } + + updatePosition() { + //below the seek bar + // this.parent.insertAdjacentElement("afterEnd", this.container); + + //on the seek bar + this.parent.insertAdjacentElement("afterBegin", this.container); + } + + updateColor(segment, color, opacity) { + let bars = document.querySelectorAll('[data-vs-segment-type=' + segment + ']'); + for (let bar of bars) { + bar.style.backgroundColor = color; + bar.style.opacity = opacity; + } + } + + set(timestamps, types, duration) { + while (this.container.firstChild) { + this.container.removeChild(this.container.firstChild); + } + + if (!timestamps || !types) { + return; + } + + // to avoid rounding error resulting in width more than 100% + duration = Math.floor(duration * 100) / 100; + let width; + for (let i = 0; i < timestamps.length; i++) { + width = (timestamps[i][1] - timestamps[i][0]) / duration * 100; + width = Math.floor(width * 100) / 100; + + let bar = this.createBar(); + bar.setAttribute('data-vs-segment-type', types[i]); + + bar.style.backgroundColor = barTypes[types[i]].color; + bar.style.opacity = barTypes[types[i]].opacity; + bar.style.width = width + '%'; + bar.style.left = (timestamps[i][0] / duration * 100) + "%"; + bar.style.position = "absolute" + + this.container.insertAdjacentElement('beforeEnd', bar); + this.bars[i] = bar; + } + } + + createBar() { + let bar = document.createElement('li'); + bar.classList.add('previewbar'); + bar.innerHTML = ' '; + return bar; + } + + remove() { + this.container.remove(); + this.container = undefined; + } +} \ No newline at end of file From 8f0a9d97f671005a08594761a51ade30e97cf736 Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Mon, 12 Aug 2019 20:00:42 -0400 Subject: [PATCH 03/11] Downvoting now temporarily hides the sponsor time. --- content.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/content.js b/content.js index 1e1d5d2a..e7db1359 100644 --- a/content.js +++ b/content.js @@ -942,6 +942,24 @@ function afterDownvote(UUID) { //add element to div document.getElementById("sponsorTimesVoteButtonsContainer" + UUID).appendChild(thanksForVotingText); document.getElementById("sponsorTimesVoteButtonsContainer" + UUID).appendChild(thanksForVotingInfoText); + + //remove this sponsor from the sponsors looked up + //find which one it is + let removeIndex = -1; + for (let i = 0; i < sponsorTimes.length; i++) { + if (UUIDs[i] == UUID) { + //this one is the one to remove + removeIndex = i; + } + } + + if (removeIndex != -1) { + //remove this sponsor time + sponsorTimes.splice(removeIndex, 1); + + //update the preview + previewBar.set(sponsorTimes, [], v.duration); + } } function addLoadingInfo(message, UUID) { From 13d0e4a33a244037e35e95f9d9a6bd8720f07261 Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Mon, 12 Aug 2019 20:22:00 -0400 Subject: [PATCH 04/11] Made it still possible to vote on downvoted sponsors. Added "(hidden)" to the button. Resolves https://github.com/ajayyy/SponsorBlock/issues/28 --- content.js | 30 ++++++++++++++++++------------ popup.js | 9 ++++++++- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/content.js b/content.js index e7db1359..0a181560 100644 --- a/content.js +++ b/content.js @@ -7,6 +7,9 @@ var UUIDs = null; //what video id are these sponsors for var sponsorVideoID = null; +//these are sponsors that have been downvoted +var hiddenSponsorTimes = []; + //the time this video is starting at when first played, if not zero var youtubeVideoStartTime = null; @@ -106,6 +109,7 @@ function messageListener(request, sender, sendResponse) { sendResponse({ found: sponsorDataFound, sponsorTimes: sponsorTimes, + hiddenSponsorTimes: hiddenSponsorTimes, UUIDs: UUIDs }); @@ -431,7 +435,7 @@ function checkSponsorTime(sponsorTimes, index, openNotice) { lastTime = v.currentTime - 0.0001; } - if (checkIfTimeToSkip(v.currentTime, sponsorTimes[index][0])) { + if (checkIfTimeToSkip(v.currentTime, sponsorTimes[index][0]) && !hiddenSponsorTimes.includes(index)) { //skip it skipToTime(v, index, sponsorTimes, openNotice); @@ -945,21 +949,23 @@ function afterDownvote(UUID) { //remove this sponsor from the sponsors looked up //find which one it is - let removeIndex = -1; for (let i = 0; i < sponsorTimes.length; i++) { if (UUIDs[i] == UUID) { - //this one is the one to remove - removeIndex = i; + //this one is the one to hide + + //add this as a hidden sponsorTime + hiddenSponsorTimes.push(i); + + let sponsorTimesLeft = sponsorTimes.slice(); + for (let j = 0; j < hiddenSponsorTimes.length; j++) { + //remove this sponsor time + sponsorTimesLeft.splice(hiddenSponsorTimes[j], 1); + } + + //update the preview + previewBar.set(sponsorTimesLeft, [], v.duration); } } - - if (removeIndex != -1) { - //remove this sponsor time - sponsorTimes.splice(removeIndex, 1); - - //update the preview - previewBar.set(sponsorTimes, [], v.duration); - } } function addLoadingInfo(message, UUID) { diff --git a/popup.js b/popup.js index 71587889..220fb769 100644 --- a/popup.js +++ b/popup.js @@ -384,7 +384,14 @@ function runThePopup() { for (let i = 0; i < request.sponsorTimes.length; i++) { let sponsorTimeButton = document.createElement("button"); sponsorTimeButton.className = "warningButton popupElement"; - sponsorTimeButton.innerText = getFormattedTime(request.sponsorTimes[i][0]) + " to " + getFormattedTime(request.sponsorTimes[i][1]); + + let extraInfo = ""; + if (request.hiddenSponsorTimes.includes(i)) { + //this one is hidden + extraInfo = " (hidden)"; + } + + sponsorTimeButton.innerText = getFormattedTime(request.sponsorTimes[i][0]) + " to " + getFormattedTime(request.sponsorTimes[i][1]) + extraInfo; let votingButtons = document.createElement("div"); From 5445146b5610f171d992e4e3acd2b92cb76f2495 Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Mon, 12 Aug 2019 23:20:35 -0400 Subject: [PATCH 05/11] Added ability to set your username --- popup.html | 27 ++++++++++++++++++++++++++- popup.js | 46 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 71 insertions(+), 2 deletions(-) diff --git a/popup.html b/popup.html index 82c2d66c..0c829494 100644 --- a/popup.html +++ b/popup.html @@ -114,6 +114,31 @@ +
+
+
+ + +
+ + +