forked from htmlpreview/htmlpreview.github.com
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathhtmlpreview.js
449 lines (418 loc) · 13.1 KB
/
htmlpreview.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
// SPDX-FileCopyrightText: 2012 - 2021 Jerzy Głowacki <[email protected]>
// SPDX-FileCopyrightText: 2024 Robin Vobruba <[email protected]>
//
// SPDX-License-Identifier: Apache-2.0
// eslint-disable-next-line max-statements
(function () {
/**
* Given a set of variant values, creates an enum.
* @param {string[]} variants -
* A list of unique strings, representing the enum variants.
* @return {object} an enum-like, immutable dictionary
*/
const createEnum = function (variants) {
const enumObject = {};
for (const vari of variants) {
enumObject[vari] = Symbol(vari);
}
return Object.freeze(enumObject);
};
const FORGE_SOFTWARES = createEnum([
'GitHub',
'BitBucket',
'GitLab',
'ForgeJo',
'SourceHut'
]);
const FORGE_HOSTS = createEnum([
'GitHub_com',
'BitBucket_org',
'GitLab_com',
'Allmende_io',
'GitLab_OpenSourceEcology_de',
'CodeBerg_org',
'Git_Sr_Ht'
]);
/**
* Takes any URL to a file on a known git forge,
* and returns the raw version of that files URL on the same forge.
* If it already is the raw version,
* this function just returns it as is.
* @param {URL} forgeFileUrl - Any URL,
* potentially pointing to a git hosted raw (plain-text) file
* @returns {URL} The raw version of the (git hosted) file URL.
*
* NOTE: This function 1st of 2 that is git-forge specific.
*/
// eslint-disable-next-line max-statements
const rawifyForgeUrl = function (forgeFileUrl) {
if (forgeFileUrl === null) {
return null;
}
const forge = extractForge(forgeFileUrl);
const sw = forge[0];
if (sw === null) {
// do nothing
} else if (sw === FORGE_SOFTWARES.GitHub) {
forgeFileUrl.hostname = 'raw.githubusercontent.com';
forgeFileUrl.pathname = forgeFileUrl.pathname.replace(
/^(\/[^/]+\/[^/]+)\/(blob|raw)\/([^/]+\/)/,
'$1/$3'
);
} else if (sw === FORGE_SOFTWARES.BitBucket) {
forgeFileUrl.pathname = forgeFileUrl.pathname.replace(
/^(\/[^/]+\/[^/]+)\/src\/([^/]+\/)/,
'$1/raw/$2'
);
} else if (sw === FORGE_SOFTWARES.GitLab) {
forgeFileUrl.pathname = forgeFileUrl.pathname.replace(
/^(\/[^/]+\/.+?)\/(-\/)?blob\/([^/]+\/)/,
'$1/-/raw/$3'
);
} else if (sw === FORGE_SOFTWARES.ForgeJo) {
forgeFileUrl.pathname = forgeFileUrl.pathname.replace(
/^(\/[^/]+\/[^/]+)\/src\/([^/]+\/)/,
'$1/raw/$2'
);
} else if (sw === FORGE_SOFTWARES.SourceHut) {
forgeFileUrl.pathname = forgeFileUrl.pathname.replace(
/^(\/~[^/]+\/[^/]+)\/tree\/([^/]+)\/item\/([^/]+)/,
'$1/blob/$2/$3'
);
} else {
reportError('Unsupported git-forge software: ' + sw.toString());
}
return forgeFileUrl;
};
/**
* Takes any URL to a file on a known git forge,
* and returns the raw version of that files URL on the same forge.
* If it already is the raw version,
* this function just returns it as is.
* @param {string} previewFileUrl - Any URL,
* potentially pointing to a git hosted raw (plain-text) file
* @returns {URL} The raw version of the (git hosted) file URL.
*
* NOTE: This function 1st of 2 that is git-forge specific.
*/
const rawifyForgeUrlStr = function (previewFileUrl) {
let previewFileUrlParsed;
try {
previewFileUrlParsed = new URL(previewFileUrl);
} catch (err) {
reportError('Invalid URL provided in parameter "url"');
}
return rawifyForgeUrl(previewFileUrlParsed);
};
/**
* Reports an error directly in HTML.
* @param {string} msg - The error message to be reported to the user.
* @returns {void}
*/
const reportError = function (msg) {
const errP = document.createElement('p');
errP.innerHTML = msg;
document.body.appendChild(errP);
throw new SyntaxError(msg);
};
/**
* If the first parameter is a URL to a file on a known git forge,
* returns the URL to the raw version of this file
* (vs the HTML/Web view of it).
* @returns {string} The raw version of the (git hosted) file URL
* requested to be previewed.
*/
const getRawFileUrl = function () {
if (location.search.length === 0) {
return null;
}
const params = new URLSearchParams(location.search);
const previewFileUrl = params.get('url');
if (previewFileUrl === null) {
reportError('Missing required parameter "url"');
// reportError('Please use "...?url=..." vs the old "...?..."');
}
return rawifyForgeUrlStr(previewFileUrl).href;
};
const RE_GITLAB_PATH = /^\/[^/]+\/.+\/(-\/)?(blob|raw)\/[^/]+/;
const RE_SOURCEHUT_PATH = /^\/~[^/]+\/[^/]+\/(tree|blob)\/[^/]+/;
/**
* Extracts the forge software and host,
* the given a URL that points to a file on a known git forge.
* @param {URL} url - Any URL,
* potentially pointing to a git hosted raw (plain-text) file
* @returns {{ software: Symbol, host: Symbol}} `(software, host)`,
* or `(null, null)` if unsupported/unidentified/
* not a git hosted raw file.
*
* NOTE: This is function 2nd of 2 that is git-forge specific.
*/
// eslint-disable-next-line max-statements
const extractForge = function (url) {
let software = null;
let host = null;
if (url.host == 'raw.githubusercontent.com'
|| url.host == 'github.com') {
software = FORGE_SOFTWARES.GitHub;
host = FORGE_HOSTS.GitHub_com;
} else if (url.host == 'bitbucket.org'
&& (/^\/[^/]+\/[^/]+\/(src|raw)\/[^/]+/).test(url.pathname)) {
software = FORGE_SOFTWARES.BitBucket;
host = FORGE_HOSTS.BitBucket_org;
} else if (url.host == 'gitlab.com'
&& RE_GITLAB_PATH.test(url.pathname)) {
software = FORGE_SOFTWARES.GitLab;
host = FORGE_HOSTS.GitLab_com;
} else if (url.host == 'lab.allmende.io'
&& RE_GITLAB_PATH.test(url.pathname)) {
software = FORGE_SOFTWARES.GitLab;
host = FORGE_HOSTS.Lab_Allmende_io;
} else if (url.host == 'gitlab.opensourceecology.de'
&& RE_GITLAB_PATH.test(url.pathname)) {
software = FORGE_SOFTWARES.GitLab;
host = FORGE_HOSTS.GitLab_OpenSourceEcology_de;
} else if (url.host == 'codeberg.org'
&& (/^\/[^/]+\/[^/]+\/(src|raw)\/[^/]+/).test(url.pathname)) {
software = FORGE_SOFTWARES.ForgeJo;
host = FORGE_HOSTS.CodeBerg_org;
} else if (url.host == 'git.sr.ht'
&& RE_SOURCEHUT_PATH.test(url.pathname)) {
software = FORGE_SOFTWARES.SourceHut;
host = FORGE_HOSTS.Git_Sr_Ht;
}
return [software, host];
};
/**
* Indicates whether the given URL points to a file on a known git forge.
* @param {URL} url - Any URL,
* potentially pointing to a git hosted raw (plain-text) file
* @returns {boolean} `true` if the given URL indeed does point
* to a git hosted raw file
*/
const isGitForgeFileUrlParsed = function (url) {
return extractForge(url)[0] !== null;
};
/**
* Indicates whether the given URL points to a file on a known git forge.
* @param {string} url - Any URL,
* potentially pointing to a git hosted raw (plain-text) file
* @returns {boolean} `true` if the given URL indeed does point
* to a git hosted raw file
*/
const isGitForgeFileUrl = function (url) {
try {
return isGitForgeFileUrlParsed(new URL(url));
} catch (err) {
if (err instanceof RangeError) {
return false;
}
throw err;
}
};
/**
* Returns whether the given URL points to an HTML file,
* considering only the file extension.
* @param {string} url - Any URL
* @returns {boolean} 'true' if the given URL points to an HTML file.
*/
const isHtmlUrl = function (url) {
return (url.indexOf('.html') > 0 || url.indexOf('.htm') > 0);
};
/**
* Returns the base URL of our service,
* to which the git hosted file URL can be appended.
* @returns {string} a URL representing the our service base.
*/
const getServiceBase = function () {
if (window.location) {
const loc = window.location;
return loc.origin + loc.pathname;
}
// Fallback value
return 'https://html-preview.github.io/';
};
/**
* Rewrite URL so it can be loaded using CORS proxy.
* @param {string} url - Any URL
* @returns {string} The re-routed (for preview) version of the provided URL
*/
const rewrite = function (url) {
return location.origin + location.pathname + '?url=' + url;
};
/**
* Rewrite URL so it can be loaded using CORS proxy,
* if it points to a file on a known git forge.
* @param {object} obj - An object containing a property that is a URL
* @param {string} prop - The name of the URL property
* @returns {void}
*/
const rewriteCond = function (obj, prop) {
// Get absolute URL
const url = obj[prop];
if (isGitForgeFileUrl(url)) {
obj[prop] = rewrite(url);
}
};
const serviceBase = getServiceBase();
document.getElementById('service_base').innerHTML = serviceBase + '?url=';
const previewForm = document.getElementById('previewform');
// Get URL of the raw file
const rawFileUrl = getRawFileUrl();
const replaceFrames = function () {
const frame = document.querySelectorAll('iframe[src],frame[src]');
for (let i = 0; i < frame.length; ++i) {
rewriteCond(frame[i], 'src');
}
};
const replaceObjects = function () {
const object = document.querySelectorAll('object[data]');
for (let i = 0; i < object.length; ++i) {
rewriteCond(object[i], 'data');
}
};
const replaceLinks = function () {
const a = document.querySelectorAll('a[href]');
let href;
for (let i = 0; i < a.length; ++i) {
// Get absolute URL
href = a[i].href;
// Check if it's an anchor
if (a[i].hash.length > 0) {
// Rewrite links to this document only
if ((a[i].origin + a[i].pathname) == rawFileUrl) {
// Then rewrite URL with support for empty anchor
a[i].href
= location.origin + location.pathname + location.search
+ '#' + a[i].hash.substring(1);
}
// Do not modify external URLs with fragment
} else if (isGitForgeFileUrl(href) && isHtmlUrl(href)) {
// Then rewrite URL so it can be loaded using CORS proxy
a[i].href = rewrite(href);
}
}
};
const replaceStylesheets = function () {
const link = document.querySelectorAll('link[rel=stylesheet]');
const links = [];
let href;
for (let i = 0; i < link.length; ++i) {
// Get absolute URL
href = link[i].href;
if (href != "" && isGitForgeFileUrl(href)) {
// Then add it to links queue and fetch using CORS proxy
links.push(fetchProxy(href, null, 0));
}
}
Promise.all(links).then(function (res) {
for (let i = 0; i < res.length; ++i) {
loadCSS(res[i]);
}
});
};
const replaceScripts = function () {
// eslint-disable-next-line @stylistic/js/max-len
const script = document.querySelectorAll('script[type="text/htmlpreview"]');
const scripts = [];
let src;
for (let i = 0; i < script.length; ++i) {
// Get absolute URL
src = script[i].src;
if (src != "" && isGitForgeFileUrl(src)) {
// Then add it to scripts queue and fetch using CORS proxy
scripts.push(fetchProxy(src, null, 0));
} else {
script[i].removeAttribute('type');
// Add inline script to queue to eval in order
scripts.push(script[i].innerHTML);
}
}
Promise.all(scripts).then(function (res) {
for (let i = 0; i < res.length; ++i) {
loadJS(res[i]);
}
// Dispatch DOMContentLoaded event after loading all scripts
document.dispatchEvent(new Event(
'DOMContentLoaded',
{bubbles: true, cancelable: true}
));
});
};
const replaceAssets = function () {
// Framesets
if (document.querySelectorAll('frameset').length) {
// Don't replace CSS/JS if it's a frameset,
// because it will be erased by document.write()
return;
}
replaceFrames();
replaceObjects();
replaceLinks();
replaceStylesheets();
replaceScripts();
};
const loadHTML = function (data) {
if (data) {
// Add <base> just after <head>
// and replace <script type="text/javascript">
// with <script type="text/htmlpreview">
data = data.replace(
/<head([^>]*)>/i,
'<head$1><base href="' + rawFileUrl + '">'
).replace(
// eslint-disable-next-line @stylistic/js/max-len
/<script(\s*src=["'][^"']*["'])?(\s*type=["'](text|application)\/javascript["'])?/gi,
'<script type="text/htmlpreview"$1'
);
// Delay updating document to have it cleared before
setTimeout(function () {
document.open();
document.write(data);
document.close();
replaceAssets();
}, 10);
}
};
const loadCSS = function (data) {
if (data) {
const style = document.createElement('style');
style.innerHTML = data;
document.head.appendChild(style);
}
};
const loadJS = function (data) {
if (data) {
const script = document.createElement('script');
script.innerHTML = data;
document.body.appendChild(script);
}
};
const fetchProxy = function (url, options, i) {
const proxy = [
// try without proxy first
'',
'https://api.codetabs.com/v1/proxy/?quest='
];
return fetch(proxy[i] + url, options).then(function (res) {
if (!res.ok) {
const errMsg = res.status + ' ' + res.statusText;
throw new Error('Cannot load ' + url + ': ' + errMsg);
}
return res.text();
}).catch(function (error) {
if (i === proxy.length - 1) {
throw error;
}
return fetchProxy(url, options, i + 1);
});
};
if (rawFileUrl && rawFileUrl.indexOf(location.hostname) < 0) {
fetchProxy(rawFileUrl, null, 0).then(loadHTML).catch(function (error) {
// console.error(error);
previewForm.style.display = 'block';
previewForm.innerText = error;
});
} else {
previewForm.style.display = 'block';
}
})();