Fix merge error

This commit is contained in:
OfficialNoob
2019-08-13 22:14:45 +01:00
7 changed files with 290 additions and 38 deletions

View File

@@ -5,38 +5,39 @@ chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
}); });
chrome.runtime.onMessage.addListener(function (request, sender, callback) { chrome.runtime.onMessage.addListener(function (request, sender, callback) {
if (request.message == "submitTimes") { switch(request.message) {
submitTimes(request.videoID, callback); case "submitTimes":
submitTimes(request.videoID, callback);
//this allows the callback to be called later by the submitTimes function
return true; //this allows the callback to be called later by the submitTimes function
} else if (request.message == "addSponsorTime") { return true;
addSponsorTime(request.time, request.videoID, callback); case "addSponsorTime":
addSponsorTime(request.time, request.videoID, callback);
//this allows the callback to be called later
return true; //this allows the callback to be called later
} else if (request.message == "getSponsorTimes") { return true;
getSponsorTimes(request.videoID, function(sponsorTimes) { case "getSponsorTimes":
callback({ getSponsorTimes(request.videoID, function(sponsorTimes) {
sponsorTimes: sponsorTimes callback({
}) sponsorTimes: sponsorTimes
}); })
});
//this allows the callback to be called later
return true; //this allows the callback to be called later
} else if (request.message == "submitVote") { return true;
submitVote(request.type, request.UUID, callback); case "submitVote":
submitVote(request.type, request.UUID, callback);
//this allows the callback to be called later
return true; //this allows the callback to be called later
} else if (request.message == "alertPrevious") { return true;
chrome.notifications.create("stillThere" + Math.random(), { case "alertPrevious":
type: "basic", chrome.notifications.create("stillThere" + Math.random(), {
title: chrome.i18n.getMessage("wantToSubmit") + request.previousVideoID + "?", type: "basic",
message: chrome.i18n.getMessage("leftTimes"), title: "Do you want to submit the sponsor times for video id " + request.previousVideoID + "?",
iconUrl: "./icons/LogoSponsorBlocker256px.png" 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 //add help page on install

View File

@@ -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 { .popup {
z-index: 10; z-index: 10;
width: 100%; width: 100%;

View File

@@ -7,18 +7,27 @@ var UUIDs = null;
//what video id are these sponsors for //what video id are these sponsors for
var sponsorVideoID = null; 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 //the time this video is starting at when first played, if not zero
var youtubeVideoStartTime = null; var youtubeVideoStartTime = null;
//the video //the video
var v; var v;
var listenerAdded;
//the channel this video is about //the channel this video is about
var channelURL; var channelURL;
//is this channel whitelised from getting sponsors skipped //is this channel whitelised from getting sponsors skipped
var channelWhitelisted = false; 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 if(id = getYouTubeVideoID(document.URL)){ // Direct Links
videoIDChange(id); videoIDChange(id);
} }
@@ -102,6 +111,7 @@ function messageListener(request, sender, sendResponse) {
sendResponse({ sendResponse({
found: sponsorDataFound, found: sponsorDataFound,
sponsorTimes: sponsorTimes, sponsorTimes: sponsorTimes,
hiddenSponsorTimes: hiddenSponsorTimes,
UUIDs: UUIDs UUIDs: UUIDs
}); });
@@ -233,6 +243,9 @@ function videoIDChange(id) {
sponsorVideoID = id; sponsorVideoID = id;
sponsorLookupRetries = 0; sponsorLookupRetries = 0;
//empty the preview bar
previewBar.set([], [], 0);
//see if there is a video start time //see if there is a video start time
youtubeVideoStartTime = getYouTubeVideoStartTime(document.URL); youtubeVideoStartTime = getYouTubeVideoStartTime(document.URL);
@@ -310,6 +323,17 @@ function sponsorsLookup(id) {
sponsorTimes = JSON.parse(xmlhttp.responseText).sponsorTimes; sponsorTimes = JSON.parse(xmlhttp.responseText).sponsorTimes;
UUIDs = JSON.parse(xmlhttp.responseText).UUIDs; UUIDs = JSON.parse(xmlhttp.responseText).UUIDs;
//update the preview bar
//leave the type blank for now until categories are added
console.log(v.duration)
if (isNaN(v.duration)) {
//wait until it is loaded
v.addEventListener('durationchange', updatePreviewBar);
} else {
//set it now
updatePreviewBar();
}
getChannelID(); getChannelID();
sponsorLookupRetries = 0; sponsorLookupRetries = 0;
@@ -330,7 +354,7 @@ function sponsorsLookup(id) {
}); });
sponsorLookupRetries = 0; sponsorLookupRetries = 0;
} else if (xmlhttp.readyState == 4 && sponsorLookupRetries < 15) { } else if (xmlhttp.readyState == 4 && sponsorLookupRetries < 90) {
//some error occurred, try again in a second //some error occurred, try again in a second
setTimeout(() => sponsorsLookup(id), 1000); setTimeout(() => sponsorsLookup(id), 1000);
@@ -344,6 +368,13 @@ function sponsorsLookup(id) {
}; };
} }
function updatePreviewBar() {
previewBar.set(sponsorTimes, [], v.duration);
//the listener is only needed once
v.removeEventListener('durationchange', updatePreviewBar);
}
function getChannelID() { function getChannelID() {
//get channel id //get channel id
let channelContainers = document.querySelectorAll("#owner-name"); let channelContainers = document.querySelectorAll("#owner-name");
@@ -423,7 +454,7 @@ function checkSponsorTime(sponsorTimes, index, openNotice) {
lastTime = v.currentTime - 0.0001; lastTime = v.currentTime - 0.0001;
} }
if (checkIfTimeToSkip(v.currentTime, sponsorTimes[index][0])) { if (checkIfTimeToSkip(v.currentTime, sponsorTimes[index][0]) && !hiddenSponsorTimes.includes(index)) {
//skip it //skip it
skipToTime(v, index, sponsorTimes, openNotice); skipToTime(v, index, sponsorTimes, openNotice);
@@ -934,6 +965,26 @@ function afterDownvote(UUID) {
//add element to div //add element to div
document.getElementById("sponsorTimesVoteButtonsContainer" + UUID).appendChild(thanksForVotingText); document.getElementById("sponsorTimesVoteButtonsContainer" + UUID).appendChild(thanksForVotingText);
document.getElementById("sponsorTimesVoteButtonsContainer" + UUID).appendChild(thanksForVotingInfoText); 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) { function addLoadingInfo(message, UUID) {

View File

@@ -1,7 +1,7 @@
{ {
"name": "__MSG_fullName__", "name": "__MSG_fullName__",
"short_name": "__MSG_Name__", "short_name": "__MSG_Name__",
"version": "1.0.33", "version": "1.0.35",
"default_locale": "en", "default_locale": "en",
"description": "__MSG_Description__", "description": "__MSG_Description__",
"content_scripts": [ "content_scripts": [
@@ -12,6 +12,7 @@
"all_frames": true, "all_frames": true,
"js": [ "js": [
"config.js", "config.js",
"utils/previewBar.js",
"utils.js", "utils.js",
"content.js", "content.js",
"popup.js" "popup.js"

View File

@@ -54,6 +54,14 @@ function runThePopup() {
// submitTimesInfoMessage // submitTimesInfoMessage
"submitTimesInfoMessageContainer", "submitTimesInfoMessageContainer",
"submitTimesInfoMessage", "submitTimesInfoMessage",
// Username
"setUsernameContainer",
"setUsernameButton",
"setUsernameStatusContainer",
"setUsernameStatus",
"setUsername",
"usernameInput",
"submitUsername",
// More // More
"submissionSection", "submissionSection",
"mainControls", "mainControls",
@@ -78,6 +86,8 @@ function runThePopup() {
SB.showDeleteButtonPlayerControls.addEventListener("click", showDeleteButtonPlayerControls); SB.showDeleteButtonPlayerControls.addEventListener("click", showDeleteButtonPlayerControls);
SB.disableSponsorViewTracking.addEventListener("click", disableSponsorViewTracking); SB.disableSponsorViewTracking.addEventListener("click", disableSponsorViewTracking);
SB.enableSponsorViewTracking.addEventListener("click", enableSponsorViewTracking); SB.enableSponsorViewTracking.addEventListener("click", enableSponsorViewTracking);
SB.setUsernameButton.addEventListener("click", setUsernameButton);
SB.submitUsername.addEventListener("click", submitUsername);
SB.optionsButton.addEventListener("click", openOptions); SB.optionsButton.addEventListener("click", openOptions);
SB.reportAnIssue.addEventListener("click", reportAnIssue); SB.reportAnIssue.addEventListener("click", reportAnIssue);
SB.hideDiscordButton.addEventListener("click", hideDiscordButton); SB.hideDiscordButton.addEventListener("click", hideDiscordButton);
@@ -374,7 +384,14 @@ function runThePopup() {
for (let i = 0; i < request.sponsorTimes.length; i++) { for (let i = 0; i < request.sponsorTimes.length; i++) {
let sponsorTimeButton = document.createElement("button"); let sponsorTimeButton = document.createElement("button");
sponsorTimeButton.className = "warningButton popupElement"; 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"); let votingButtons = document.createElement("div");
@@ -973,11 +990,64 @@ function runThePopup() {
} }
} }
//make the options div visisble //make the options div visible
function openOptions() { function openOptions() {
document.getElementById("optionsButtonContainer").style.display = "none"; document.getElementById("optionsButtonContainer").style.display = "none";
document.getElementById("options").style.display = "unset"; 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 //this is not a YouTube video page
function displayNoVideo() { function displayNoVideo() {

View File

@@ -114,6 +114,31 @@
</div> </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"> <div id="discordButtonContainer" class="popupElement" style="display: none">
<br/> <br/>
@@ -121,7 +146,7 @@
<br/> <br/>
Come join the official discord server to give suggestions and feedback! Come join the official discord server to give suggestions and feedback!
<br/> <br/>

86
utils/previewBar.js Normal file
View File

@@ -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;
}
}