1
0
mirror of https://github.com/krateng/maloja.git synced 2023-08-10 21:12:55 +03:00

Fixed week pulse view on start page

This commit is contained in:
Krateng
2019-03-18 20:30:28 +01:00
parent d07ca92252
commit 88cbce33e2
2 changed files with 52 additions and 52 deletions

View File

@ -2,10 +2,18 @@
chrome.tabs.onUpdated.addListener(onTabUpdated);
chrome.tabs.onRemoved.addListener(onTabRemoved);
chrome.tabs.onActivated.addListener(onTabChanged);
chrome.runtime.onMessage.addListener(onPlaybackUpdate);
var patterns = [
"https://music.youtube.com",
"http://music.youtube.com"
];
function onTabUpdated(tabId, changeInfo, tab) {
if (changeInfo.status !== "complete") {
return;
}
console.log("Update")
chrome.tabs.get(tabId,party)
}
@ -14,26 +22,19 @@ function onTabRemoved() {
}
function onTabChanged(activeInfo) {
}
function party(tab) {
var patterns = [
"https://music.youtube.com",
"http://music.youtube.com"
];
importantPage = false
for (var i=0;i<patterns.length;i++) {
if (tab.url.startsWith(patterns[i])) {
importantPage = true
}
}
if (importantPage) {
window.setTimeout(function(){chrome.tabs.executeScript(tab.id,{"file":"contentScript.js"})},1000); // youtube for some reason decides to not update the artist immediately
}
@ -65,21 +66,21 @@ function startPlayback(artist,title,seconds) {
// CASE 1: Resuming playback of previously played title
if (artist == currentArtist && title == currentTitle && !currentlyPlaying) {
console.log("Resuming playback")
// Already played full song
while (alreadyPlayed > currentLength) {
alreadyPlayed = alreadyPlayed - currentLength
scrobble(currentArtist,currentTitle,currentLength)
}
setUpdate()
currentlyPlaying = true
}
// CASE 2: New track is being played
else if (artist != currentArtist || title != currentTitle) {
//first inform ourselves that the previous track has now been stopped for good
stopPlayback(artist,title)
//then initialize new playback
@ -97,25 +98,25 @@ function startPlayback(artist,title,seconds) {
// 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) {
//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")
}
// Already played full song
while (alreadyPlayed > currentLength) {
alreadyPlayed = alreadyPlayed - currentLength
scrobble(currentArtist,currentTitle,currentLength)
}
currentlyPlaying = false
currentlyPlaying = false
//ONLY CASE 2: Playback ended
if (artist != currentArtist || title != currentTitle) {
if (alreadyPlayed > currentLength / 2) {
@ -148,7 +149,7 @@ function ostopPlayback(artist,title) {
}
function ostartPlayback(artist,title,seconds) {
console.log("Playback started!")
if (artist == currentArtist && title == currentTitle && !currentlyPlaying) {
console.log("Still previous track!")
@ -160,7 +161,7 @@ function ostartPlayback(artist,title,seconds) {
}
alreadyPlayed = alreadyPlayed - currentLength
alreadyScrobbled = false
}
d = new Date()
t = Math.floor(d.getTime()/1000)
@ -176,21 +177,21 @@ function ostartPlayback(artist,title,seconds) {
delta = t - lastUpdate
console.log("Since the last update, " + delta + " seconds of music have been played")
alreadyPlayed = alreadyPlayed + delta
}
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)
}
else if (alreadyScrobbled) {
console.log("We already scrobbled this track tho.")
alreadyScrobbled = false
}
console.log("But now, new track!")
d = new Date()
t = Math.floor(d.getTime()/1000)
@ -219,8 +220,8 @@ function scrobble(artist,title,seconds) {
xhttp.send("artist=" + artiststring + "&title=" + titlestring + "&duration=" + seconds + "&key=" + APIKEY)
});
});
}
function setUpdate() {