Skip to content

Commit

Permalink
Save track name when loading information. There will be a race condit…
Browse files Browse the repository at this point in the history
…ion during check play position, whereby the embedded web player has already loaded the new track but we have not updated our track information yet, so we compare the ui track name with our saved track name, if theyre different, dont do any seeks or position comparison (#24)
  • Loading branch information
abdulazizali77 committed Oct 15, 2020
1 parent 7ab2316 commit 030a1a5
Showing 1 changed file with 30 additions and 16 deletions.
46 changes: 30 additions & 16 deletions src/contentscript/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var containerDiv;
var dialogDiv;
var bearertoken;
var currentTrackId;
var currentTrackName;
var rangeMin = 0;
var rangeMax = 1;
var currentPlayingPosition;
Expand Down Expand Up @@ -322,26 +323,38 @@ function handleRangeChange(event) {
function checkPlayingPosition() {


//if rangeMax is default or unset then dont care
if (!(rangeMin == 0 && (rangeMax == currentTrackIdDuration || rangeMax == 1))) {
//NB: if the max is not set but the min is, the playback cant seek back if it reaches the end
//so we have to offset against 1second, however this is a bit iffy and buggy
let offset = 0;
if (rangeMax == currentTrackIdDuration) {
offset = 1;
}
if (currentPlayingPosition < rangeMin || currentPlayingPosition >= (rangeMax - offset)) {
console.log("currentPlayingPosition is outside!" + currentPlayingPosition + " " + rangeMin + " " + rangeMax + " " + (rangeMax - offset));
//TODO: a flag to disable jumping to min if less than minimum
seek(Number.parseInt(rangeMin) * 1000);
let nowt1 = document.querySelectorAll('a[data-testid="nowplaying-track-link"]')[0];
let tempTrackName;
if (nowt1 != undefined) {
tempTrackName = nowt1.innerText;
}
//FIXME: this might call while
if (tempTrackName == currentTrackName) {
//if rangeMax is default or unset then dont care
if (!(rangeMin == 0 && (rangeMax == currentTrackIdDuration || rangeMax == 1))) {
//NB: if the max is not set but the min is, the playback cant seek back if it reaches the end
//so we have to offset against 1second, however this is a bit iffy and buggy
let offset = 0;
if (rangeMax == currentTrackIdDuration) {
offset = 1;
}
if (currentPlayingPosition < rangeMin || currentPlayingPosition >= (rangeMax - offset)) {
console.log("DEBUG currentPlayingPosition is outside! tempTrackName=" + tempTrackName + " currentTrackName=" + currentTrackName + " currentTrackId=" + currentTrackId + " currentTrackIdDuration=" + currentTrackIdDuration + " pos=" + currentPlayingPosition + " " + rangeMin + " " + rangeMax + " " + (rangeMax - offset));
//TODO: a flag to disable jumping to min if less than minimum
seek(Number.parseInt(rangeMin) * 1000);
} else {
//console.log("currentPlayingPosition within range!" + currentPlayingPosition + " " + rangeMin+" "+rangeMin);
//console.log("DEBUG currentPlayingPosition is outside!" + currentTrackId + " currentTrackIdDuration=" + currentTrackIdDuration);
}
} else {
//console.log("currentPlayingPosition within range!" + currentPlayingPosition + " " + rangeMin+" "+rangeMin);
if (currentPlayingPosition < rangeMin || currentPlayingPosition > rangeMax) {
console.log("DEBUG ASSERT shouldnt happen tempTrackName=" + tempTrackName + " currentTrackName=" + currentTrackName + " currentTrackId=" + currentTrackId + " currentTrackIdDuration=" + currentTrackIdDuration + " pos=" + currentPlayingPosition + " " + rangeMin + " " + rangeMax);
}
}
} else {
if (currentPlayingPosition < rangeMin || currentPlayingPosition > rangeMax) {
console.log("ASSERT shouldnt happen " + currentPlayingPosition + " " + rangeMin + " " + rangeMax);
}
console.log("DEBUG track changed! tempTrackName=" + tempTrackName + " currentTrackName=" + currentTrackName + " currentTrackId=" + currentTrackId + " currentTrackIdDuration=" + currentTrackIdDuration + " pos=" + currentPlayingPosition + " " + rangeMin + " " + rangeMax );
}

}


Expand Down Expand Up @@ -370,6 +383,7 @@ function spotifyInitTrack(token) {
console.log("DEBUG API CALLED " + trackid + " " + trackname + " " + trackduration + " " + trackprogress);

currentTrackId = trackid;
currentTrackName = trackname;
currentTrackIdDuration = trackduration_s;
getTrack(trackid).then((result) => {
let detail = {
Expand Down

0 comments on commit 030a1a5

Please sign in to comment.