Skip to content

Commit

Permalink
Add version selector to play page
Browse files Browse the repository at this point in the history
  • Loading branch information
Juice10 committed Dec 19, 2023
1 parent 045271e commit f56cd2d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 15 deletions.
4 changes: 4 additions & 0 deletions play/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
</head>
<body>
<p>Events JSON: <a class="json" href="#">loading...</a></p>
<p>
Version:
<select id="versions"></select>
</p>
<div id="jsoneditor" style="width: 100%; height: 400px"></div>
<div id="player">
<p class="loading">Loading player...</p>
Expand Down
14 changes: 2 additions & 12 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
import { populateVersions } from "./populate-versions";
import "./style.css";

import versionsJson from "./versions.json";

function onLoad() {
document.getElementById("versions")!.innerHTML = Object.entries(versionsJson)
.map(([version, config]) => {
const { rrwebVersion } = config;
const isDefault = "default" in config && config.default;

return `<option value="${version}" ${
isDefault ? "selected" : ""
} =>${version} (rrweb v${rrwebVersion})</option>`;
})
.join("");
populateVersions();

console.log("Welcome to rrwebdebug.com!");
}
Expand Down
19 changes: 16 additions & 3 deletions src/play.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import versionsJson from "./versions.json";
import populateVersions from "./populate-versions";

function allowedVersion(version) {
const allVersions = Object.keys(versionsJson);
Expand All @@ -15,13 +16,22 @@ function defaultVersion() {
}

function scriptSRC(version) {
if (!allowedVersion(version)) version = defaultVersion();
return `https://cdn.jsdelivr.net/npm/rrweb-player@${version}/dist/index.js`;
}
function styleHref(version) {
if (!allowedVersion(version)) version = defaultVersion();
return `https://cdn.jsdelivr.net/npm/rrweb-player@${version}/dist/style.css`;
}
function setupVersionSelector(version) {
populateVersions(version);
document.getElementById("versions").addEventListener("change", (e) => {
const newVersion = e.target.value;
// reload page with selected version
const location = new URL(document.location);
location.searchParams.set("version", newVersion);

document.location.href = location.href;
});
}

function playVideo(events, config) {
const component = new rrwebPlayer({
Expand Down Expand Up @@ -69,7 +79,8 @@ function getJSONBlobId(url) {
async function startPlayer() {
const location = new URL(document.location);
const url = location.searchParams.get("url");
const version = location.searchParams.get("version");
let version = location.searchParams.get("version");
if (!allowedVersion(version)) version = defaultVersion();
const canvas = Boolean(location.searchParams.get("canvas"));
const autoPlay = Boolean(location.searchParams.get("play"));
const useVirtualDom = Boolean(location.searchParams.get("virtual-dom"));
Expand Down Expand Up @@ -131,6 +142,8 @@ async function startPlayer() {
showJSON(events);
});

setupVersionSelector(version);

document.head.appendChild(scriptEl);
document.querySelector("a.json").setAttribute("href", url);
document.querySelector("a.json").innerText = url;
Expand Down
20 changes: 20 additions & 0 deletions src/populate-versions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import versionsJson from "./versions.json";
export function populateVersions(selectedVersion?: string) {
document.getElementById("versions")!.innerHTML = Object.entries(versionsJson)
.map(([version, config]) => {
const { rrwebVersion } = config;
const isDefault = "default" in config && config.default;

return `<option value="${version}" ${
isDefault ? "selected" : ""
}>${version} (rrweb v${rrwebVersion})</option>`;
})
.join("");

if (selectedVersion) {
(document.getElementById("versions") as HTMLSelectElement).value =
selectedVersion;
}
}

export default populateVersions;

0 comments on commit f56cd2d

Please sign in to comment.