Skip to content

Commit

Permalink
Add button to save state
Browse files Browse the repository at this point in the history
  • Loading branch information
avelad committed Nov 18, 2024
1 parent 5eb4d57 commit 2a0d106
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ body {
margin-top: 15px;
}

#save-state {
display: block;
}

#shaka-player-version {
margin-top: 15px;
}
Expand Down
34 changes: 34 additions & 0 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ function setupUI () {
configContainer.appendChild(nativeHlsInput);
}
}
const saveStateButton = document.createElement('button');
saveStateButton.id = 'save-state';
saveStateButton.textContent = 'Update URL';
saveStateButton.addEventListener('click', remakeHash);
configContainer.appendChild(saveStateButton);
document.body.appendChild(configContainer);

if (params.url) {
Expand Down Expand Up @@ -272,6 +277,35 @@ function createPlayer(videoContainer, video, url) {
return player;
}

function remakeHash() {
const params = [];

const nativeHlsInput = document.getElementById('native-hls-input');
if (nativeHlsInput && nativeHlsInput.checked) {
params.push('hls');
}

const urls = [];
const inputs = document.querySelectorAll('input[type=url]');
for (const input of inputs) {
const url = input.value;
if (!url) {
continue;
}
urls.push(url);
}
if (urls.length) {
params.push('url=' + urls.join(','));
}

const state = null;
const title = '';
const hash = params.length ? '#' + params.join(';') : '';
// Calling history.replaceState can change the URL or hash of the page
// without actually triggering any changes
history.replaceState(state, title, document.location.pathname + hash);
}

function initApp() {
shaka.polyfill.installAll();
if (shaka.Player.isBrowserSupported()) {
Expand Down

0 comments on commit 2a0d106

Please sign in to comment.