-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from WayOfTheCrab/feature/link-to-playhead-loca…
…tion make it possible to link to a particular playhead location
- Loading branch information
Showing
3 changed files
with
61 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,51 @@ | ||
const copyPosToClipboard = (ev) => { | ||
ev.preventDefault(); | ||
|
||
const permaLink = document.querySelector(".perma-link"); | ||
navigator.clipboard.writeText(permaLink.href); | ||
|
||
const origText = permaLink.innerText; | ||
permaLink.innerText = "Copied to clipboard..."; | ||
ev.target.onmouseleave = () => { | ||
setTimeout(() => { | ||
permaLink.innerText = origText; | ||
}, 1000); | ||
}; | ||
} | ||
|
||
onload = (_) => { | ||
const players = document.querySelectorAll("audio"); | ||
// ----------------------------------------------------------------------------- | ||
// - Link to a position - | ||
// ----------------------------------------------------------------------------- | ||
const querystring = new URLSearchParams(window.location.search); | ||
const playhead = querystring.get("t"); | ||
|
||
// ----------------------------------------------------------------------------- | ||
// - Remember playhead position - | ||
// ----------------------------------------------------------------------------- | ||
const player = document.querySelector("audio"); | ||
const permaLink = document.querySelector(".perma-link"); | ||
if (!(player || permaLink)) return; | ||
|
||
permaLink.onclick = copyPosToClipboard; | ||
|
||
const id = "player-id-" + player.currentSrc; | ||
|
||
for (i in players) { | ||
const player = players[i]; | ||
const id = "player-id-" + player.currentSrc; | ||
var lastTime = parseFloat(window.localStorage.getItem(id)) || 0; | ||
|
||
var lastTime = parseFloat(window.localStorage.getItem(id)) || 0; | ||
player.currentTime = lastTime; | ||
player.ontimeupdate = (_) => { | ||
permaLink.href = `${permaLink.dataset['url']}?t=${Math.round(player.currentTime)}`; | ||
if (Math.abs(player.currentTime - lastTime) > 5) { | ||
lastTime = player.currentTime; | ||
window.localStorage.setItem(id, lastTime); | ||
} | ||
}; | ||
|
||
player.currentTime = lastTime; | ||
player.ontimeupdate = (_) => { | ||
if (Math.abs(player.currentTime - lastTime) > 5) { | ||
lastTime = player.currentTime; | ||
window.localStorage.setItem(id, lastTime); | ||
} | ||
}; | ||
// If the playhead position is set in the querystring | ||
// that will take precedence over the stored location | ||
if (playhead != null) { | ||
const playheadPos = parseFloat(playhead || 0); | ||
player.currentTime = playheadPos; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters