2019-07-09 20:50:19 +02:00
//was sponsor data found when doing SponsorsLookup
var sponsorDataFound = false ;
2019-07-16 00:56:16 +02:00
//the actual sponsorTimes if loaded and UUIDs associated with them
2019-07-28 23:22:05 +02:00
var sponsorTimes = null ;
var UUIDs = null ;
//what video id are these sponsors for
var sponsorVideoID = null ;
2019-08-03 04:37:12 +02:00
//the time this video is starting at when first played, if not zero
var youtubeVideoStartTime = null ;
2019-07-28 23:22:05 +02:00
if ( id = getYouTubeVideoID ( document . URL ) ) { // Direct Links
videoIDChange ( id ) ;
}
2019-07-10 00:03:56 +02:00
2019-07-09 21:05:16 +02:00
//the video
var v ;
2019-08-04 03:35:41 +02:00
//the channel this video is about
var channelURL ;
//is this channel whitelised from getting sponsors skipped
var channelWhitelisted = false ;
2019-07-09 21:05:16 +02:00
//the last time looked at (used to see if this time is in the interval)
2019-07-26 02:00:07 +02:00
var lastTime = - 1 ;
2019-01-18 20:49:43 +01:00
2019-07-26 02:00:07 +02:00
//the actual time (not video time) that the last skip happened
var lastUnixTimeSkipped = - 1 ;
2019-07-24 01:37:33 +02:00
2019-07-10 03:44:41 +02:00
//the last time in the video a sponsor was skipped
//used for the go back button
var lastSponsorTimeSkipped = null ;
2019-07-16 00:56:16 +02:00
//used for ratings
var lastSponsorTimeSkippedUUID = null ;
2019-07-10 03:44:41 +02:00
2019-07-12 22:48:07 +02:00
//if showing the start sponsor button or the end sponsor button on the player
var showingStartSponsor = true ;
2019-07-13 01:04:24 +02:00
//should the video controls buttons be added
var hideVideoPlayerControls = false ;
2019-07-30 03:40:13 +02:00
var hideInfoButtonPlayerControls = false ;
var hideDeleteButtonPlayerControls = false ;
2019-07-13 01:04:24 +02:00
2019-08-02 02:01:33 +02:00
//the downloaded sponsor times
var sponsorTimes = [ ] ;
var UUIDs = [ ] ;
//the sponsor times being prepared to be submitted
var sponsorTimesSubmitting = [ ] ;
2019-07-29 21:42:14 +02:00
//becomes true when isInfoFound is called
//this is used to close the popup on YouTube when the other popup opens
var popupInitialised = false ;
2019-07-24 03:06:36 +02:00
//should view counts be tracked
var trackViewCount = false ;
chrome . storage . sync . get ( [ "trackViewCount" ] , function ( result ) {
let trackViewCountStorage = result . trackViewCount ;
if ( trackViewCountStorage != undefined ) {
trackViewCount = trackViewCountStorage ;
} else {
trackViewCount = true ;
}
} ) ;
2019-07-10 04:10:25 +02:00
//if the notice should not be shown
//happens when the user click's the "Don't show notice again" button
var dontShowNotice = false ;
2019-07-22 22:42:57 +02:00
chrome . storage . sync . get ( [ "dontShowNoticeAgain" ] , function ( result ) {
2019-07-10 04:10:25 +02:00
let dontShowNoticeAgain = result . dontShowNoticeAgain ;
if ( dontShowNoticeAgain != undefined ) {
dontShowNotice = dontShowNoticeAgain ;
}
} ) ;
2019-07-30 19:27:20 +02:00
//get messages from the background script and the popup
chrome . runtime . onMessage . addListener ( messageListener ) ;
function messageListener ( request , sender , sendResponse ) {
2019-07-09 23:03:44 +02:00
//message from background script
2019-08-08 15:02:06 +02:00
if ( request . message == "TabUpdate" ) {
if ( id = getYouTubeVideoID ( document . URL ) ) videoIDChange ( id ) ;
2019-01-15 19:29:25 +01:00
}
2019-07-09 05:43:06 +02:00
2019-07-09 06:05:27 +02:00
//messages from popup script
2019-07-10 04:10:25 +02:00
if ( request . message == "sponsorStart" ) {
2019-07-29 21:42:14 +02:00
sponsorMessageStarted ( sendResponse ) ;
2019-07-09 05:43:06 +02:00
}
2019-07-09 06:05:27 +02:00
2019-08-02 02:01:33 +02:00
if ( request . message == "sponsorDataChanged" ) {
updateSponsorTimesSubmitting ( ) ;
}
2019-07-10 04:10:25 +02:00
if ( request . message == "isInfoFound" ) {
2019-07-10 00:03:56 +02:00
//send the sponsor times along with if it's found
2019-07-09 06:05:27 +02:00
sendResponse ( {
2019-07-10 00:03:56 +02:00
found : sponsorDataFound ,
2019-07-18 04:53:42 +02:00
sponsorTimes : sponsorTimes ,
UUIDs : UUIDs
2019-07-29 21:42:14 +02:00
} ) ;
if ( popupInitialised && document . getElementById ( "sponsorBlockPopupContainer" ) != null ) {
//the popup should be closed now that another is opening
closeInfoMenu ( ) ;
}
popupInitialised = true ;
2019-07-09 06:05:27 +02:00
}
2019-07-09 21:55:33 +02:00
2019-07-10 04:10:25 +02:00
if ( request . message == "getVideoID" ) {
2019-07-09 21:55:33 +02:00
sendResponse ( {
videoID : getYouTubeVideoID ( document . URL )
} )
}
2019-07-10 04:10:25 +02:00
2019-08-04 03:35:41 +02:00
if ( request . message == "getChannelURL" ) {
sendResponse ( {
channelURL : channelURL
} )
}
if ( request . message == "isChannelWhitelisted" ) {
sendResponse ( {
value : channelWhitelisted
} )
}
if ( request . message == "whitelistChange" ) {
channelWhitelisted = request . value ;
sponsorsLookup ( getYouTubeVideoID ( document . URL ) ) ;
}
2019-07-10 04:10:25 +02:00
if ( request . message == "showNoticeAgain" ) {
dontShowNotice = false ;
}
2019-07-12 23:45:20 +02:00
2019-07-22 00:19:56 +02:00
if ( request . message == "changeStartSponsorButton" ) {
2019-07-22 22:46:50 +02:00
changeStartSponsorButton ( request . showStartSponsor , request . uploadButtonVisible ) ;
2019-07-12 23:45:20 +02:00
}
2019-07-13 01:04:24 +02:00
if ( request . message == "changeVideoPlayerControlsVisibility" ) {
hideVideoPlayerControls = request . value ;
2019-07-30 03:40:13 +02:00
updateVisibilityOfPlayerControlsButton ( ) ;
} else if ( request . message == "changeInfoButtonPlayerControlsVisibility" ) {
hideInfoButtonPlayerControls = request . value ;
updateVisibilityOfPlayerControlsButton ( ) ;
} else if ( request . message == "changeDeleteButtonPlayerControlsVisibility" ) {
hideDeleteButtonPlayerControls = request . value ;
2019-07-13 01:04:24 +02:00
updateVisibilityOfPlayerControlsButton ( ) ;
}
2019-07-24 03:06:36 +02:00
if ( request . message == "trackViewCount" ) {
trackViewCount = request . value ;
}
2019-07-30 19:27:20 +02:00
}
2018-06-23 17:09:05 +02:00
2019-07-26 22:20:07 +02:00
//check for hotkey pressed
document . onkeydown = function ( e ) {
e = e || window . event ;
var key = e . which || e . keyCode ;
let video = document . getElementById ( "movie_player" ) ;
//is the video in focus, otherwise they could be typing a comment
if ( document . activeElement === video ) {
if ( key == 186 ) {
//semicolon
startSponsorClicked ( ) ;
} else if ( key == 222 ) {
//single quote
submitSponsorTimes ( ) ;
}
}
}
2019-07-13 00:28:41 +02:00
function videoIDChange ( id ) {
2019-07-28 23:22:05 +02:00
2019-08-08 15:44:48 +02:00
//not a url change
if ( sponsorVideoID == id ) return ;
chrome . storage . sync . set ( { videoid : id } ) ;
2019-08-04 05:30:45 +02:00
//close popup
closeInfoMenu ( ) ;
2019-07-26 02:00:07 +02:00
//reset last sponsor times
lastTime = - 1 ;
lastUnixTimeSkipped = - 1 ;
2019-07-27 02:19:51 +02:00
//reset sponsor times
2019-07-28 23:22:05 +02:00
sponsorTimes = null ;
UUIDs = null ;
sponsorVideoID = id ;
2019-07-27 02:19:51 +02:00
2019-08-03 04:37:12 +02:00
//see if there is a video start time
youtubeVideoStartTime = getYouTubeVideoStartTime ( document . URL ) ;
2019-07-13 00:28:41 +02:00
//reset sponsor data found check
sponsorDataFound = false ;
sponsorsLookup ( id ) ;
2019-08-02 18:18:56 +02:00
//make sure everything is properly added
2019-08-03 20:11:47 +02:00
updateVisibilityOfPlayerControlsButton ( true ) ;
2019-08-02 18:18:56 +02:00
2019-08-02 02:01:33 +02:00
//reset sponsor times submitting
sponsorTimesSubmitting = [ ] ;
2019-07-13 00:28:41 +02:00
//see if the onvideo control image needs to be changed
chrome . runtime . sendMessage ( {
message : "getSponsorTimes" ,
videoID : id
} , function ( response ) {
if ( response != undefined ) {
let sponsorTimes = response . sponsorTimes ;
2019-07-28 23:22:05 +02:00
if ( sponsorTimes != null && sponsorTimes . length > 0 && sponsorTimes [ sponsorTimes . length - 1 ] . length >= 2 ) {
2019-07-29 00:48:28 +02:00
changeStartSponsorButton ( true , true ) ;
2019-07-28 23:22:05 +02:00
} else if ( sponsorTimes != null && sponsorTimes . length > 0 && sponsorTimes [ sponsorTimes . length - 1 ] . length < 2 ) {
2019-07-29 00:48:28 +02:00
changeStartSponsorButton ( false , true ) ;
} else {
2019-07-30 02:54:10 +02:00
changeStartSponsorButton ( true , false ) ;
2019-07-13 00:28:41 +02:00
}
2019-08-02 02:01:33 +02:00
//see if this data should be saved in the sponsorTimesSubmitting variable
if ( sponsorTimes != undefined && sponsorTimes . length > 0 ) {
sponsorTimesSubmitting = sponsorTimes ;
}
2019-07-13 00:28:41 +02:00
}
} ) ;
2019-07-13 01:04:24 +02:00
2019-07-30 03:40:13 +02:00
//see if video controls buttons should be added
2019-07-22 22:42:57 +02:00
chrome . storage . sync . get ( [ "hideVideoPlayerControls" ] , function ( result ) {
2019-07-13 01:04:24 +02:00
if ( result . hideVideoPlayerControls != undefined ) {
hideVideoPlayerControls = result . hideVideoPlayerControls ;
}
updateVisibilityOfPlayerControlsButton ( ) ;
} ) ;
2019-07-30 03:40:13 +02:00
chrome . storage . sync . get ( [ "hideInfoButtonPlayerControls" ] , function ( result ) {
if ( result . hideInfoButtonPlayerControls != undefined ) {
hideInfoButtonPlayerControls = result . hideInfoButtonPlayerControls ;
}
updateVisibilityOfPlayerControlsButton ( ) ;
} ) ;
chrome . storage . sync . get ( [ "hideDeleteButtonPlayerControls" ] , function ( result ) {
if ( result . hideDeleteButtonPlayerControls != undefined ) {
hideDeleteButtonPlayerControls = result . hideDeleteButtonPlayerControls ;
}
2019-08-03 20:11:47 +02:00
updateVisibilityOfPlayerControlsButton ( false ) ;
2019-07-30 03:40:13 +02:00
} ) ;
2019-07-13 00:28:41 +02:00
}
2019-07-09 20:50:19 +02:00
function sponsorsLookup ( id ) {
2019-08-02 02:01:33 +02:00
v = document . querySelector ( 'video' ) // Youtube video player
2019-08-02 18:18:56 +02:00
//there is no video here
2019-08-03 20:11:47 +02:00
if ( v == null ) {
setTimeout ( ( ) => sponsorsLookup ( id ) , 100 ) ;
return ;
}
2019-08-02 02:01:33 +02:00
//check database for sponsor times
sendRequestToServer ( 'GET' , "/api/getVideoSponsorTimes?videoID=" + id , function ( xmlhttp ) {
if ( xmlhttp . readyState == 4 && xmlhttp . status == 200 ) {
sponsorDataFound = true ;
sponsorTimes = JSON . parse ( xmlhttp . responseText ) . sponsorTimes ;
UUIDs = JSON . parse ( xmlhttp . responseText ) . UUIDs ;
2019-08-04 03:35:41 +02:00
getChannelID ( ) ;
2019-08-02 02:01:33 +02:00
} else if ( xmlhttp . readyState == 4 ) {
sponsorDataFound = false ;
//check if this video was uploaded recently
//use the invidious api to get the time published
sendRequestToCustomServer ( 'GET' , "https://invidio.us/api/v1/videos/" + id , function ( xmlhttp , error ) {
if ( xmlhttp . readyState == 4 && xmlhttp . status == 200 ) {
let unixTimePublished = JSON . parse ( xmlhttp . responseText ) . published ;
//if less than 3 days old
if ( ( Date . now ( ) / 1000 ) - unixTimePublished < 259200 ) {
setTimeout ( ( ) => sponsorsLookup ( id ) , 10000 ) ;
2019-07-22 03:08:23 +02:00
}
2019-08-02 02:01:33 +02:00
}
} ) ;
2019-07-26 02:00:07 +02:00
}
2019-08-02 02:01:33 +02:00
} ) ;
2019-07-26 02:00:07 +02:00
2019-08-02 02:01:33 +02:00
//add the event to run on the videos "ontimeupdate"
v . ontimeupdate = function ( ) {
sponsorCheck ( ) ;
} ;
}
2019-07-24 01:33:12 +02:00
2019-08-04 03:35:41 +02:00
function getChannelID ( ) {
//get channel id
let channelContainers = document . querySelectorAll ( "#owner-name" ) ;
let channelURLContainer = null ;
for ( let i = 0 ; i < channelContainers . length ; i ++ ) {
if ( channelContainers [ i ] . firstElementChild != null ) {
channelURLContainer = channelContainers [ i ] . firstElementChild ;
}
}
2019-08-04 04:12:20 +02:00
if ( channelContainers . length == 0 ) {
//old YouTube theme
channelContainers = document . getElementsByClassName ( "yt-user-info" ) ;
if ( channelContainers . length != 0 ) {
channelURLContainer = channelContainers [ 0 ] . firstElementChild ;
}
}
2019-08-04 03:35:41 +02:00
if ( channelURLContainer == null ) {
//try later
setTimeout ( getChannelID , 100 ) ;
return ;
}
channelURL = channelURLContainer . getAttribute ( "href" ) ;
//see if this is a whitelisted channel
chrome . storage . sync . get ( [ "whitelistedChannels" ] , function ( result ) {
let whitelistedChannels = result . whitelistedChannels ;
if ( whitelistedChannels != undefined && whitelistedChannels . includes ( channelURL ) ) {
//reset sponsor times to nothing
sponsorTimes = [ ] ;
UUIDs = [ ] ;
channelWhitelisted = true ;
}
} ) ;
}
2019-08-02 02:01:33 +02:00
//video skipping
function sponsorCheck ( ) {
let skipHappened = false ;
2019-07-24 01:33:12 +02:00
2019-08-02 02:01:33 +02:00
if ( sponsorTimes != null ) {
//see if any sponsor start time was just passed
for ( let i = 0 ; i < sponsorTimes . length ; i ++ ) {
//if something was skipped
2019-08-02 02:21:45 +02:00
if ( checkSponsorTime ( sponsorTimes , i , true ) ) {
2019-08-02 02:01:33 +02:00
skipHappened = true ;
break ;
}
}
}
2019-07-24 01:33:12 +02:00
2019-08-02 02:01:33 +02:00
if ( ! skipHappened ) {
//check for the "preview" sponsors (currently edited by this user)
for ( let i = 0 ; i < sponsorTimesSubmitting . length ; i ++ ) {
//must be a finished sponsor and be valid
if ( sponsorTimesSubmitting [ i ] . length > 1 && sponsorTimesSubmitting [ i ] [ 1 ] > sponsorTimesSubmitting [ i ] [ 0 ] ) {
checkSponsorTime ( sponsorTimesSubmitting , i , false ) ;
2019-07-24 03:06:36 +02:00
}
2019-07-16 00:56:16 +02:00
}
2019-07-24 01:33:12 +02:00
}
2019-07-26 02:00:07 +02:00
//don't keep track until they are loaded in
2019-08-02 02:01:33 +02:00
if ( sponsorTimes != null || sponsorTimesSubmitting . length > 0 ) {
2019-07-26 02:00:07 +02:00
lastTime = v . currentTime ;
}
2018-06-23 17:09:05 +02:00
}
2019-01-18 20:49:43 +01:00
2019-08-02 02:01:33 +02:00
function checkSponsorTime ( sponsorTimes , index , openNotice ) {
//this means part of the video was just skipped
if ( Math . abs ( v . currentTime - lastTime ) > 1 && lastTime != - 1 ) {
//make lastTime as if the video was playing normally
lastTime = v . currentTime - 0.0001 ;
}
if ( checkIfTimeToSkip ( v . currentTime , sponsorTimes [ index ] [ 0 ] ) ) {
//skip it
skipToTime ( v , index , sponsorTimes , openNotice ) ;
//something was skipped
return true ;
}
return false ;
}
function checkIfTimeToSkip ( currentVideoTime , startTime ) {
let currentTime = Date . now ( ) ;
//If the sponsor time is in between these times, skip it
//Checks if the last time skipped to is not too close to now, to make sure not to get too many
// sponsor times in a row (from one troll)
2019-08-03 04:37:12 +02:00
//the last term makes 0 second start times possible only if the video is not setup to start at a different time from zero
2019-08-02 02:01:33 +02:00
return ( Math . abs ( currentVideoTime - startTime ) < 0.3 && startTime >= lastTime && startTime <= currentVideoTime &&
2019-08-03 04:37:12 +02:00
( lastUnixTimeSkipped == - 1 || currentTime - lastUnixTimeSkipped > 500 ) ) || ( lastTime == - 1 && startTime == 0 && youtubeVideoStartTime == null )
2019-08-02 02:01:33 +02:00
}
//skip fromt he start time to the end time for a certain index sponsor time
function skipToTime ( v , index , sponsorTimes , openNotice ) {
v . currentTime = sponsorTimes [ index ] [ 1 ] ;
lastSponsorTimeSkipped = sponsorTimes [ index ] [ 0 ] ;
let currentUUID = UUIDs [ index ] ;
lastSponsorTimeSkippedUUID = currentUUID ;
if ( openNotice ) {
//send out the message saying that a sponsor message was skipped
openSkipNotice ( currentUUID ) ;
setTimeout ( ( ) => closeSkipNotice ( currentUUID ) , 7000 ) ;
//send telemetry that a this sponsor was skipped happened
if ( trackViewCount ) {
sendRequestToServer ( "GET" , "/api/viewedVideoSponsorTime?UUID=" + currentUUID ) ;
}
}
}
2019-07-18 01:42:13 +02:00
function goBackToPreviousTime ( UUID ) {
2019-07-28 23:22:05 +02:00
if ( sponsorTimes != null ) {
2019-07-10 03:44:41 +02:00
//add a tiny bit of time to make sure it is not skipped again
2019-07-18 01:42:13 +02:00
v . currentTime = sponsorTimes [ UUIDs . indexOf ( UUID ) ] [ 0 ] + 0.001 ;
2019-07-10 03:44:41 +02:00
2019-07-18 01:42:13 +02:00
closeSkipNotice ( UUID ) ;
2019-07-10 03:44:41 +02:00
}
}
2019-07-12 16:42:39 +02:00
//Adds a sponsorship starts button to the player controls
2019-07-12 22:44:50 +02:00
function addPlayerControlsButton ( ) {
2019-07-14 19:10:53 +02:00
if ( document . getElementById ( "startSponsorButton" ) != null ) {
//it's already added
return ;
}
2019-07-12 16:42:39 +02:00
let startSponsorButton = document . createElement ( "button" ) ;
2019-07-13 01:04:24 +02:00
startSponsorButton . id = "startSponsorButton" ;
2019-07-23 03:11:37 +02:00
startSponsorButton . className = "ytp-button playerButton" ;
2019-07-12 16:42:39 +02:00
startSponsorButton . setAttribute ( "title" , "Sponsor Starts Now" ) ;
2019-07-12 22:44:50 +02:00
startSponsorButton . addEventListener ( "click" , startSponsorClicked ) ;
2019-07-12 16:42:39 +02:00
let startSponsorImage = document . createElement ( "img" ) ;
2019-07-12 22:48:07 +02:00
startSponsorImage . id = "startSponsorImage" ;
2019-07-23 03:11:37 +02:00
startSponsorImage . className = "playerButtonImage" ;
2019-07-12 16:42:39 +02:00
startSponsorImage . src = chrome . extension . getURL ( "icons/PlayerStartIconSponsorBlocker256px.png" ) ;
//add the image to the button
startSponsorButton . appendChild ( startSponsorImage ) ;
2019-08-02 18:18:56 +02:00
let controls = document . getElementsByClassName ( "ytp-right-controls" ) ;
let referenceNode = controls [ controls . length - 1 ] ;
2019-08-03 20:11:47 +02:00
if ( referenceNode == undefined ) {
//page not loaded yet
setTimeout ( addPlayerControlsButton , 100 ) ;
return ;
}
2019-07-12 16:42:39 +02:00
referenceNode . prepend ( startSponsorButton ) ;
}
2019-07-13 01:04:24 +02:00
function removePlayerControlsButton ( ) {
document . getElementById ( "startSponsorButton" ) . style . display = "none" ;
2019-07-22 00:19:56 +02:00
document . getElementById ( "submitButton" ) . style . display = "none" ;
2019-07-13 01:04:24 +02:00
}
//adds or removes the player controls button to what it should be
function updateVisibilityOfPlayerControlsButton ( ) {
2019-08-02 18:18:56 +02:00
//not on a proper video yet
if ( ! getYouTubeVideoID ( document . URL ) ) return ;
2019-07-26 22:20:07 +02:00
addPlayerControlsButton ( ) ;
2019-07-29 21:42:14 +02:00
addInfoButton ( ) ;
2019-07-30 02:54:10 +02:00
addDeleteButton ( ) ;
2019-07-26 22:20:07 +02:00
addSubmitButton ( ) ;
2019-07-13 01:04:24 +02:00
if ( hideVideoPlayerControls ) {
removePlayerControlsButton ( ) ;
}
2019-07-30 03:40:13 +02:00
if ( hideInfoButtonPlayerControls ) {
document . getElementById ( "infoButton" ) . style . display = "none" ;
}
if ( hideDeleteButtonPlayerControls ) {
document . getElementById ( "deleteButton" ) . style . display = "none" ;
}
2019-07-13 01:04:24 +02:00
}
2019-07-12 16:42:39 +02:00
2019-07-12 22:44:50 +02:00
function startSponsorClicked ( ) {
2019-07-29 21:42:14 +02:00
//it can't update to this info yet
closeInfoMenu ( ) ;
2019-07-12 22:50:26 +02:00
toggleStartSponsorButton ( ) ;
//send back current time with message
chrome . runtime . sendMessage ( {
message : "addSponsorTime" ,
2019-07-26 22:20:07 +02:00
time : v . currentTime ,
videoID : getYouTubeVideoID ( document . URL )
2019-08-02 02:01:33 +02:00
} , function ( response ) {
//see if the sponsorTimesSubmitting needs to be updated
updateSponsorTimesSubmitting ( ) ;
} ) ;
}
function updateSponsorTimesSubmitting ( ) {
chrome . runtime . sendMessage ( {
message : "getSponsorTimes" ,
videoID : getYouTubeVideoID ( document . URL )
} , function ( response ) {
if ( response != undefined ) {
let sponsorTimes = response . sponsorTimes ;
//see if this data should be saved in the sponsorTimesSubmitting variable
if ( sponsorTimes != undefined ) {
sponsorTimesSubmitting = sponsorTimes ;
}
}
2019-07-12 22:50:26 +02:00
} ) ;
}
2019-07-22 22:46:50 +02:00
function changeStartSponsorButton ( showStartSponsor , uploadButtonVisible ) {
2019-07-30 02:54:10 +02:00
//if it isn't visible, there is no data
2019-07-30 03:40:13 +02:00
if ( uploadButtonVisible && ! hideDeleteButtonPlayerControls ) {
2019-07-30 02:54:10 +02:00
document . getElementById ( "deleteButton" ) . style . display = "unset" ;
} else {
document . getElementById ( "deleteButton" ) . style . display = "none" ;
}
2019-07-22 22:46:50 +02:00
if ( showStartSponsor ) {
2019-07-12 22:48:07 +02:00
showingStartSponsor = true ;
document . getElementById ( "startSponsorImage" ) . src = chrome . extension . getURL ( "icons/PlayerStartIconSponsorBlocker256px.png" ) ;
2019-07-29 00:51:46 +02:00
document . getElementById ( "startSponsorButton" ) . setAttribute ( "title" , "Sponsor Starts Now" ) ;
2019-07-22 00:19:56 +02:00
2019-07-30 03:40:13 +02:00
if ( document . getElementById ( "startSponsorImage" ) . style . display != "none" && uploadButtonVisible && ! hideInfoButtonPlayerControls ) {
2019-07-22 00:19:56 +02:00
document . getElementById ( "submitButton" ) . style . display = "unset" ;
} else if ( ! uploadButtonVisible ) {
//disable submit button
document . getElementById ( "submitButton" ) . style . display = "none" ;
}
} else {
showingStartSponsor = false ;
document . getElementById ( "startSponsorImage" ) . src = chrome . extension . getURL ( "icons/PlayerStopIconSponsorBlocker256px.png" ) ;
2019-07-29 00:51:46 +02:00
document . getElementById ( "startSponsorButton" ) . setAttribute ( "title" , "Sponsor Ends Now" ) ;
2019-07-22 00:19:56 +02:00
//disable submit button
document . getElementById ( "submitButton" ) . style . display = "none" ;
}
}
function toggleStartSponsorButton ( ) {
changeStartSponsorButton ( ! showingStartSponsor , true ) ;
}
2019-07-29 21:42:14 +02:00
//shows the info button on the video player
function addInfoButton ( ) {
if ( document . getElementById ( "infoButton" ) != null ) {
//it's already added
return ;
}
//make a submit button
let infoButton = document . createElement ( "button" ) ;
infoButton . id = "infoButton" ;
infoButton . className = "ytp-button playerButton" ;
infoButton . setAttribute ( "title" , "Open SponsorBlock Popup" ) ;
infoButton . addEventListener ( "click" , openInfoMenu ) ;
let infoImage = document . createElement ( "img" ) ;
infoImage . id = "infoButtonImage" ;
infoImage . className = "playerButtonImage" ;
infoImage . src = chrome . extension . getURL ( "icons/PlayerInfoIconSponsorBlocker256px.png" ) ;
//add the image to the button
infoButton . appendChild ( infoImage ) ;
2019-08-02 18:18:56 +02:00
let controls = document . getElementsByClassName ( "ytp-right-controls" ) ;
let referenceNode = controls [ controls . length - 1 ] ;
2019-08-03 20:11:47 +02:00
if ( referenceNode == undefined ) {
//page not loaded yet
setTimeout ( addInfoButton , 100 ) ;
return ;
}
2019-07-29 21:42:14 +02:00
referenceNode . prepend ( infoButton ) ;
}
2019-07-30 02:54:10 +02:00
//shows the delete button on the video player
function addDeleteButton ( ) {
if ( document . getElementById ( "deleteButton" ) != null ) {
//it's already added
return ;
}
//make a submit button
let deleteButton = document . createElement ( "button" ) ;
deleteButton . id = "deleteButton" ;
deleteButton . className = "ytp-button playerButton" ;
deleteButton . setAttribute ( "title" , "Clear Sponsor Times" ) ;
deleteButton . addEventListener ( "click" , clearSponsorTimes ) ;
//hide it at the start
deleteButton . style . display = "none" ;
let deleteImage = document . createElement ( "img" ) ;
deleteImage . id = "deleteButtonImage" ;
deleteImage . className = "playerButtonImage" ;
deleteImage . src = chrome . extension . getURL ( "icons/PlayerDeleteIconSponsorBlocker256px.png" ) ;
//add the image to the button
deleteButton . appendChild ( deleteImage ) ;
2019-08-02 18:18:56 +02:00
let controls = document . getElementsByClassName ( "ytp-right-controls" ) ;
let referenceNode = controls [ controls . length - 1 ] ;
2019-08-03 20:11:47 +02:00
if ( referenceNode == undefined ) {
//page not loaded yet
setTimeout ( addDeleteButton , 100 ) ;
return ;
}
2019-07-30 02:54:10 +02:00
referenceNode . prepend ( deleteButton ) ;
}
2019-07-22 00:19:56 +02:00
//shows the submit button on the video player
function addSubmitButton ( ) {
if ( document . getElementById ( "submitButton" ) != null ) {
//it's already added
return ;
2019-07-12 22:48:07 +02:00
}
2019-07-22 00:19:56 +02:00
//make a submit button
let submitButton = document . createElement ( "button" ) ;
submitButton . id = "submitButton" ;
2019-07-23 03:11:37 +02:00
submitButton . className = "ytp-button playerButton" ;
2019-07-22 00:19:56 +02:00
submitButton . setAttribute ( "title" , "Submit Sponsor Times" ) ;
submitButton . addEventListener ( "click" , submitSponsorTimes ) ;
//hide it at the start
submitButton . style . display = "none" ;
let submitImage = document . createElement ( "img" ) ;
submitImage . id = "submitButtonImage" ;
2019-07-23 03:11:37 +02:00
submitImage . className = "playerButtonImage" ;
2019-07-22 00:19:56 +02:00
submitImage . src = chrome . extension . getURL ( "icons/PlayerUploadIconSponsorBlocker256px.png" ) ;
//add the image to the button
submitButton . appendChild ( submitImage ) ;
2019-08-02 18:18:56 +02:00
let controls = document . getElementsByClassName ( "ytp-right-controls" ) ;
let referenceNode = controls [ controls . length - 1 ] ;
2019-08-03 20:11:47 +02:00
if ( referenceNode == undefined ) {
//page not loaded yet
setTimeout ( addSubmitButton , 100 ) ;
return ;
}
2019-08-02 18:18:56 +02:00
2019-07-22 00:19:56 +02:00
referenceNode . prepend ( submitButton ) ;
2019-07-12 22:44:50 +02:00
}
2019-07-29 21:42:14 +02:00
function openInfoMenu ( ) {
if ( document . getElementById ( "sponsorBlockPopupContainer" ) != null ) {
//it's already added
return ;
}
popupInitialised = false ;
//hide info button
document . getElementById ( "infoButton" ) . style . display = "none" ;
2019-07-30 19:27:20 +02:00
sendRequestToCustomServer ( 'GET' , chrome . extension . getURL ( "popup.html" ) , function ( xmlhttp ) {
if ( xmlhttp . readyState == 4 && xmlhttp . status == 200 ) {
var popup = document . createElement ( "div" ) ;
popup . id = "sponsorBlockPopupContainer" ;
popup . innerHTML = xmlhttp . responseText
//close button
let closeButton = document . createElement ( "div" ) ;
closeButton . innerText = "Close Popup" ;
closeButton . classList = "smallLink" ;
closeButton . setAttribute ( "align" , "center" ) ;
closeButton . addEventListener ( "click" , closeInfoMenu ) ;
//add the close button
popup . prepend ( closeButton ) ;
let parentNode = document . getElementById ( "secondary" ) ;
if ( parentNode == null ) {
//old youtube theme
parentNode = document . getElementById ( "watch7-sidebar-contents" ) ;
}
2019-07-29 21:42:14 +02:00
2019-07-30 19:27:20 +02:00
//make the logo source not 404
//query selector must be used since getElementByID doesn't work on a node and this isn't added to the document yet
let logo = popup . querySelector ( "#sponsorBlockPopupLogo" ) ;
logo . src = chrome . extension . getURL ( "icons/LogoSponsorBlocker256px.png" ) ;
2019-07-29 21:42:14 +02:00
2019-07-30 19:27:20 +02:00
//remove the style sheet and font that are not necessary
popup . querySelector ( "#sponorBlockPopupFont" ) . remove ( ) ;
popup . querySelector ( "#sponorBlockStyleSheet" ) . remove ( ) ;
2019-07-29 21:42:14 +02:00
2019-07-30 19:27:20 +02:00
parentNode . insertBefore ( popup , parentNode . firstChild ) ;
2019-07-29 21:42:14 +02:00
2019-07-30 19:27:20 +02:00
//run the popup init script
runThePopup ( ) ;
}
} ) ;
2019-07-29 21:42:14 +02:00
}
function closeInfoMenu ( ) {
let popup = document . getElementById ( "sponsorBlockPopupContainer" ) ;
if ( popup != null ) {
popup . remove ( ) ;
//show info button
document . getElementById ( "infoButton" ) . style . display = "unset" ;
}
}
2019-07-30 02:54:10 +02:00
function clearSponsorTimes ( ) {
//it can't update to this info yet
closeInfoMenu ( ) ;
let currentVideoID = getYouTubeVideoID ( document . URL ) ;
let sponsorTimeKey = 'sponsorTimes' + currentVideoID ;
chrome . storage . sync . get ( [ sponsorTimeKey ] , function ( result ) {
let sponsorTimes = result [ sponsorTimeKey ] ;
if ( sponsorTimes != undefined && sponsorTimes . length > 0 ) {
let confirmMessage = "Are you sure you want to clear this?\n\n" + getSponsorTimesMessage ( sponsorTimes ) ;
confirmMessage += "\n\nTo edit or delete individual values, click the info button or open the extension popup by clicking the extension icon in the top right corner."
if ( ! confirm ( confirmMessage ) ) return ;
//clear the sponsor times
let sponsorTimeKey = "sponsorTimes" + currentVideoID ;
chrome . storage . sync . set ( { [ sponsorTimeKey ] : [ ] } ) ;
2019-08-02 02:01:33 +02:00
//clear sponsor times submitting
sponsorTimesSubmitting = [ ] ;
2019-07-30 02:54:10 +02:00
//set buttons to be correct
changeStartSponsorButton ( true , false ) ;
}
} ) ;
}
2019-07-10 03:31:21 +02:00
//Opens the notice that tells the user that a sponsor was just skipped
2019-07-26 02:00:07 +02:00
function openSkipNotice ( UUID ) {
2019-07-10 04:10:25 +02:00
if ( dontShowNotice ) {
//don't show, return
return ;
}
2019-07-18 01:42:13 +02:00
let amountOfPreviousNotices = document . getElementsByClassName ( "sponsorSkipNotice" ) . length ;
if ( amountOfPreviousNotices > 0 ) {
//already exists
let previousNotice = document . getElementsByClassName ( "sponsorSkipNotice" ) [ 0 ] ;
previousNotice . classList . add ( "secondSkipNotice" )
}
2019-07-12 16:42:39 +02:00
let noticeElement = document . createElement ( "div" ) ;
2019-07-18 01:42:13 +02:00
//what sponsor time this is about
2019-07-26 02:00:07 +02:00
noticeElement . id = "sponsorSkipNotice" + UUID ;
2019-07-18 01:42:13 +02:00
noticeElement . classList . add ( "sponsorSkipObject" ) ;
noticeElement . classList . add ( "sponsorSkipNotice" ) ;
2019-08-02 02:32:04 +02:00
noticeElement . style . zIndex = 50 + amountOfPreviousNotices ;
2019-07-10 20:43:14 +02:00
2019-07-12 16:42:39 +02:00
let logoElement = document . createElement ( "img" ) ;
2019-07-26 02:00:07 +02:00
logoElement . id = "sponsorSkipLogo" + UUID ;
2019-07-18 01:42:13 +02:00
logoElement . className = "sponsorSkipLogo" ;
2019-07-11 22:15:47 +02:00
logoElement . src = chrome . extension . getURL ( "icons/LogoSponsorBlocker256px.png" ) ;
2019-07-12 16:42:39 +02:00
let noticeMessage = document . createElement ( "div" ) ;
2019-07-26 02:00:07 +02:00
noticeMessage . id = "sponsorSkipMessage" + UUID ;
2019-07-18 01:42:13 +02:00
noticeMessage . classList . add ( "sponsorSkipMessage" ) ;
noticeMessage . classList . add ( "sponsorSkipObject" ) ;
2019-07-12 00:47:03 +02:00
noticeMessage . innerText = "Hey, you just skipped a sponsor!" ;
2019-07-12 16:42:39 +02:00
let noticeInfo = document . createElement ( "p" ) ;
2019-07-26 02:00:07 +02:00
noticeInfo . id = "sponsorSkipInfo" + UUID ;
2019-07-18 01:42:13 +02:00
noticeInfo . classList . add ( "sponsorSkipInfo" ) ;
noticeInfo . classList . add ( "sponsorSkipObject" ) ;
2019-07-15 22:28:41 +02:00
noticeInfo . innerText = "This message will disapear in 7 seconds" ;
//thumbs up and down buttons
let voteButtonsContainer = document . createElement ( "div" ) ;
2019-07-26 02:00:07 +02:00
voteButtonsContainer . id = "sponsorTimesVoteButtonsContainer" + UUID ;
2019-07-15 22:28:41 +02:00
voteButtonsContainer . setAttribute ( "align" , "center" ) ;
let upvoteButton = document . createElement ( "img" ) ;
2019-07-26 02:00:07 +02:00
upvoteButton . id = "sponsorTimesUpvoteButtonsContainer" + UUID ;
2019-07-15 22:28:41 +02:00
upvoteButton . className = "sponsorSkipObject voteButton" ;
upvoteButton . src = chrome . extension . getURL ( "icons/upvote.png" ) ;
2019-07-20 04:24:59 +02:00
upvoteButton . addEventListener ( "click" , ( ) => vote ( 1 , UUID ) ) ;
2019-07-15 22:28:41 +02:00
let downvoteButton = document . createElement ( "img" ) ;
2019-07-26 02:00:07 +02:00
downvoteButton . id = "sponsorTimesDownvoteButtonsContainer" + UUID ;
2019-07-15 22:28:41 +02:00
downvoteButton . className = "sponsorSkipObject voteButton" ;
downvoteButton . src = chrome . extension . getURL ( "icons/downvote.png" ) ;
2019-07-20 04:24:59 +02:00
downvoteButton . addEventListener ( "click" , ( ) => vote ( 0 , UUID ) ) ;
2019-07-15 22:28:41 +02:00
//add thumbs up and down buttons to the container
voteButtonsContainer . appendChild ( upvoteButton ) ;
voteButtonsContainer . appendChild ( downvoteButton ) ;
2019-07-10 03:15:43 +02:00
2019-07-12 16:42:39 +02:00
let buttonContainer = document . createElement ( "div" ) ;
2019-07-10 03:44:41 +02:00
buttonContainer . setAttribute ( "align" , "center" ) ;
2019-07-12 16:42:39 +02:00
let goBackButton = document . createElement ( "button" ) ;
2019-07-10 20:43:14 +02:00
goBackButton . innerText = "Go back" ;
goBackButton . className = "sponsorSkipButton" ;
2019-07-18 01:42:13 +02:00
goBackButton . addEventListener ( "click" , ( ) => goBackToPreviousTime ( UUID ) ) ;
2019-07-10 03:44:41 +02:00
2019-07-12 16:42:39 +02:00
let hideButton = document . createElement ( "button" ) ;
2019-07-12 00:47:03 +02:00
hideButton . innerText = "Dismiss" ;
2019-07-10 20:43:14 +02:00
hideButton . className = "sponsorSkipButton" ;
2019-07-18 01:42:13 +02:00
hideButton . addEventListener ( "click" , ( ) => closeSkipNotice ( UUID ) ) ;
2019-07-10 03:46:11 +02:00
2019-07-12 16:42:39 +02:00
let dontShowAgainButton = document . createElement ( "button" ) ;
2019-07-10 21:11:55 +02:00
dontShowAgainButton . innerText = "Don't Show This Again" ;
2019-07-10 20:43:14 +02:00
dontShowAgainButton . className = "sponsorSkipDontShowButton" ;
2019-07-10 04:10:25 +02:00
dontShowAgainButton . addEventListener ( "click" , dontShowNoticeAgain ) ;
2019-07-10 03:44:41 +02:00
buttonContainer . appendChild ( goBackButton ) ;
2019-07-10 03:46:11 +02:00
buttonContainer . appendChild ( hideButton ) ;
2019-07-10 04:12:47 +02:00
buttonContainer . appendChild ( document . createElement ( "br" ) ) ;
2019-07-10 20:43:14 +02:00
buttonContainer . appendChild ( document . createElement ( "br" ) ) ;
2019-07-10 04:10:25 +02:00
buttonContainer . appendChild ( dontShowAgainButton ) ;
2019-07-10 03:44:41 +02:00
2019-07-11 22:15:47 +02:00
noticeElement . appendChild ( logoElement ) ;
2019-07-10 03:44:41 +02:00
noticeElement . appendChild ( noticeMessage ) ;
2019-07-12 00:47:03 +02:00
noticeElement . appendChild ( noticeInfo ) ;
2019-07-15 22:28:41 +02:00
noticeElement . appendChild ( voteButtonsContainer ) ;
2019-07-10 03:44:41 +02:00
noticeElement . appendChild ( buttonContainer ) ;
2019-07-10 03:15:43 +02:00
2019-08-02 02:32:04 +02:00
let referenceNode = document . getElementById ( "movie_player" ) ;
2019-08-02 02:55:47 +02:00
if ( referenceNode == null ) {
//for embeds
let player = document . getElementById ( "player" ) ;
referenceNode = player . firstChild ;
let index = 1 ;
//find the child that is the video player (sometimes it is not the first)
while ( ! referenceNode . classList . contains ( "html5-video-player" ) || ! referenceNode . classList . contains ( "ytp-embed" ) ) {
referenceNode = player . children [ index ] ;
index ++ ;
}
}
2019-07-10 03:15:43 +02:00
referenceNode . prepend ( noticeElement ) ;
2019-01-18 20:49:43 +01:00
}
2019-07-09 05:43:06 +02:00
2019-07-20 04:24:59 +02:00
function afterDownvote ( UUID ) {
2019-07-16 02:38:26 +02:00
//change text to say thanks for voting
//remove buttons
2019-07-20 19:41:13 +02:00
let upvoteButton = document . getElementById ( "sponsorTimesUpvoteButtonsContainer" + UUID ) ;
let downvoteButton = document . getElementById ( "sponsorTimesDownvoteButtonsContainer" + UUID ) ;
if ( upvoteButton != null ) {
document . getElementById ( "sponsorTimesVoteButtonsContainer" + UUID ) . removeChild ( upvoteButton ) ;
}
if ( downvoteButton != null ) {
document . getElementById ( "sponsorTimesVoteButtonsContainer" + UUID ) . removeChild ( downvoteButton ) ;
}
let previousInfoMessage = document . getElementById ( "sponsorTimesInfoMessage" + UUID ) ;
if ( previousInfoMessage != null ) {
//remove it
document . getElementById ( "sponsorTimesVoteButtonsContainer" + UUID ) . removeChild ( previousInfoMessage ) ;
}
2019-07-16 02:38:26 +02:00
//add thanks for voting text
let thanksForVotingText = document . createElement ( "p" ) ;
thanksForVotingText . id = "sponsorTimesThanksForVotingText" ;
thanksForVotingText . innerText = "Thanks for voting!"
//add extra info for voting
let thanksForVotingInfoText = document . createElement ( "p" ) ;
thanksForVotingInfoText . id = "sponsorTimesThanksForVotingInfoText" ;
thanksForVotingInfoText . innerText = "Hit go back to get to where you came from."
//add element to div
2019-07-18 01:42:13 +02:00
document . getElementById ( "sponsorTimesVoteButtonsContainer" + UUID ) . appendChild ( thanksForVotingText ) ;
document . getElementById ( "sponsorTimesVoteButtonsContainer" + UUID ) . appendChild ( thanksForVotingInfoText ) ;
2019-07-16 01:13:09 +02:00
}
2019-07-20 19:41:13 +02:00
function addLoadingInfo ( message , UUID ) {
//change text to say thanks for message
2019-07-20 04:24:59 +02:00
//remove buttons
2019-07-20 19:41:13 +02:00
let upvoteButton = document . getElementById ( "sponsorTimesUpvoteButtonsContainer" + UUID ) ;
let downvoteButton = document . getElementById ( "sponsorTimesDownvoteButtonsContainer" + UUID ) ;
if ( upvoteButton != null ) {
document . getElementById ( "sponsorTimesVoteButtonsContainer" + UUID ) . removeChild ( upvoteButton ) ;
}
if ( downvoteButton != null ) {
document . getElementById ( "sponsorTimesVoteButtonsContainer" + UUID ) . removeChild ( downvoteButton ) ;
}
let previousInfoMessage = document . getElementById ( "sponsorTimesInfoMessage" + UUID ) ;
if ( previousInfoMessage != null ) {
//remove it
document . getElementById ( "sponsorTimesVoteButtonsContainer" + UUID ) . removeChild ( previousInfoMessage ) ;
}
2019-07-20 04:24:59 +02:00
//add thanks for voting text
let thanksForVotingText = document . createElement ( "p" ) ;
2019-07-20 19:41:13 +02:00
thanksForVotingText . id = "sponsorTimesInfoMessage" + UUID ;
thanksForVotingText . className = "sponsorTimesInfoMessage" ;
2019-07-20 04:24:59 +02:00
thanksForVotingText . innerText = message ;
//add element to div
document . getElementById ( "sponsorTimesVoteButtonsContainer" + UUID ) . appendChild ( thanksForVotingText ) ;
}
2019-07-18 01:42:13 +02:00
function vote ( type , UUID ) {
2019-07-20 19:41:13 +02:00
//add loading info
addLoadingInfo ( "Loading..." , UUID )
2019-07-16 01:13:09 +02:00
chrome . runtime . sendMessage ( {
message : "submitVote" ,
type : type ,
2019-07-18 01:42:13 +02:00
UUID : UUID
2019-07-20 04:24:59 +02:00
} , function ( response ) {
if ( response != undefined ) {
//see if it was a success or failure
if ( response . successType == 1 ) {
//success
if ( type == 0 ) {
afterDownvote ( UUID ) ;
} else if ( type == 1 ) {
closeSkipNotice ( UUID ) ;
}
} else if ( response . successType == 0 ) {
//failure: duplicate vote
2019-07-20 19:41:13 +02:00
addLoadingInfo ( "It seems you've already voted before" , UUID )
2019-07-20 19:26:55 +02:00
} else if ( response . successType == - 1 ) {
2019-07-30 03:06:15 +02:00
if ( response . statusCode == 502 ) {
addLoadingInfo ( "It seems the sever is down. Contact the dev immediately." , UUID )
} else {
//failure: unknown error
addLoadingInfo ( "A connection error has occured. Error code: " + response . statusCode , UUID )
}
2019-07-20 04:24:59 +02:00
}
}
2019-07-16 01:13:09 +02:00
} ) ;
}
2019-07-18 01:42:13 +02:00
//Closes the notice that tells the user that a sponsor was just skipped for this UUID
function closeSkipNotice ( UUID ) {
let notice = document . getElementById ( "sponsorSkipNotice" + UUID ) ;
2019-07-10 03:44:41 +02:00
if ( notice != null ) {
notice . remove ( ) ;
}
2019-07-10 03:31:21 +02:00
}
2019-07-18 01:42:13 +02:00
//Closes all notices that tell the user that a sponsor was just skipped
function closeAllSkipNotices ( ) {
let notices = document . getElementsByClassName ( "sponsorSkipNotice" ) ;
for ( let i = 0 ; i < notices . length ; i ++ ) {
notices [ i ] . remove ( ) ;
}
}
2019-07-10 04:10:25 +02:00
function dontShowNoticeAgain ( ) {
2019-07-22 22:42:57 +02:00
chrome . storage . sync . set ( { "dontShowNoticeAgain" : true } ) ;
2019-07-10 04:10:25 +02:00
dontShowNotice = true ;
2019-07-18 01:42:13 +02:00
closeAllSkipNotices ( ) ;
2019-07-10 04:10:25 +02:00
}
2019-07-29 21:42:14 +02:00
function sponsorMessageStarted ( callback ) {
2019-08-03 20:11:47 +02:00
v = document . querySelector ( 'video' ) ;
2019-07-09 05:43:06 +02:00
//send back current time
2019-07-29 21:42:14 +02:00
callback ( {
2019-07-09 05:43:06 +02:00
time : v . currentTime
2019-07-29 21:42:14 +02:00
} )
2019-07-12 22:50:26 +02:00
//update button
toggleStartSponsorButton ( ) ;
2019-07-10 03:15:43 +02:00
}
2019-07-22 00:19:56 +02:00
function submitSponsorTimes ( ) {
2019-07-27 17:08:29 +02:00
if ( document . getElementById ( "submitButton" ) . style . display == "none" ) {
//don't submit, not ready
return ;
}
2019-07-29 21:42:14 +02:00
//it can't update to this info yet
closeInfoMenu ( ) ;
2019-07-29 17:36:57 +02:00
let currentVideoID = getYouTubeVideoID ( document . URL ) ;
let sponsorTimeKey = 'sponsorTimes' + currentVideoID ;
chrome . storage . sync . get ( [ sponsorTimeKey ] , function ( result ) {
let sponsorTimes = result [ sponsorTimeKey ] ;
if ( sponsorTimes != undefined && sponsorTimes . length > 0 ) {
let confirmMessage = "Are you sure you want to submit this?\n\n" + getSponsorTimesMessage ( sponsorTimes ) ;
2019-07-29 22:29:20 +02:00
confirmMessage += "\n\nTo edit or delete values, click the info button or open the extension popup by clicking the extension icon in the top right corner."
2019-07-29 17:36:57 +02:00
if ( ! confirm ( confirmMessage ) ) return ;
sendSubmitMessage ( ) ;
}
} ) ;
}
//send the message to the background js
//called after all the checks have been made that it's okay to do so
function sendSubmitMessage ( ) {
2019-07-22 00:19:56 +02:00
//add loading animation
document . getElementById ( "submitButtonImage" ) . src = chrome . extension . getURL ( "icons/PlayerUploadIconSponsorBlocker256px.png" ) ;
document . getElementById ( "submitButton" ) . style . animation = "rotate 1s 0s infinite" ;
let currentVideoID = getYouTubeVideoID ( document . URL ) ;
chrome . runtime . sendMessage ( {
message : "submitTimes" ,
videoID : currentVideoID
} , function ( response ) {
if ( response != undefined ) {
if ( response . statusCode == 200 ) {
//hide loading message
2019-07-22 21:59:21 +02:00
let submitButton = document . getElementById ( "submitButton" ) ;
//finish this animation
submitButton . style . animation = "rotate 1s" ;
//when the animation is over, hide the button
2019-08-04 02:52:09 +02:00
let animationEndListener = function ( ) {
2019-08-01 21:19:58 +02:00
changeStartSponsorButton ( true , false ) ;
2019-08-04 02:52:09 +02:00
submitButton . style . animation = "none" ;
submitButton . removeEventListener ( "animationend" , animationEndListener ) ;
} ;
submitButton . addEventListener ( "animationend" , animationEndListener ) ;
2019-07-22 00:19:56 +02:00
//clear the sponsor times
let sponsorTimeKey = "sponsorTimes" + currentVideoID ;
2019-07-22 22:42:57 +02:00
chrome . storage . sync . set ( { [ sponsorTimeKey ] : [ ] } ) ;
2019-07-22 00:19:56 +02:00
} else {
//for a more detailed error message, they should check the popup
//show that the upload failed
document . getElementById ( "submitButton" ) . style . animation = "unset" ;
document . getElementById ( "submitButtonImage" ) . src = chrome . extension . getURL ( "icons/PlayerUploadFailedIconSponsorBlocker256px.png" ) ;
2019-07-30 03:09:10 +02:00
if ( response . statusCode == 400 ) {
alert ( "Server said this request was invalid" ) ;
} else if ( response . statusCode == 429 ) {
alert ( "You have submitted too many sponsor times for this one video, are you sure there are this many?" ) ;
} else if ( response . statusCode == 409 ) {
alert ( "This has already been submitted before" ) ;
} else if ( response . statusCode == 502 ) {
alert ( "It seems the server is down. Contact the dev to inform them. Error code " + response . statusCode ) ;
} else {
alert ( "There was an error submitting your sponsor times, please try again later. Error code " + response . statusCode ) ;
}
2019-07-22 00:19:56 +02:00
}
}
} ) ;
}
2019-07-29 17:36:57 +02:00
//get the message that visually displays the video times
function getSponsorTimesMessage ( sponsorTimes ) {
let sponsorTimesMessage = "" ;
for ( let i = 0 ; i < sponsorTimes . length ; i ++ ) {
for ( let s = 0 ; s < sponsorTimes [ i ] . length ; s ++ ) {
let timeMessage = getFormattedTime ( sponsorTimes [ i ] [ s ] ) ;
//if this is an end time
if ( s == 1 ) {
timeMessage = " to " + timeMessage ;
} else if ( i > 0 ) {
//add commas if necessary
timeMessage = ", " + timeMessage ;
}
sponsorTimesMessage += timeMessage ;
}
}
return sponsorTimesMessage ;
}
//converts time in seconds to minutes:seconds
function getFormattedTime ( seconds ) {
let minutes = Math . floor ( seconds / 60 ) ;
let secondsDisplay = Math . round ( seconds - minutes * 60 ) ;
if ( secondsDisplay < 10 ) {
//add a zero
secondsDisplay = "0" + secondsDisplay ;
}
let formatted = minutes + ":" + secondsDisplay ;
return formatted ;
}
2019-07-20 21:33:52 +02:00
function sendRequestToServer ( type , address , callback ) {
let xmlhttp = new XMLHttpRequest ( ) ;
xmlhttp . open ( type , serverAddress + address , true ) ;
if ( callback != undefined ) {
xmlhttp . onreadystatechange = function ( ) {
callback ( xmlhttp , false ) ;
} ;
xmlhttp . onerror = function ( ev ) {
callback ( xmlhttp , true ) ;
} ;
}
//submit this request
xmlhttp . send ( ) ;
}
2019-07-22 03:08:23 +02:00
function sendRequestToCustomServer ( type , fullAddress , callback ) {
let xmlhttp = new XMLHttpRequest ( ) ;
xmlhttp . open ( type , fullAddress , true ) ;
if ( callback != undefined ) {
xmlhttp . onreadystatechange = function ( ) {
callback ( xmlhttp , false ) ;
} ;
xmlhttp . onerror = function ( ev ) {
callback ( xmlhttp , true ) ;
} ;
}
//submit this request
xmlhttp . send ( ) ;
}
2019-07-10 03:15:43 +02:00
function getYouTubeVideoID ( url ) { // Returns with video id else returns false
var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/ ;
var match = url . match ( regExp ) ;
2019-07-27 23:18:43 +02:00
var id = new URL ( url ) . searchParams . get ( "v" ) ;
2019-08-02 02:55:47 +02:00
if ( url . includes ( "/embed/" ) ) {
//it is an embed, don't search for v
id = match [ 7 ] ;
}
2019-07-27 23:18:43 +02:00
return ( match && match [ 7 ] . length == 11 ) ? id : false ;
2019-07-27 13:57:44 +02:00
}
2019-08-03 04:37:12 +02:00
//returns the start time of the video if there was one specified (ex. ?t=5s)
function getYouTubeVideoStartTime ( url ) {
let searchParams = new URL ( url ) . searchParams ;
var startTime = searchParams . get ( "t" ) ;
if ( startTime == null ) {
startTime = searchParams . get ( "time_continue" ) ;
}
return startTime ;
2019-08-08 15:02:06 +02:00
}