Skip to content

Commit

Permalink
Merge pull request #107 from ajayyy/experimental-ajay
Browse files Browse the repository at this point in the history
Preview bar, downvote to hide sponsor, usernames
  • Loading branch information
ajayyy authored Aug 13, 2019
2 parents 5916baf + 524e443 commit 5347340
Show file tree
Hide file tree
Showing 6 changed files with 237 additions and 5 deletions.
18 changes: 18 additions & 0 deletions content.css
Original file line number Diff line number Diff line change
@@ -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%;
Expand Down
34 changes: 33 additions & 1 deletion content.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -19,6 +22,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);
}
Expand Down Expand Up @@ -102,6 +109,7 @@ function messageListener(request, sender, sendResponse) {
sendResponse({
found: sponsorDataFound,
sponsorTimes: sponsorTimes,
hiddenSponsorTimes: hiddenSponsorTimes,
UUIDs: UUIDs
});

Expand Down Expand Up @@ -310,6 +318,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;
Expand Down Expand Up @@ -423,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);

Expand Down Expand Up @@ -934,6 +946,26 @@ 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
for (let i = 0; i < sponsorTimes.length; i++) {
if (UUIDs[i] == UUID) {
//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);
}
}
}

function addLoadingInfo(message, UUID) {
Expand Down
3 changes: 2 additions & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "SponsorBlock for YouTube - Skip Sponsorships",
"short_name": "SponsorBlock",
"version": "1.0.33",
"version": "1.0.34",
"description": "Skip over sponsorship on YouTube videos. Report sponsors on videos you watch to save the time of others.",
"content_scripts": [
{
Expand All @@ -11,6 +11,7 @@
"all_frames": true,
"js": [
"config.js",
"utils/previewBar.js",
"utils.js",
"content.js",
"popup.js"
Expand Down
27 changes: 26 additions & 1 deletion popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,39 @@ <h3 id="submitTimesInfoMessage" class="popupElement">

</div>

<div id="setUsernameContainer" class="popupElement">
<br/>
<br/>

<button id="setUsernameButton" class="warningButton popupElement">Set Username</button>
</div>

<div id="setUsername" class="popupElement" style="display: none">
<br/>

<h3>Set Username</h3>

<div id="setUsernameStatusContainer" style="display: none">
<h2 id="setUsernameStatus"></h2>
</div>


<input id="usernameInput" hint="Username"></input>

<br/>
<br/>

<button id="submitUsername" class="warningButton popupElement">Submit Username</button>
</div>

<div id="discordButtonContainer" class="popupElement" style="display: none">
<br/>

<a href="https://discord.gg/QnmVMpU" class="popupElement" target="_blank"><img src="https://www.logolynx.com/images/logolynx/1b/1bcc0f0aefe71b2c8ce66ffe8645d365.png" height="32px"/></a>

<br/>

Come join the official discord server to give suggestions and feedback!
Come join the official discord server to give suggestions and feedback!

<br/>

Expand Down
74 changes: 72 additions & 2 deletions popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ function runThePopup() {
// submitTimesInfoMessage
"submitTimesInfoMessageContainer",
"submitTimesInfoMessage",
// Username
"setUsernameContainer",
"setUsernameButton",
"setUsernameStatusContainer",
"setUsernameStatus",
"setUsername",
"usernameInput",
"submitUsername",
// More
"submissionSection",
"mainControls",
Expand All @@ -78,6 +86,8 @@ function runThePopup() {
SB.showDeleteButtonPlayerControls.addEventListener("click", showDeleteButtonPlayerControls);
SB.disableSponsorViewTracking.addEventListener("click", disableSponsorViewTracking);
SB.enableSponsorViewTracking.addEventListener("click", enableSponsorViewTracking);
SB.setUsernameButton.addEventListener("click", setUsernameButton);
SB.submitUsername.addEventListener("click", submitUsername);
SB.optionsButton.addEventListener("click", openOptions);
SB.reportAnIssue.addEventListener("click", reportAnIssue);
SB.hideDiscordButton.addEventListener("click", hideDiscordButton);
Expand Down Expand Up @@ -384,7 +394,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");

Expand Down Expand Up @@ -988,11 +1005,64 @@ function runThePopup() {
}
}

//make the options div visisble
//make the options div visible
function openOptions() {
document.getElementById("optionsButtonContainer").style.display = "none";
document.getElementById("options").style.display = "unset";
}

//make the options username setting option visible
function setUsernameButton() {
//get the userID
chrome.storage.sync.get(["userID"], function(result) {
//get username from the server
sendRequestToServer("GET", "/api/getUsername?userID=" + result.userID, function (xmlhttp, error) {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
SB.usernameInput.value = JSON.parse(xmlhttp.responseText).userName;

SB.submitUsername.style.display = "unset";
SB.usernameInput.style.display = "unset";

SB.setUsernameContainer.style.display = "none";
SB.setUsername.style.display = "unset";
} else {
SB.setUsername.style.display = "unset";
SB.submitUsername.style.display = "none";
SB.usernameInput.style.display = "none";

SB.setUsernameStatus.innerText = "Couldn't connect to server. Error code: " + xmlhttp.status;
}
});
});
}

//submit the new username
function submitUsername() {
//add loading indicator
SB.setUsernameStatusContainer.style.display = "unset";
SB.setUsernameStatus.innerText = "Loading...";

//get the userID
chrome.storage.sync.get(["userID"], function(result) {
sendRequestToServer("POST", "/api/setUsername?userID=" + result.userID + "&username=" + SB.usernameInput.value, function (xmlhttp, error) {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
//submitted
SB.submitUsername.style.display = "none";
SB.usernameInput.style.display = "none";

SB.setUsernameStatus.innerText = "Success!";
} else if (xmlhttp.readyState == 4 && xmlhttp.status == 400) {
SB.setUsernameStatus.innerText = "Bad Request";
} else {
SB.setUsernameStatus.innerText = getErrorMessage(EN_US, xmlhttp.status);
}
});
});


SB.setUsernameContainer.style.display = "none";
SB.setUsername.style.display = "unset";
}

//this is not a YouTube video page
function displayNoVideo() {
Expand Down
86 changes: 86 additions & 0 deletions utils/previewBar.js
Original file line number Diff line number Diff line change
@@ -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 = '&nbsp;';
return bar;
}

remove() {
this.container.remove();
this.container = undefined;
}
}

0 comments on commit 5347340

Please sign in to comment.