|
1 | | -import { PlayerAnalyticsConnector } from "@eyevinn/player-analytics-client-sdk-web"; |
| 1 | +import { PlayerAnalyticsConnector } from "../index.ts"; |
2 | 2 |
|
3 | 3 | function getDeviceInfo() { |
4 | 4 | const ua = navigator.userAgent || ""; |
@@ -30,51 +30,69 @@ function getDeviceInfo() { |
30 | 30 | return { deviceType, deviceModel }; |
31 | 31 | } |
32 | 32 |
|
33 | | -document.addEventListener("DOMContentLoaded", async () => { |
34 | | - const videoElement = document.querySelector("video"); |
| 33 | +document.addEventListener("DOMContentLoaded", () => { |
| 34 | + const videoElement = document.getElementById("videoPlayer"); |
35 | 35 | const inputElement = document.getElementById("videoUrlInput"); |
| 36 | + const loadBtn = document.getElementById("loadButton"); |
| 37 | + |
36 | 38 | const eventsinkUrl = |
37 | 39 | "https://eyevinn-epas1.eyevinn-player-analytics-eventsink.auto.prod.osaas.io/"; |
38 | | - const debug = false; |
| 40 | + const analytics = new PlayerAnalyticsConnector(eventsinkUrl, false); |
39 | 41 |
|
40 | | - const analytics = new PlayerAnalyticsConnector(eventsinkUrl, debug); |
| 42 | + function cleanUrl(raw) { |
| 43 | + let url = raw.trim().replace(/^"|"$/g, ""); |
| 44 | + if (url.includes(" ")) { |
| 45 | + url = url.split(" ")[0]; |
| 46 | + } |
| 47 | + return url; |
| 48 | + } |
41 | 49 |
|
42 | | - await analytics.init({ |
43 | | - sessionId: `demo-page-${Date.now()}`, |
44 | | - heartbeatInterval: 10000, |
45 | | - }); |
| 50 | + async function loadVideo(urlRaw) { |
| 51 | + const url = cleanUrl(urlRaw); |
| 52 | + if (!url) return; |
46 | 53 |
|
47 | | - analytics.load(videoElement); |
| 54 | + await analytics.init({ |
| 55 | + sessionId: `demo-page-${Date.now()}`, |
| 56 | + heartbeatInterval: 10000, |
| 57 | + }); |
| 58 | + analytics.load(videoElement); |
| 59 | + if (Hls.isSupported() && url.endsWith(".m3u8")) { |
| 60 | + const hls = new Hls(); |
| 61 | + hls.loadSource(url); |
| 62 | + hls.attachMedia(videoElement); |
| 63 | + } else { |
| 64 | + videoElement.src = url; |
| 65 | + } |
48 | 66 |
|
49 | | - // Default video |
50 | | - let streamUrl = |
51 | | - "https://archive.org/serve/big-bunny-sample-video/SampleVideo.ia.mp4"; |
52 | | - const contentId = streamUrl.split("/").pop() || ""; |
53 | | - const { deviceType, deviceModel } = getDeviceInfo(); |
| 67 | + const contentId = url.split("/").pop() || ""; |
| 68 | + const { deviceType, deviceModel } = getDeviceInfo(); |
| 69 | + analytics.reportMetadata({ |
| 70 | + live: false, |
| 71 | + contentId: contentId, |
| 72 | + contentUrl: videoElement.src, |
| 73 | + deviceType: deviceType, |
| 74 | + deviceModel: deviceModel, |
| 75 | + }); |
| 76 | + videoElement.play(); |
| 77 | + } |
54 | 78 |
|
55 | | - videoElement.src = streamUrl; |
56 | | - analytics.reportMetadata({ |
57 | | - live: false, |
58 | | - contentId: contentId, |
59 | | - contentUrl: streamUrl, |
60 | | - deviceType: deviceType, |
61 | | - deviceModel: deviceModel, |
| 79 | + loadBtn.addEventListener("click", () => { |
| 80 | + const url = inputElement.value; |
| 81 | + loadVideo(url); |
62 | 82 | }); |
63 | 83 |
|
64 | | - inputElement.addEventListener("change", () => { |
65 | | - const newUrl = inputElement.value.trim(); |
66 | | - if (newUrl) { |
67 | | - videoElement.src = newUrl; |
68 | | - const contentId = streamUrl.split("/").pop() || ""; |
69 | | - |
70 | | - analytics.reportMetadata({ |
71 | | - live: false, |
72 | | - contentId: contentId, |
73 | | - contentUrl: newUrl, |
74 | | - deviceType: deviceType, |
75 | | - deviceModel: deviceModel, |
76 | | - }); |
77 | | - videoElement.play(); |
| 84 | + inputElement.addEventListener("keydown", (e) => { |
| 85 | + if (e.key === "Enter") { |
| 86 | + loadBtn.click(); |
78 | 87 | } |
79 | 88 | }); |
| 89 | + |
| 90 | + // Accessibility for Grafana |
| 91 | + document |
| 92 | + .getElementById("grafana-link") |
| 93 | + .addEventListener("keydown", function (e) { |
| 94 | + if (e.key === "Enter" || e.key === " ") { |
| 95 | + window.open(this.href, "_blank", "noopener"); |
| 96 | + } |
| 97 | + }); |
80 | 98 | }); |
0 commit comments