-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathloadwabac.js
122 lines (97 loc) · 3.19 KB
/
loadwabac.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
const proxyPrefix = "https://wabac-cors-proxy.webrecorder.workers.dev/proxy/";
// [TODO]
// eslint-disable-next-line @typescript-eslint/no-unused-vars
class WabacLiveProxy {
constructor({ collName = "liveproxy", adblockUrl = undefined } = {}) {
this.url = "";
this.ts = "";
this.collName = collName;
this.matchRx = new RegExp(`${collName}\\/([\\d]+)?\\w\\w_\\/(.*)`);
this.adblockUrl = adblockUrl;
this.queryParams = { injectScripts: "./custom.js" };
}
async init() {
window.addEventListener("load", () => {
const iframe = document.querySelector("#content");
if (iframe) {
iframe.addEventListener("load", () =>
this.onIframeLoad(iframe.contentWindow.location.href),
);
}
});
const scope = "./";
// also add inject of custom.js as a script into each replayed page
await navigator.serviceWorker.register(
"./sw.js?" + new URLSearchParams(this.queryParams).toString(),
{ scope },
);
let initedResolve = null;
const inited = new Promise((resolve) => (initedResolve = resolve));
navigator.serviceWorker.addEventListener("message", (event) => {
if (event.data.msg_type === "collAdded") {
// the replay is ready to be loaded when this message is received
initedResolve();
}
});
const baseUrl = new URL(window.location);
baseUrl.hash = "";
const msg = {
msg_type: "addColl",
name: this.collName,
type: "live",
file: { sourceUrl: `proxy:${proxyPrefix}` },
skipExisting: false,
extraConfig: {
prefix: proxyPrefix,
isLive: false,
baseUrl: baseUrl.href,
baseUrlHashReplay: true,
noPostToGet: true,
archivePrefix: "https://web.archive.org/web/",
adblockUrl: this.adblockUrl,
},
};
if (!navigator.serviceWorker.controller) {
navigator.serviceWorker.addEventListener("controllerchange", () => {
navigator.serviceWorker.controller.postMessage(msg);
});
} else {
navigator.serviceWorker.controller.postMessage(msg);
}
// [TODO]
// eslint-disable-next-line @typescript-eslint/no-misused-promises, @typescript-eslint/no-unnecessary-condition
if (inited) {
await inited;
}
window.addEventListener("load", () => {
this.onHashChange();
});
window.addEventListener("hashchange", () => {
this.onHashChange();
});
this.onHashChange();
}
onHashChange() {
const m = window.location.hash.slice(1).match(/\/?(?:([\d]+)\/)?(.*)/);
const url = m?.[2] || "https://example.com/";
const ts = m?.[1] || "";
// don't change if same url
if (url === this.url && ts === this.ts) {
return;
}
let iframeUrl = ts
? `/w/${this.collName}/${ts}mp_/${url}`
: `/w/${this.collName}/mp_/${url}`;
const iframe = document.querySelector("#content");
iframe.src = iframeUrl;
this.url = url;
this.ts = ts;
window.location.hash = ts ? `#${ts}/${url}` : `#${url}`;
}
onIframeLoad(url) {
const m = url.match(this.matchRx);
this.ts = m[1] || "";
this.url = m[2] || "";
window.location.hash = this.ts ? `#${this.ts}/${this.url}` : `#${this.url}`;
}
}