mirror of
https://github.com/ajayyy/SponsorBlock.git
synced 2024-11-10 09:07:45 +01:00
Merge pull request #105 from ajayyy/experimental
Lowered permissions + Edit panel additions
This commit is contained in:
commit
5916baf5ea
6 changed files with 163 additions and 110 deletions
|
@ -1,27 +1,7 @@
|
|||
//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 URL changed and is youtube video message contentScript the video id
|
||||
if (changeInfo.url && id) {
|
||||
videoIDChange(id, tabId);
|
||||
}
|
||||
}
|
||||
chrome.tabs.sendMessage(tabId, {
|
||||
message: 'update',
|
||||
});
|
||||
});
|
||||
|
||||
chrome.runtime.onMessage.addListener(function (request, sender, callback) {
|
||||
|
@ -49,7 +29,14 @@ chrome.runtime.onMessage.addListener(function (request, sender, 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"
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
//add help page on install
|
||||
|
@ -154,7 +141,7 @@ function submitVote(type, UUID, callback) {
|
|||
function submitTimes(videoID, callback) {
|
||||
//get the video times from storage
|
||||
let sponsorTimeKey = 'sponsorTimes' + videoID;
|
||||
chrome.storage.sync.get([sponsorTimeKey], function(result) {
|
||||
chrome.storage.sync.get([sponsorTimeKey, "userID"], function(result) {
|
||||
let sponsorTimes = result[sponsorTimeKey];
|
||||
let userID = result.userID;
|
||||
|
||||
|
@ -193,56 +180,6 @@ function submitTimes(videoID, callback) {
|
|||
});
|
||||
}
|
||||
|
||||
function videoIDChange(currentVideoID, tabId) {
|
||||
//send a message to the content script
|
||||
chrome.tabs.sendMessage(tabId, {
|
||||
message: 'ytvideoid',
|
||||
id: currentVideoID
|
||||
});
|
||||
|
||||
chrome.storage.sync.get(["sponsorVideoID", "previousVideoID"], function(result) {
|
||||
const sponsorVideoID = result.sponsorVideoID;
|
||||
const previousVideoID = result.previousVideoID;
|
||||
|
||||
//not a url change
|
||||
if (sponsorVideoID == currentVideoID){
|
||||
return;
|
||||
}
|
||||
|
||||
chrome.storage.sync.set({
|
||||
"sponsorVideoID": currentVideoID
|
||||
});
|
||||
|
||||
//warn them if they had unsubmitted times
|
||||
if (previousVideoID != null) {
|
||||
//get the sponsor times from storage
|
||||
let sponsorTimeKey = 'sponsorTimes' + previousVideoID;
|
||||
chrome.storage.sync.get([sponsorTimeKey], function(result) {
|
||||
let sponsorTimes = result[sponsorTimeKey];
|
||||
|
||||
if (sponsorTimes != undefined && sponsorTimes.length > 0) {
|
||||
//warn them that they have unsubmitted sponsor times
|
||||
chrome.notifications.create("stillThere" + Math.random(), {
|
||||
type: "basic",
|
||||
title: "Do you want to submit the sponsor times for watch?v=" + 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"
|
||||
});
|
||||
}
|
||||
|
||||
//set the previous video id to the currentID
|
||||
chrome.storage.sync.set({
|
||||
"previousVideoID": currentVideoID
|
||||
});
|
||||
});
|
||||
} else {
|
||||
chrome.storage.sync.set({
|
||||
"previousVideoID": currentVideoID
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function sendRequestToServer(type, address, callback) {
|
||||
let xmlhttp = new XMLHttpRequest();
|
||||
|
||||
|
|
54
content.js
54
content.js
|
@ -1,6 +1,6 @@
|
|||
//was sponsor data found when doing SponsorsLookup
|
||||
var sponsorDataFound = false;
|
||||
|
||||
var previousVideoID = null;
|
||||
//the actual sponsorTimes if loaded and UUIDs associated with them
|
||||
var sponsorTimes = null;
|
||||
var UUIDs = null;
|
||||
|
@ -81,14 +81,14 @@ chrome.storage.sync.get(["dontShowNoticeAgain"], function(result) {
|
|||
|
||||
//get messages from the background script and the popup
|
||||
chrome.runtime.onMessage.addListener(messageListener);
|
||||
|
||||
|
||||
function messageListener(request, sender, sendResponse) {
|
||||
//message from background script
|
||||
if (request.message == "ytvideoid") {
|
||||
videoIDChange(request.id);
|
||||
}
|
||||
|
||||
//messages from popup script
|
||||
|
||||
if (request.message == "update") {
|
||||
if(id = getYouTubeVideoID(document.URL)) videoIDChange(id);
|
||||
}
|
||||
|
||||
if (request.message == "sponsorStart") {
|
||||
sponsorMessageStarted(sendResponse);
|
||||
}
|
||||
|
@ -119,6 +119,16 @@ function messageListener(request, sender, sendResponse) {
|
|||
})
|
||||
}
|
||||
|
||||
if (request.message == "skipToTime") {
|
||||
v.currentTime = request.time;
|
||||
}
|
||||
|
||||
if (request.message == "getCurrentTime") {
|
||||
sendResponse({
|
||||
currentTime: v.currentTime
|
||||
});
|
||||
}
|
||||
|
||||
if (request.message == "getChannelURL") {
|
||||
sendResponse({
|
||||
channelURL: channelURL
|
||||
|
@ -183,11 +193,33 @@ document.onkeydown = function(e){
|
|||
}
|
||||
|
||||
function videoIDChange(id) {
|
||||
//not a url change
|
||||
if (sponsorVideoID == id){
|
||||
return;
|
||||
}
|
||||
|
||||
//not a url change
|
||||
if (sponsorVideoID == id) return;
|
||||
|
||||
//warn them if they had unsubmitted times
|
||||
if (previousVideoID != null) {
|
||||
//get the sponsor times from storage
|
||||
let sponsorTimeKey = 'sponsorTimes' + previousVideoID;
|
||||
chrome.storage.sync.get([sponsorTimeKey], function(result) {
|
||||
let sponsorTimes = result[sponsorTimeKey];
|
||||
|
||||
if (sponsorTimes != undefined && sponsorTimes.length > 0) {
|
||||
//warn them that they have unsubmitted sponsor times
|
||||
chrome.runtime.sendMessage({
|
||||
message: "alertPrevious",
|
||||
previousVideoID: previousVideoID
|
||||
})
|
||||
}
|
||||
|
||||
//set the previous video id to the currentID
|
||||
previousVideoID = id;
|
||||
});
|
||||
} else {
|
||||
//set the previous id now, don't wait for chrome.storage.get
|
||||
previousVideoID = id;
|
||||
}
|
||||
|
||||
//close popup
|
||||
closeInfoMenu();
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"name": "SponsorBlock for YouTube - Skip Sponsorships",
|
||||
"short_name": "SponsorBlock",
|
||||
"version": "1.0.31",
|
||||
"version": "1.0.33",
|
||||
"description": "Skip over sponsorship on YouTube videos. Report sponsors on videos you watch to save the time of others.",
|
||||
"content_scripts": [
|
||||
{
|
||||
|
@ -36,7 +36,6 @@
|
|||
"popup.html"
|
||||
],
|
||||
"permissions": [
|
||||
"tabs",
|
||||
"storage",
|
||||
"notifications",
|
||||
"https://sponsor.ajay.app/*"
|
||||
|
|
10
popup.css
10
popup.css
|
@ -80,8 +80,14 @@ h1.popupElement {
|
|||
|
||||
.mediumLink.popupElement {
|
||||
font-size: 15px;
|
||||
padding-left: 15px;
|
||||
padding-right: 15px;
|
||||
margin-left: 25px;
|
||||
margin-right: 25px;
|
||||
text-decoration: underline;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.tinyLink.popupElement {
|
||||
font-size: 10px;
|
||||
text-decoration: underline;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
|
|
@ -93,6 +93,8 @@
|
|||
|
||||
</div>
|
||||
</b>
|
||||
|
||||
<br/>
|
||||
|
||||
<button id="clearTimes" class="smallButton popupElement">Clear Times</button>
|
||||
|
||||
|
|
117
popup.js
117
popup.js
|
@ -201,17 +201,25 @@ function runThePopup() {
|
|||
}
|
||||
});
|
||||
|
||||
|
||||
chrome.tabs.query({
|
||||
active: true,
|
||||
currentWindow: true
|
||||
}, loadTabData);
|
||||
}, onTabs);
|
||||
|
||||
function onTabs(tabs) {
|
||||
chrome.tabs.sendMessage(tabs[0].id, {message: 'getVideoID'}, function(result) {
|
||||
if (result != undefined && result.videoID) {
|
||||
currentVideoID = result.videoID;
|
||||
|
||||
loadTabData(tabs);
|
||||
} else if (result == undefined && chrome.runtime.lastError) {
|
||||
//this isn't a YouTube video then, or at least the content script is not loaded
|
||||
displayNoVideo();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function loadTabData(tabs) {
|
||||
//set current videoID
|
||||
currentVideoID = getYouTubeVideoID(tabs[0].url);
|
||||
|
||||
if (!currentVideoID) {
|
||||
//this isn't a YouTube video then
|
||||
displayNoVideo();
|
||||
|
@ -465,8 +473,11 @@ function runThePopup() {
|
|||
let index = i;
|
||||
deleteButton.addEventListener("click", () => deleteSponsorTime(index));
|
||||
|
||||
let spacer = document.createElement("span");
|
||||
spacer.innerText = " ";
|
||||
let previewButton = document.createElement("span");
|
||||
previewButton.id = "sponsorTimePreviewButton" + i;
|
||||
previewButton.innerText = "Preview";
|
||||
previewButton.className = "mediumLink popupElement";
|
||||
previewButton.addEventListener("click", () => previewSponsorTime(index));
|
||||
|
||||
let editButton = document.createElement("span");
|
||||
editButton.id = "sponsorTimeEditButton" + i;
|
||||
|
@ -488,19 +499,46 @@ function runThePopup() {
|
|||
}
|
||||
|
||||
currentSponsorTimeContainer.innerText = currentSponsorTimeMessage;
|
||||
currentSponsorTimeContainer.addEventListener("click", () => editSponsorTime(index));
|
||||
|
||||
sponsorTimesContainer.appendChild(currentSponsorTimeContainer);
|
||||
sponsorTimesContainer.appendChild(deleteButton);
|
||||
|
||||
//only if it is a complete sponsor time
|
||||
if (sponsorTimes[i].length > 1) {
|
||||
sponsorTimesContainer.appendChild(previewButton);
|
||||
sponsorTimesContainer.appendChild(editButton);
|
||||
|
||||
currentSponsorTimeContainer.addEventListener("click", () => editSponsorTime(index));
|
||||
}
|
||||
}
|
||||
|
||||
return sponsorTimesContainer;
|
||||
}
|
||||
|
||||
function previewSponsorTime(index) {
|
||||
let skipTime = sponsorTimes[index][0];
|
||||
|
||||
if (document.getElementById("startTimeMinutes" + index) != null) {
|
||||
//edit is currently open, use that time
|
||||
|
||||
skipTime = getSponsorTimeEditTimes("startTime", index);
|
||||
|
||||
//save the edit
|
||||
saveSponsorTimeEdit(index, false);
|
||||
}
|
||||
|
||||
chrome.tabs.query({
|
||||
active: true,
|
||||
currentWindow: true
|
||||
}, tabs => {
|
||||
chrome.tabs.sendMessage(
|
||||
tabs[0].id, {
|
||||
message: "skipToTime",
|
||||
time: skipTime - 2
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
function editSponsorTime(index) {
|
||||
if (document.getElementById("startTimeMinutes" + index) != null) {
|
||||
|
@ -513,6 +551,13 @@ function runThePopup() {
|
|||
|
||||
let sponsorTimeContainer = document.getElementById("sponsorTimeContainer" + index);
|
||||
|
||||
//the button to set the current time
|
||||
let startTimeNowButton = document.createElement("span");
|
||||
startTimeNowButton.id = "startTimeNowButton" + index;
|
||||
startTimeNowButton.innerText = "(Now)";
|
||||
startTimeNowButton.className = "tinyLink popupElement";
|
||||
startTimeNowButton.addEventListener("click", () => setEditTimeToCurrentTime("startTime", index));
|
||||
|
||||
//get sponsor time minutes and seconds boxes
|
||||
let startTimeMinutes = document.createElement("input");
|
||||
startTimeMinutes.id = "startTimeMinutes" + index;
|
||||
|
@ -527,7 +572,7 @@ function runThePopup() {
|
|||
startTimeSeconds.type = "text";
|
||||
startTimeSeconds.value = getTimeInFormattedSeconds(sponsorTimes[index][0]);
|
||||
startTimeSeconds.style.width = "60px";
|
||||
|
||||
|
||||
let endTimeMinutes = document.createElement("input");
|
||||
endTimeMinutes.id = "endTimeMinutes" + index;
|
||||
endTimeMinutes.className = "sponsorTime popupElement";
|
||||
|
@ -541,6 +586,13 @@ function runThePopup() {
|
|||
endTimeSeconds.type = "text";
|
||||
endTimeSeconds.value = getTimeInFormattedSeconds(sponsorTimes[index][1]);
|
||||
endTimeSeconds.style.width = "60px";
|
||||
|
||||
//the button to set the current time
|
||||
let endTimeNowButton = document.createElement("span");
|
||||
endTimeNowButton.id = "endTimeNowButton" + index;
|
||||
endTimeNowButton.innerText = "(Now)";
|
||||
endTimeNowButton.className = "tinyLink popupElement";
|
||||
endTimeNowButton.addEventListener("click", () => setEditTimeToCurrentTime("endTime", index));
|
||||
|
||||
let colonText = document.createElement("span");
|
||||
colonText.innerText = ":";
|
||||
|
@ -553,6 +605,7 @@ function runThePopup() {
|
|||
sponsorTimeContainer.removeChild(sponsorTimeContainer.firstChild);
|
||||
}
|
||||
|
||||
sponsorTimeContainer.appendChild(startTimeNowButton);
|
||||
sponsorTimeContainer.appendChild(startTimeMinutes);
|
||||
sponsorTimeContainer.appendChild(colonText);
|
||||
sponsorTimeContainer.appendChild(startTimeSeconds);
|
||||
|
@ -560,6 +613,7 @@ function runThePopup() {
|
|||
sponsorTimeContainer.appendChild(endTimeMinutes);
|
||||
sponsorTimeContainer.appendChild(colonText);
|
||||
sponsorTimeContainer.appendChild(endTimeSeconds);
|
||||
sponsorTimeContainer.appendChild(endTimeNowButton);
|
||||
|
||||
//add save button and remove edit button
|
||||
let saveButton = document.createElement("span");
|
||||
|
@ -573,16 +627,37 @@ function runThePopup() {
|
|||
|
||||
sponsorTimesContainer.replaceChild(saveButton, editButton);
|
||||
}
|
||||
|
||||
function setEditTimeToCurrentTime(idStartName, index) {
|
||||
chrome.tabs.query({
|
||||
active: true,
|
||||
currentWindow: true
|
||||
}, tabs => {
|
||||
chrome.tabs.sendMessage(
|
||||
tabs[0].id,
|
||||
{message: "getCurrentTime"},
|
||||
function (response) {
|
||||
let minutes = document.getElementById(idStartName + "Minutes" + index);
|
||||
let seconds = document.getElementById(idStartName + "Seconds" + index);
|
||||
|
||||
minutes.value = getTimeInMinutes(response.currentTime);
|
||||
seconds.value = getTimeInFormattedSeconds(response.currentTime);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
//id start name is whether it is the startTime or endTime
|
||||
//gives back the time in seconds
|
||||
function getSponsorTimeEditTimes(idStartName, index) {
|
||||
let minutes = document.getElementById(idStartName + "Minutes" + index);
|
||||
let seconds = document.getElementById(idStartName + "Seconds" + index);
|
||||
|
||||
return parseInt(minutes.value) * 60 + parseFloat(seconds.value);
|
||||
}
|
||||
|
||||
function saveSponsorTimeEdit(index) {
|
||||
let startTimeMinutes = document.getElementById("startTimeMinutes" + index);
|
||||
let startTimeSeconds = document.getElementById("startTimeSeconds" + index);
|
||||
|
||||
let endTimeMinutes = document.getElementById("endTimeMinutes" + index);
|
||||
let endTimeSeconds = document.getElementById("endTimeSeconds" + index);
|
||||
|
||||
sponsorTimes[index][0] = parseInt(startTimeMinutes.value) * 60 + parseFloat(startTimeSeconds.value);
|
||||
sponsorTimes[index][1] = parseInt(endTimeMinutes.value) * 60 + parseFloat(endTimeSeconds.value);
|
||||
function saveSponsorTimeEdit(index, closeEditMode = true) {
|
||||
sponsorTimes[index][0] = getSponsorTimeEditTimes("startTime", index);
|
||||
sponsorTimes[index][1] = getSponsorTimeEditTimes("endTime", index);
|
||||
|
||||
//save this
|
||||
let sponsorTimeKey = "sponsorTimes" + currentVideoID;
|
||||
|
@ -598,9 +673,11 @@ function runThePopup() {
|
|||
});
|
||||
});
|
||||
|
||||
displaySponsorTimes();
|
||||
if (closeEditMode) {
|
||||
displaySponsorTimes();
|
||||
|
||||
showSubmitTimesIfNecessary();
|
||||
showSubmitTimesIfNecessary();
|
||||
}
|
||||
}
|
||||
|
||||
//deletes the sponsor time submitted at an index
|
||||
|
|
Loading…
Reference in a new issue