|
| 1 | +class WabacReplay |
| 2 | +{ |
| 3 | + constructor(prefix, url, ts, staticPrefix, coll, swScopePrefix) { |
| 4 | + this.prefix = prefix; |
| 5 | + this.url = url; |
| 6 | + this.ts = ts; |
| 7 | + this.staticPrefix = staticPrefix; |
| 8 | + this.collName = coll; |
| 9 | + this.isRoot = coll === "$root"; |
| 10 | + this.archivePrefix = this.isRoot ? "/" : `/${this.collName}/`; |
| 11 | + this.swScope = swScopePrefix; |
| 12 | + this.adblockUrl = undefined; |
| 13 | + |
| 14 | + this.queryParams = {"replayPrefix": ""}; |
| 15 | + if (this.isRoot) { |
| 16 | + this.queryParams["root"] = "$root"; |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + async init() { |
| 21 | + const scope = this.swScope + "/"; |
| 22 | + |
| 23 | + await navigator.serviceWorker.register( |
| 24 | + `${this.staticPrefix}/sw.js?` + new URLSearchParams(this.queryParams).toString(), |
| 25 | + { scope }, |
| 26 | + ); |
| 27 | + |
| 28 | + let initedResolve = null; |
| 29 | + |
| 30 | + const inited = new Promise((resolve) => initedResolve = resolve); |
| 31 | + |
| 32 | + navigator.serviceWorker.addEventListener("message", (event) => { |
| 33 | + if (event.data.msg_type === "collAdded") { |
| 34 | + // the replay is ready to be loaded when this message is received |
| 35 | + initedResolve(); |
| 36 | + } |
| 37 | + }); |
| 38 | + |
| 39 | + const proxyPrefix = ""; |
| 40 | + |
| 41 | + const msg = { |
| 42 | + msg_type: "addColl", |
| 43 | + name: this.collName, |
| 44 | + type: "live", |
| 45 | + root: this.isRoot, |
| 46 | + file: {"sourceUrl": `proxy:${proxyPrefix}`}, |
| 47 | + skipExisting: true, |
| 48 | + extraConfig: { |
| 49 | + prefix: proxyPrefix, |
| 50 | + isLive: false, |
| 51 | + baseUrl: this.prefix, |
| 52 | + baseUrlAppendReplay: true, |
| 53 | + noPostToGet: false, |
| 54 | + archivePrefix: this.archivePrefix, |
| 55 | + archiveMod: "ir_", |
| 56 | + adblockUrl: this.adblockUrl, |
| 57 | + noPostToGet: true, |
| 58 | + }, |
| 59 | + }; |
| 60 | + |
| 61 | + if (!navigator.serviceWorker.controller) { |
| 62 | + navigator.serviceWorker.addEventListener("controllerchange", () => { |
| 63 | + navigator.serviceWorker.controller.postMessage(msg); |
| 64 | + }); |
| 65 | + } else { |
| 66 | + navigator.serviceWorker.controller.postMessage(msg); |
| 67 | + } |
| 68 | + |
| 69 | + window.addEventListener("message", event => { |
| 70 | + let data = event.data; |
| 71 | + if (window.WBBanner) { |
| 72 | + window.WBBanner.onMessage(event); |
| 73 | + } |
| 74 | + if (data.wb_type === "load" || data.wb_type === "replace-url") { |
| 75 | + history.replaceState({}, data.title, this.prefix + data.ts + '/' + data.url); |
| 76 | + } |
| 77 | + }); |
| 78 | + |
| 79 | + if (inited) { |
| 80 | + await inited; |
| 81 | + } |
| 82 | + |
| 83 | + this.load_url(this.url, this.ts); |
| 84 | + } |
| 85 | + |
| 86 | + // called by the Vue banner when the timeline is clicked |
| 87 | + load_url(url, ts) { |
| 88 | + const iframe = document.querySelector('#replay_iframe'); |
| 89 | + iframe.src = `${this.swScope}${this.archivePrefix}${ts}mp_/${url}`; |
| 90 | + } |
| 91 | +} |
0 commit comments