diff --git a/ContentScript.js b/ContentScript.js index 4de796d5..7cb2ac9a 100644 --- a/ContentScript.js +++ b/ContentScript.js @@ -8,6 +8,11 @@ chrome.runtime.onMessage.addListener( // Detect URL Changes if (request.message === 'ytvideoid') { // Message from background script SponsorsLookup(request.id); } + + //message from popup script + if (request.message === 'sponsorStart') { + sponsorMessageStarted(); + } }); function SponsorsLookup(id) { @@ -38,3 +43,15 @@ function youtube_parser(url) { // Returns with video id else returns false var match = url.match(regExp); return (match && match[7].length == 11) ? match[7] : false; } + +function sponsorMessageStarted() { + let v = document.querySelector('video'); + + console.log(v.currentTime) + + //send back current time + chrome.runtime.sendMessage({ + message: "time", + time: v.currentTime + }); +} \ No newline at end of file diff --git a/background.js b/background.js index 21199ed1..20ed74c3 100644 --- a/background.js +++ b/background.js @@ -1,15 +1,20 @@ chrome.tabs.onUpdated.addListener( // On tab update function(tabId, changeInfo, tab) { - if (changeInfo.url && id = youtube_parser(changeInfo.url)) { // If URL changed and is youtube video message ContentScript the video id - chrome.tabs.sendMessage( tabId, { - message: 'ytvideoid', - id: id - }) + if (changeInfo != undefined && changeInfo.url != undefined) { + console.log(changeInfo) + let id = youtube_parser(changeInfo.url); + if (changeInfo.url && id) { // If URL changed and is youtube video message contentScript the video id + chrome.tabs.sendMessage( tabId, { + message: 'ytvideoid', + id: id + }) + } } } ); + function youtube_parser(url) { // Return video id or false var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/; var match = url.match(regExp); return (match && match[7].length == 11) ? match[7] : false; -} +} \ No newline at end of file diff --git a/manifest.json b/manifest.json index 6f52b86a..b9de834f 100644 --- a/manifest.json +++ b/manifest.json @@ -1,20 +1,25 @@ { "name": "YTSponsorSkip", "version": "1.0", - "description": "Skip youtube video sponsors", + "description": "Skip youtube video sponsors.", "content_scripts": [ { "matches": [ "https://*.youtube.com/*" ], "js": [ - "ContentScript.js" + "contentScript.js" ] } ], "permissions": [ - "tabs" + "tabs", + "storage" ], + "browser_action": { + "default_title": "SponsorBlock", + "default_popup": "popup.html" + }, "background": { "scripts":["background.js"] }, diff --git a/popup.css b/popup.css new file mode 100644 index 00000000..2c0193cd --- /dev/null +++ b/popup.css @@ -0,0 +1,7 @@ +* { + font-family: 'Arial'; +} + +body { + width: 300px; +} \ No newline at end of file diff --git a/popup.html b/popup.html new file mode 100644 index 00000000..ebeea867 --- /dev/null +++ b/popup.html @@ -0,0 +1,27 @@ + + + Set Page Color Popup + + +
+
+
+

SponsorBlock

+
+ +
+ +

Latest Sponsor Message Times Chosen

+ +
+ +
+
+ + +
+
+
+ + + \ No newline at end of file diff --git a/popup.js b/popup.js new file mode 100644 index 00000000..95edb9bd --- /dev/null +++ b/popup.js @@ -0,0 +1,92 @@ +document.getElementById("sponsorStart").addEventListener("click", sendSponsorStartMessage); +document.getElementById("clearTimes").addEventListener("click", clearTimes); + +//if true, the button now selects the end time +var startTimeChosen = false; + +//the start and end time pairs (2d) +var videoTimes = []; +//load video times +chrome.storage.local.get(['videoTimes'], function(result) { + if (result.videoTimes != undefined) { + videoTimes = result.videoTimes; + + if (videoTimes[videoTimes.length - 1].length < 2) { + startTimeChosen = true; + } + + displayVideoTimes(); + } +}); + +function sendSponsorStartMessage() { + //the content script will get the message if a YouTube page is open + chrome.tabs.query({ + active: true, + currentWindow: true + }, tabs => { + // ...and send a request for the DOM info... + chrome.tabs.sendMessage( + tabs[0].id, + {from: 'popup', subject: 'DOMInfo', message: 'sponsorStart'} + ); + }); +} + +chrome.runtime.onMessage.addListener(function (request, sender, callback) { + if (request.message == "time") { + let timeMessage = request.time.toFixed(2) + "s"; + + let videoTimesIndex = videoTimes.length - (startTimeChosen ? 1 : 0); + + if (videoTimes[videoTimesIndex] == undefined) { + videoTimes[videoTimesIndex] = []; + } + + videoTimes[videoTimesIndex][startTimeChosen ? 1 : 0] = request.time; + + chrome.storage.local.set({"videoTimes": videoTimes}); + + //update startTimeChosen variable + if (!startTimeChosen) { + startTimeChosen = true; + document.getElementById("sponsorStart").innerHTML = "Sponsorship Ends"; + } else { + startTimeChosen = false; + document.getElementById("sponsorStart").innerHTML = "Sponsorship Start"; + } + + //display video times on screen + displayVideoTimes(); + } +}); + +//display the video times from the array +function displayVideoTimes() { + //make sure the div is empty first + document.getElementById("sponsorMessageTimes").innerHTML = ""; + + for (let i = 0; i < videoTimes.length; i++) { + console.log(videoTimes) + for (let s = 0; s < videoTimes[i].length; s++) { + let timeMessage = videoTimes[i][s] + "s"; + //if this is an end time + if (s == 1) { + timeMessage = " to " + timeMessage; + } else if (i > 0) { + //add commas if necessary + timeMessage = ", " + timeMessage; + } + + document.getElementById("sponsorMessageTimes").innerHTML += timeMessage; + } + } +} + +function clearTimes() { + videoTimes = []; + + chrome.storage.local.set({"videoTimes": videoTimes}); + + displayVideoTimes(); +} \ No newline at end of file