2018-11-27 18:08:14 +03:00
chrome . tabs . onUpdated . addListener ( onTabUpdated ) ;
chrome . tabs . onRemoved . addListener ( onTabRemoved ) ;
chrome . runtime . onMessage . addListener ( onPlaybackUpdate ) ;
2019-03-18 22:30:28 +03:00
var patterns = [
"https://music.youtube.com" ,
"http://music.youtube.com"
] ;
2018-11-27 18:08:14 +03:00
function onTabUpdated ( tabId , changeInfo , tab ) {
2019-03-18 22:30:28 +03:00
if ( changeInfo . status !== "complete" ) {
return ;
}
2019-03-14 21:05:47 +03:00
console . log ( "Update" )
2018-11-27 18:08:14 +03:00
chrome . tabs . get ( tabId , party )
}
function onTabRemoved ( ) {
}
function party ( tab ) {
importantPage = false
2019-03-18 22:30:28 +03:00
2018-11-27 18:08:14 +03:00
for ( var i = 0 ; i < patterns . length ; i ++ ) {
if ( tab . url . startsWith ( patterns [ i ] ) ) {
importantPage = true
2019-03-18 22:30:28 +03:00
2018-11-27 18:08:14 +03:00
}
}
2019-03-18 22:30:28 +03:00
2018-11-27 18:08:14 +03:00
if ( importantPage ) {
2019-03-14 22:08:33 +03:00
window . setTimeout ( function ( ) { chrome . tabs . executeScript ( tab . id , { "file" : "contentScript.js" } ) } , 1000 ) ; // youtube for some reason decides to not update the artist immediately
2018-11-27 18:08:14 +03:00
}
}
2019-03-14 22:08:33 +03:00
2018-11-27 18:08:14 +03:00
function onPlaybackUpdate ( request , sender ) {
//console.log("Got update from Plex Web!")
if ( request . type == "stopPlayback" && currentlyPlaying ) {
2018-11-29 15:43:45 +03:00
stopPlayback ( request . artist , request . title ) ;
2018-11-27 18:08:14 +03:00
}
else if ( request . type == "startPlayback" ) {
startPlayback ( request . artist , request . title , request . duration ) ;
}
}
var currentTitle ;
var currentArtist ;
var currentLength ;
var alreadyPlayed ;
var currentlyPlaying = false ;
var lastUpdate = 0 ;
var alreadyScrobbled = false ;
function startPlayback ( artist , title , seconds ) {
2018-11-29 15:43:45 +03:00
// CASE 1: Resuming playback of previously played title
if ( artist == currentArtist && title == currentTitle && ! currentlyPlaying ) {
console . log ( "Resuming playback" )
2019-03-18 22:30:28 +03:00
2018-11-29 15:43:45 +03:00
// Already played full song
while ( alreadyPlayed > currentLength ) {
alreadyPlayed = alreadyPlayed - currentLength
scrobble ( currentArtist , currentTitle , currentLength )
}
2019-03-18 22:30:28 +03:00
2018-11-29 15:43:45 +03:00
setUpdate ( )
currentlyPlaying = true
2019-03-18 22:30:28 +03:00
2018-11-29 15:43:45 +03:00
}
2019-03-18 22:30:28 +03:00
2018-11-29 15:43:45 +03:00
// CASE 2: New track is being played
else if ( artist != currentArtist || title != currentTitle ) {
2019-03-18 22:30:28 +03:00
2018-11-29 15:43:45 +03:00
//first inform ourselves that the previous track has now been stopped for good
stopPlayback ( artist , title )
//then initialize new playback
console . log ( "New track" )
setUpdate ( )
alreadyPlayed = 0
currentTitle = title
currentArtist = artist
currentLength = seconds
console . log ( artist + " - " + title + " is playing!" )
currentlyPlaying = true
}
}
// the artist and title arguments are not attributes of the track being stopped, but of the track active now
// they are here to recognize whether the playback has been paused or completely ended / replaced
function stopPlayback ( artist , title ) {
2019-03-18 22:30:28 +03:00
2018-11-29 15:43:45 +03:00
//CASE 1: Playback just paused OR CASE 2: Playback ended
if ( currentlyPlaying ) {
d = setUpdate ( )
alreadyPlayed = alreadyPlayed + d
console . log ( d + " seconds played since last update, " + alreadyPlayed + " seconds played overall" )
}
2019-03-18 22:30:28 +03:00
2018-11-29 15:43:45 +03:00
// Already played full song
while ( alreadyPlayed > currentLength ) {
alreadyPlayed = alreadyPlayed - currentLength
scrobble ( currentArtist , currentTitle , currentLength )
}
2019-03-18 22:30:28 +03:00
2018-11-29 15:43:45 +03:00
currentlyPlaying = false
2019-03-18 22:30:28 +03:00
2018-11-29 15:43:45 +03:00
//ONLY CASE 2: Playback ended
if ( artist != currentArtist || title != currentTitle ) {
if ( alreadyPlayed > currentLength / 2 ) {
scrobble ( currentArtist , currentTitle , alreadyPlayed )
alreadyPlayed = 0
}
}
}
2018-11-29 16:45:31 +03:00
// One problem here: Closing the player while it's paused does not cause an event, so the track will only be scrobbled the next time we play something.
// Also potentially problematic: Pausing a track and just leaving it should probably trigger a scrobble after some time because we can assume the user just stopped listening but didn't bother to press the X
// We could simply check for scrobblability when the track is paused, but this would remove the ability to send listening time with the scrobble
2018-11-29 15:43:45 +03:00
function ostopPlayback ( artist , title ) {
currentlyPlaying = false
console . log ( "Playback stopped!" )
d = new Date ( )
t = Math . floor ( d . getTime ( ) / 1000 )
delta = t - lastUpdate
console . log ( "Since the last update, " + delta + " seconds of music have been played" )
alreadyPlayed = alreadyPlayed + delta
console . log ( alreadyPlayed + " seconds of this track have been played overall" )
if ( ( alreadyPlayed > currentLength / 2 ) && ! alreadyScrobbled ) {
console . log ( "Enough to scrobble: " + currentArtist + " - " + currentTitle )
scrobble ( currentArtist , currentTitle )
alreadyScrobbled = true
}
}
function ostartPlayback ( artist , title , seconds ) {
2019-03-18 22:30:28 +03:00
2018-11-27 18:08:14 +03:00
console . log ( "Playback started!" )
if ( artist == currentArtist && title == currentTitle && ! currentlyPlaying ) {
console . log ( "Still previous track!" )
2018-11-28 14:29:02 +03:00
while ( alreadyPlayed > currentLength ) {
console . log ( "This song is being played several times in a row!" )
if ( ! alreadyScrobbled ) {
scrobble ( currentArtist , currentTitle )
//alreadyScrobbled = true
}
alreadyPlayed = alreadyPlayed - currentLength
alreadyScrobbled = false
2019-03-18 22:30:28 +03:00
2018-11-28 14:29:02 +03:00
}
2018-11-27 18:08:14 +03:00
d = new Date ( )
t = Math . floor ( d . getTime ( ) / 1000 )
lastUpdate = t
currentlyPlaying = true
}
else if ( artist != currentArtist || title != currentTitle ) {
console . log ( "New track!" )
if ( currentlyPlaying ) {
console . log ( "We were playing another track before, so let's check if we should scrobble that." )
d = new Date ( )
t = Math . floor ( d . getTime ( ) / 1000 )
delta = t - lastUpdate
console . log ( "Since the last update, " + delta + " seconds of music have been played" )
alreadyPlayed = alreadyPlayed + delta
2019-03-18 22:30:28 +03:00
2018-11-27 18:08:14 +03:00
}
2019-03-18 22:30:28 +03:00
2018-11-27 18:08:14 +03:00
console . log ( "The previous track was played for " + alreadyPlayed + " seconds, that's " + Math . floor ( alreadyPlayed / currentLength * 100 ) + "% of its length." )
if ( alreadyPlayed > currentLength / 2 && ! alreadyScrobbled ) {
console . log ( "Enough to scrobble: " + currentArtist + " - " + currentTitle )
scrobble ( currentArtist , currentTitle )
2019-03-18 22:30:28 +03:00
2018-11-27 18:08:14 +03:00
}
else if ( alreadyScrobbled ) {
console . log ( "We already scrobbled this track tho." )
alreadyScrobbled = false
}
2019-03-18 22:30:28 +03:00
2018-11-27 18:08:14 +03:00
console . log ( "But now, new track!" )
d = new Date ( )
t = Math . floor ( d . getTime ( ) / 1000 )
lastUpdate = t
alreadyPlayed = 0
currentTitle = title
currentArtist = artist
currentLength = seconds
console . log ( artist + " - " + title + " is playing!" )
currentlyPlaying = true
}
}
2018-11-29 15:43:45 +03:00
function scrobble ( artist , title , seconds ) {
console . log ( "Scrobbling " + artist + " - " + title + "; " + seconds + " seconds playtime" )
2018-11-27 18:08:14 +03:00
artiststring = encodeURIComponent ( artist )
titlestring = encodeURIComponent ( title )
2018-12-14 18:38:08 +03:00
chrome . storage . local . get ( "apikey" , function ( result ) {
APIKEY = result [ "apikey" ]
chrome . storage . local . get ( "serverurl" , function ( result ) {
URL = result [ "serverurl" ]
var xhttp = new XMLHttpRequest ( ) ;
xhttp . open ( "POST" , URL + "/db/newscrobble" , true ) ;
xhttp . send ( "artist=" + artiststring + "&title=" + titlestring + "&duration=" + seconds + "&key=" + APIKEY )
} ) ;
} ) ;
2019-03-18 22:30:28 +03:00
2018-11-27 18:08:14 +03:00
}
2018-11-29 15:43:45 +03:00
function setUpdate ( ) {
d = new Date ( )
t = Math . floor ( d . getTime ( ) / 1000 )
delta = t - lastUpdate
lastUpdate = t
return delta
}