Skip to content

Commit 0b4da7c

Browse files
authored
chore: Add log export to the demo page (#1522)
* chore: Add log export to the demo page * add note about adding things to log
1 parent bb9133c commit 0b4da7c

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

index.html

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
#segment-metadata pre {
2525
overflow: scroll;
2626
}
27+
button.btn-outline-secondary:hover svg,
28+
button.btn-success svg,
29+
button.btn-danger svg {
30+
fill: white;
31+
}
2732
</style>
2833
</head>
2934
<body class="m-4">
@@ -52,6 +57,9 @@
5257
<li class="nav-item" role="presentation">
5358
<button class="nav-link" id="profile-tab" data-bs-toggle="tab" data-bs-target="#content-steering" type="button" role="tab" aria-selected="false">Content Steering</button>
5459
</li>
60+
<li class="nav-item" role="presentation">
61+
<button class="nav-link" id="profile-tab" data-bs-toggle="tab" data-bs-target="#export-logs" type="button" role="tab" aria-selected="false">Logs</button>
62+
</li>
5563
</ul>
5664

5765
<div class="tab-content container-fluid">
@@ -238,6 +246,30 @@
238246
</dl>
239247
</div>
240248
</div>
249+
</div>
250+
251+
<div class="tab-pane" id="export-logs" role="historypanel">
252+
<div class="row">
253+
<div class="export-logs col-8">
254+
<p>Download or copy the player logs, which should be included when submitting a playback issue.</p>
255+
<p>To insert a comment into the player log, use <code>player.log()</code> in the console, e.g. <code>player.log('Seeking to 500');player.currentTime(500);</code></p>
256+
<button id="download-logs" class="btn btn-outline-secondary">
257+
<span class="icon">
258+
<svg xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 -960 960 960" width="24px" fill="#5f6368">
259+
<path d="M480-320 280-520l56-58 104 104v-326h80v326l104-104 56 58-200 200ZM240-160q-33 0-56.5-23.5T160-240v-120h80v120h480v-120h80v120q0 33-23.5 56.5T720-160H240Z"/>
260+
</svg>
261+
</span>
262+
Download player logs
263+
</button>
264+
<button id="copy-logs" class="btn btn-outline-secondary">
265+
<span class="icon">
266+
<svg xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 -960 960 960" width="24px" fill="#5f6368">
267+
<path d="M360-240q-33 0-56.5-23.5T280-320v-480q0-33 23.5-56.5T360-880h360q33 0 56.5 23.5T800-800v480q0 33-23.5 56.5T720-240H360Zm0-80h360v-480H360v480ZM200-80q-33 0-56.5-23.5T120-160v-560h80v560h440v80H200Zm160-240v-480 480Z"/>
268+
</svg>
269+
</span>
270+
Copy player logs to clipboard
271+
</button>
272+
</div>
241273
</div>
242274
</div>
243275
</div>

scripts/index.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,5 +769,75 @@
769769

770770
// run the change handler for the first time
771771
stateEls.minified.dispatchEvent(newEvent('change'));
772+
773+
// Setup the download / copy log buttons
774+
const downloadLogsButton = document.getElementById('download-logs');
775+
const copyLogsButton = document.getElementById('copy-logs');
776+
777+
/**
778+
* Window location and history joined with line breaks, stringifying any objects
779+
*
780+
* @return {string} Stringified history
781+
*/
782+
const stringifiedLogHistory = () => {
783+
const player = document.querySelector('video-js').player;
784+
const logs = [].concat(player.log.history());
785+
const withVhs = !!player.tech(true).vhs;
786+
787+
return [
788+
window.location.href,
789+
window.navigator.userAgent,
790+
`Video.js ${window.videojs.VERSION}`,
791+
`Using VHS: ${withVhs}`,
792+
withVhs ? JSON.stringify(player.tech(true).vhs.version()) : ''
793+
].concat(logs.map(entryArgs => {
794+
return entryArgs.map(item => {
795+
return typeof item === 'object' ? JSON.stringify(item) : item;
796+
});
797+
})).join('\n');
798+
};
799+
800+
/**
801+
* Turn a bootstrap button class on briefly then revert to btn-outline-ptimary
802+
*
803+
* @param {HTMLElement} el Element to add class to
804+
* @param {string} stateClass Bootstrap button class suffix
805+
*/
806+
const doneFeedback = (el, stateClass) => {
807+
el.classList.add(`btn-${stateClass}`);
808+
el.classList.remove('btn-outline-secondary');
809+
810+
window.setTimeout(() => {
811+
el.classList.add('btn-outline-secondary');
812+
el.classList.remove(`btn-${stateClass}`);
813+
}, 1500);
814+
};
815+
816+
downloadLogsButton.addEventListener('click', function() {
817+
const logHistory = stringifiedLogHistory();
818+
const a = document.createElement('a');
819+
const href = URL.createObjectURL(new Blob([logHistory], { type: 'text/plain' }));
820+
821+
a.setAttribute('download', 'vhs-player-logs.txt');
822+
a.setAttribute('target', '_blank');
823+
a.href = href;
824+
a.click();
825+
a.remove();
826+
URL.revokeObjectURL(href);
827+
doneFeedback(downloadLogsButton, 'success');
828+
});
829+
830+
copyLogsButton.addEventListener('click', function() {
831+
const logHistory = stringifiedLogHistory();
832+
833+
window.navigator.clipboard.writeText(logHistory).then(z => {
834+
doneFeedback(copyLogsButton, 'success');
835+
}).catch(e => {
836+
doneFeedback(copyLogsButton, 'danger');
837+
console.log('Copy failed', e);
838+
});
839+
840+
});
772841
};
842+
773843
}(window));

0 commit comments

Comments
 (0)