js_prelude.js 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315
  1. /* Web platform APIs for the NebOS browser, written in JavaScript on top of a
  2. * few C primitives (js_browser.c):
  3. * __nb_http(method, url, contentType, body) -> [status, ctype, finalUrl, text]
  4. * __nb_storage(kind[, text]) kind 0 = localStorage, 1 = sessionStorage
  5. * __nb_resolve(base, relative) -> absolute URL
  6. * __nb_nodeproto prototype shared by every DOM node wrapper
  7. * It runs once per page state, before the page's scripts; the primitives are
  8. * removed from the global object afterwards. Embedded by js_prelude.S. */
  9. (function (g) {
  10. "use strict";
  11. var http = g.__nb_http, storage = g.__nb_storage, resolve = g.__nb_resolve;
  12. var nodeproto = g.__nb_nodeproto;
  13. delete g.__nb_http; delete g.__nb_storage; delete g.__nb_resolve;
  14. delete g.__nb_nodeproto;
  15. var hasOwn = Object.prototype.hasOwnProperty;
  16. function def(name, value) {
  17. Object.defineProperty(g, name, { value: value, writable: true,
  18. configurable: true, enumerable: false });
  19. }
  20. /* ---- console, timers, misc globals ---- */
  21. var con = g.console, rawlog = con.log;
  22. /* Errors print with their stack (Vue reports caught errors this way). */
  23. function fmt(a) {
  24. if (a instanceof Error)
  25. return a.name + ": " + a.message + (a.stack ? "\n" + a.stack : "");
  26. if (a !== null && typeof a === "object" && !(typeof a.nodeType === "number")) {
  27. try { return JSON.stringify(a); } catch (e) { return String(a); }
  28. }
  29. return a;
  30. }
  31. function logger(prefix) {
  32. return function () {
  33. var args = Array.prototype.map.call(arguments, fmt);
  34. if (prefix) args.unshift(prefix);
  35. rawlog.apply(con, args);
  36. };
  37. }
  38. con.log = logger("");
  39. con.info = logger("");
  40. con.debug = logger("");
  41. con.dir = logger("");
  42. con.warn = logger("[warn]");
  43. con.error = logger("[error]");
  44. con.trace = logger("[trace]");
  45. ["group", "groupCollapsed", "groupEnd", "time", "timeEnd", "timeLog",
  46. "table", "count", "countReset", "clear"].forEach(function (k) {
  47. if (!con[k]) con[k] = function () {};
  48. });
  49. con.assert = function (c) {
  50. if (!c) con.log.apply(con, ["Assertion failed:"].concat(
  51. Array.prototype.slice.call(arguments, 1)));
  52. };
  53. def("self", g); def("top", g); def("parent", g); def("frames", g);
  54. def("isSecureContext", true);
  55. def("origin", g.location.origin);
  56. /* The engine's own Promise, captured before any page script runs: core-js
  57. * (heise's consent script) installs a Promise polyfill that schedules its
  58. * reactions through queueMicrotask, and a queueMicrotask built on the
  59. * replaced global Promise would call back into the polyfill forever. */
  60. var NativePromise = Promise;
  61. def("queueMicrotask", function (f) { NativePromise.resolve().then(f); });
  62. /* Present so core-js keeps the native Promise (it polyfills browsers that
  63. * lack unhandled-rejection events). */
  64. class PromiseRejectionEvent {
  65. constructor(type, init) {
  66. this.type = type;
  67. this.promise = init && init.promise;
  68. this.reason = init && init.reason;
  69. }
  70. }
  71. def("PromiseRejectionEvent", PromiseRejectionEvent);
  72. def("requestIdleCallback", function (f) {
  73. return setTimeout(function () {
  74. f({ didTimeout: false, timeRemaining: function () { return 10; } });
  75. }, 1);
  76. });
  77. def("cancelIdleCallback", function (id) { clearTimeout(id); });
  78. def("structuredClone", function (v) {
  79. return v === undefined ? v : JSON.parse(JSON.stringify(v));
  80. });
  81. class DOMException extends Error {
  82. constructor(message, name) {
  83. super(message === undefined ? "" : message);
  84. this.name = name === undefined ? "Error" : name;
  85. }
  86. }
  87. def("DOMException", DOMException);
  88. /* ---- base64 ---- */
  89. var B64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  90. def("btoa", function (s) {
  91. s = String(s);
  92. var out = "", i;
  93. for (i = 0; i < s.length; i += 3) {
  94. var a = s.charCodeAt(i), b = s.charCodeAt(i + 1),
  95. c = s.charCodeAt(i + 2);
  96. if (a > 255 || b > 255 || c > 255)
  97. throw new DOMException("Invalid character",
  98. "InvalidCharacterError");
  99. var n = (a << 16) | ((b || 0) << 8) | (c || 0);
  100. out += B64[n >> 18 & 63] + B64[n >> 12 & 63] +
  101. (i + 1 < s.length ? B64[n >> 6 & 63] : "=") +
  102. (i + 2 < s.length ? B64[n & 63] : "=");
  103. }
  104. return out;
  105. });
  106. def("atob", function (s) {
  107. s = String(s).replace(/[\s=]/g, "");
  108. var out = "", bits = 0, acc = 0, i;
  109. for (i = 0; i < s.length; i++) {
  110. var v = B64.indexOf(s[i]);
  111. if (v < 0)
  112. throw new DOMException("Invalid character",
  113. "InvalidCharacterError");
  114. acc = (acc << 6) | v;
  115. bits += 6;
  116. if (bits >= 8) {
  117. bits -= 8;
  118. out += String.fromCharCode((acc >> bits) & 255);
  119. }
  120. }
  121. return out;
  122. });
  123. /* ---- text encoding ---- */
  124. function utf8(s) {
  125. var out = [], i;
  126. s = String(s);
  127. for (i = 0; i < s.length; i++) {
  128. var c = s.charCodeAt(i);
  129. if (c >= 0xd800 && c < 0xdc00 && i + 1 < s.length) {
  130. var d = s.charCodeAt(i + 1);
  131. if (d >= 0xdc00 && d < 0xe000) {
  132. c = 0x10000 + ((c - 0xd800) << 10) + (d - 0xdc00);
  133. i++;
  134. }
  135. }
  136. if (c < 0x80) out.push(c);
  137. else if (c < 0x800) out.push(0xc0 | c >> 6, 0x80 | c & 63);
  138. else if (c < 0x10000)
  139. out.push(0xe0 | c >> 12, 0x80 | c >> 6 & 63, 0x80 | c & 63);
  140. else
  141. out.push(0xf0 | c >> 18, 0x80 | c >> 12 & 63,
  142. 0x80 | c >> 6 & 63, 0x80 | c & 63);
  143. }
  144. return new Uint8Array(out);
  145. }
  146. function bytesOf(b) {
  147. if (b instanceof ArrayBuffer) return new Uint8Array(b);
  148. if (ArrayBuffer.isView(b))
  149. return new Uint8Array(b.buffer, b.byteOffset, b.byteLength);
  150. return utf8(b);
  151. }
  152. function unutf8(u) {
  153. var s = "", i = 0;
  154. while (i < u.length) {
  155. var c = u[i++];
  156. if (c >= 0xf0 && i + 2 < u.length + 0) {
  157. c = ((c & 7) << 18) | ((u[i++] & 63) << 12) |
  158. ((u[i++] & 63) << 6) | (u[i++] & 63);
  159. } else if (c >= 0xe0) {
  160. c = ((c & 15) << 12) | ((u[i++] & 63) << 6) | (u[i++] & 63);
  161. } else if (c >= 0xc0) {
  162. c = ((c & 31) << 6) | (u[i++] & 63);
  163. }
  164. s += String.fromCodePoint(c);
  165. }
  166. return s;
  167. }
  168. class TextEncoder {
  169. get encoding() { return "utf-8"; }
  170. encode(s) { return utf8(s === undefined ? "" : s); }
  171. }
  172. class TextDecoder {
  173. constructor(label) { this.encoding = "utf-8"; }
  174. decode(b) { return b ? unutf8(bytesOf(b)) : ""; }
  175. }
  176. def("TextEncoder", TextEncoder);
  177. def("TextDecoder", TextDecoder);
  178. /* ---- URLSearchParams / URL ---- */
  179. function qenc(s) {
  180. return encodeURIComponent(s).replace(/%20/g, "+").replace(/[!'()~]/g,
  181. function (c) {
  182. return "%" + c.charCodeAt(0).toString(16).toUpperCase();
  183. });
  184. }
  185. function qdec(s) {
  186. s = s.replace(/\+/g, " ");
  187. try { return decodeURIComponent(s); } catch (e) { return s; }
  188. }
  189. class URLSearchParams {
  190. constructor(init) {
  191. this._l = [];
  192. this._url = null;
  193. if (init == null) return;
  194. if (init instanceof URLSearchParams) {
  195. this._l = init._l.map(function (p) { return [p[0], p[1]]; });
  196. return;
  197. }
  198. if (typeof init === "object" || typeof init === "function") {
  199. if (typeof init[Symbol.iterator] === "function") {
  200. for (var p of init)
  201. this._l.push([String(p[0]), String(p[1])]);
  202. } else {
  203. for (var k of Object.keys(init))
  204. this._l.push([k, String(init[k])]);
  205. }
  206. return;
  207. }
  208. this._parse(String(init));
  209. }
  210. _parse(s) {
  211. this._l = [];
  212. if (s[0] === "?") s = s.slice(1);
  213. for (var part of s.split("&")) {
  214. if (!part) continue;
  215. var i = part.indexOf("=");
  216. this._l.push(i < 0 ? [qdec(part), ""] :
  217. [qdec(part.slice(0, i)), qdec(part.slice(i + 1))]);
  218. }
  219. }
  220. _upd() {
  221. if (this._url) {
  222. var s = this.toString();
  223. this._url._search = s ? "?" + s : "";
  224. }
  225. }
  226. append(k, v) { this._l.push([String(k), String(v)]); this._upd(); }
  227. delete(k) {
  228. k = String(k);
  229. this._l = this._l.filter(function (p) { return p[0] !== k; });
  230. this._upd();
  231. }
  232. get(k) {
  233. k = String(k);
  234. for (var p of this._l) if (p[0] === k) return p[1];
  235. return null;
  236. }
  237. getAll(k) {
  238. k = String(k);
  239. return this._l.filter(function (p) { return p[0] === k; })
  240. .map(function (p) { return p[1]; });
  241. }
  242. has(k) { return this.get(k) !== null; }
  243. set(k, v) {
  244. k = String(k); v = String(v);
  245. var found = false;
  246. this._l = this._l.filter(function (p) {
  247. if (p[0] !== k) return true;
  248. if (found) return false;
  249. found = true;
  250. p[1] = v;
  251. return true;
  252. });
  253. if (!found) this._l.push([k, v]);
  254. this._upd();
  255. }
  256. sort() {
  257. this._l.sort(function (a, b) {
  258. return a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0;
  259. });
  260. this._upd();
  261. }
  262. forEach(f, t) { for (var p of this._l.slice()) f.call(t, p[1], p[0], this); }
  263. keys() { return this._l.map(function (p) { return p[0]; })[Symbol.iterator](); }
  264. values() { return this._l.map(function (p) { return p[1]; })[Symbol.iterator](); }
  265. entries() { return this._l.map(function (p) { return [p[0], p[1]]; })[Symbol.iterator](); }
  266. [Symbol.iterator]() { return this.entries(); }
  267. get size() { return this._l.length; }
  268. toString() {
  269. return this._l.map(function (p) {
  270. return qenc(p[0]) + "=" + qenc(p[1]);
  271. }).join("&");
  272. }
  273. }
  274. def("URLSearchParams", URLSearchParams);
  275. var URL_RE = /^([a-zA-Z][a-zA-Z0-9+.\-]*:)(\/\/([^\/?#]*))?([^?#]*)(\?[^#]*)?(#.*)?$/;
  276. class URL {
  277. constructor(url, base) {
  278. url = String(url).trim();
  279. if (!/^[a-zA-Z][a-zA-Z0-9+.\-]*:/.test(url)) {
  280. if (base === undefined)
  281. throw new TypeError("Invalid URL: " + url);
  282. base = new URL(String(base)).href;
  283. url = resolve(base, url);
  284. }
  285. this._parse(url);
  286. }
  287. _parse(u) {
  288. var m = URL_RE.exec(u);
  289. if (!m) throw new TypeError("Invalid URL: " + u);
  290. this._protocol = m[1].toLowerCase();
  291. var auth = m[3] || "", at = auth.lastIndexOf("@");
  292. this._user = "";
  293. this._pass = "";
  294. if (at >= 0) {
  295. var ui = auth.slice(0, at), c = ui.indexOf(":");
  296. auth = auth.slice(at + 1);
  297. this._user = c < 0 ? ui : ui.slice(0, c);
  298. this._pass = c < 0 ? "" : ui.slice(c + 1);
  299. }
  300. var pm = /^(\[[^\]]*\]|[^:]*)(?::(\d*))?$/.exec(auth);
  301. this._hostname = (pm ? pm[1] : auth).toLowerCase();
  302. this._port = (pm && pm[2]) || "";
  303. if ((this._protocol === "https:" && this._port === "443") ||
  304. (this._protocol === "http:" && this._port === "80"))
  305. this._port = "";
  306. this._special = m[2] !== undefined;
  307. this._pathname = m[4] || (this._special ? "/" : "");
  308. this._search = m[5] && m[5].length > 1 ? m[5] : "";
  309. this._hash = m[6] && m[6].length > 1 ? m[6] : "";
  310. if (this._sp) this._sp._parse(this._search);
  311. }
  312. get protocol() { return this._protocol; }
  313. set protocol(v) {
  314. v = String(v);
  315. this._protocol = (v.endsWith(":") ? v : v + ":").toLowerCase();
  316. }
  317. get username() { return this._user; }
  318. set username(v) { this._user = String(v); }
  319. get password() { return this._pass; }
  320. set password(v) { this._pass = String(v); }
  321. get hostname() { return this._hostname; }
  322. set hostname(v) { this._hostname = String(v).toLowerCase(); }
  323. get port() { return this._port; }
  324. set port(v) { this._port = String(v); }
  325. get host() { return this._hostname + (this._port ? ":" + this._port : ""); }
  326. set host(v) {
  327. var m = /^(\[[^\]]*\]|[^:]*)(?::(\d*))?$/.exec(String(v));
  328. if (m) { this._hostname = m[1].toLowerCase(); this._port = m[2] || ""; }
  329. }
  330. get pathname() { return this._pathname; }
  331. set pathname(v) {
  332. v = String(v);
  333. this._pathname = this._special && v[0] !== "/" ? "/" + v : v;
  334. }
  335. get search() { return this._search; }
  336. set search(v) {
  337. v = String(v);
  338. if (v && v[0] !== "?") v = "?" + v;
  339. this._search = v === "?" ? "" : v;
  340. if (this._sp) this._sp._parse(this._search);
  341. }
  342. get hash() { return this._hash; }
  343. set hash(v) {
  344. v = String(v);
  345. if (v && v[0] !== "#") v = "#" + v;
  346. this._hash = v === "#" ? "" : v;
  347. }
  348. get origin() {
  349. return this._special && this._protocol !== "file:" ?
  350. this._protocol + "//" + this.host : "null";
  351. }
  352. get href() {
  353. var a = this._user ? this._user +
  354. (this._pass ? ":" + this._pass : "") + "@" : "";
  355. return this._protocol + (this._special ? "//" + a + this.host : "") +
  356. this._pathname + this._search + this._hash;
  357. }
  358. set href(v) { this._parse(String(v)); }
  359. get searchParams() {
  360. if (!this._sp) {
  361. this._sp = new URLSearchParams(this._search);
  362. this._sp._url = this;
  363. }
  364. return this._sp;
  365. }
  366. toString() { return this.href; }
  367. toJSON() { return this.href; }
  368. static canParse(u, b) {
  369. try { new URL(u, b); return true; } catch (e) { return false; }
  370. }
  371. static parse(u, b) {
  372. try { return new URL(u, b); } catch (e) { return null; }
  373. }
  374. static createObjectURL() { return "blob:nebos/" + Date.now(); }
  375. static revokeObjectURL() {}
  376. }
  377. def("URL", URL);
  378. def("webkitURL", URL);
  379. /* ---- Headers / Blob / FormData / Request / Response ---- */
  380. class Headers {
  381. constructor(init) {
  382. this._m = new Map();
  383. if (!init) return;
  384. if (init instanceof Headers)
  385. init.forEach((v, k) => this.append(k, v));
  386. else if (typeof init[Symbol.iterator] === "function")
  387. for (var p of init) this.append(p[0], p[1]);
  388. else
  389. for (var k of Object.keys(init)) this.append(k, init[k]);
  390. }
  391. append(k, v) {
  392. k = String(k).toLowerCase();
  393. var o = this._m.get(k);
  394. this._m.set(k, o !== undefined ? o + ", " + v : String(v));
  395. }
  396. set(k, v) { this._m.set(String(k).toLowerCase(), String(v)); }
  397. get(k) {
  398. var v = this._m.get(String(k).toLowerCase());
  399. return v === undefined ? null : v;
  400. }
  401. has(k) { return this._m.has(String(k).toLowerCase()); }
  402. delete(k) { this._m.delete(String(k).toLowerCase()); }
  403. forEach(f, t) { for (var [k, v] of this._m) f.call(t, v, k, this); }
  404. keys() { return this._m.keys(); }
  405. values() { return this._m.values(); }
  406. entries() { return this._m.entries(); }
  407. [Symbol.iterator]() { return this._m.entries(); }
  408. }
  409. def("Headers", Headers);
  410. function partText(p) {
  411. if (typeof p === "string") return p;
  412. if (p instanceof Blob) return p._s;
  413. if (p instanceof ArrayBuffer || ArrayBuffer.isView(p))
  414. return unutf8(bytesOf(p));
  415. return String(p);
  416. }
  417. class Blob {
  418. constructor(parts, opts) {
  419. this._s = (parts || []).map(partText).join("");
  420. this.type = (opts && opts.type) || "";
  421. }
  422. get size() { return utf8(this._s).length; }
  423. text() { return Promise.resolve(this._s); }
  424. arrayBuffer() { return Promise.resolve(utf8(this._s).buffer); }
  425. slice(a, b, t) { return new Blob([this._s.slice(a, b)], { type: t }); }
  426. }
  427. class File extends Blob {
  428. constructor(parts, name, opts) {
  429. super(parts, opts);
  430. this.name = String(name);
  431. this.lastModified = (opts && opts.lastModified) || Date.now();
  432. }
  433. }
  434. def("Blob", Blob);
  435. def("File", File);
  436. class FormData {
  437. constructor() { this._l = []; }
  438. append(k, v) { this._l.push([String(k), v]); }
  439. set(k, v) { this.delete(k); this.append(k, v); }
  440. get(k) {
  441. for (var p of this._l) if (p[0] === k) return p[1];
  442. return null;
  443. }
  444. getAll(k) {
  445. return this._l.filter(function (p) { return p[0] === k; })
  446. .map(function (p) { return p[1]; });
  447. }
  448. has(k) { return this.get(k) !== null; }
  449. delete(k) { this._l = this._l.filter(function (p) { return p[0] !== k; }); }
  450. forEach(f, t) { for (var p of this._l) f.call(t, p[1], p[0], this); }
  451. entries() { return this._l.map(function (p) { return [p[0], p[1]]; })[Symbol.iterator](); }
  452. keys() { return this._l.map(function (p) { return p[0]; })[Symbol.iterator](); }
  453. values() { return this._l.map(function (p) { return p[1]; })[Symbol.iterator](); }
  454. [Symbol.iterator]() { return this.entries(); }
  455. }
  456. def("FormData", FormData);
  457. class AbortSignal {
  458. constructor() {
  459. this.aborted = false;
  460. this.reason = undefined;
  461. this.onabort = null;
  462. this._l = [];
  463. }
  464. addEventListener(t, f) { if (t === "abort") this._l.push(f); }
  465. removeEventListener(t, f) { this._l = this._l.filter(function (x) { return x !== f; }); }
  466. throwIfAborted() { if (this.aborted) throw this.reason; }
  467. static abort(r) { var c = new AbortController(); c.abort(r); return c.signal; }
  468. static timeout(ms) {
  469. var c = new AbortController();
  470. setTimeout(function () {
  471. c.abort(new DOMException("signal timed out", "TimeoutError"));
  472. }, ms);
  473. return c.signal;
  474. }
  475. }
  476. class AbortController {
  477. constructor() { this.signal = new AbortSignal(); }
  478. abort(r) {
  479. var s = this.signal, ev;
  480. if (s.aborted) return;
  481. s.aborted = true;
  482. s.reason = r === undefined ?
  483. new DOMException("signal is aborted without reason",
  484. "AbortError") : r;
  485. ev = { type: "abort", target: s };
  486. if (typeof s.onabort === "function") s.onabort(ev);
  487. s._l.forEach(function (f) { f(ev); });
  488. }
  489. }
  490. def("AbortSignal", AbortSignal);
  491. def("AbortController", AbortController);
  492. class Request {
  493. constructor(input, init) {
  494. init = init || {};
  495. if (input instanceof Request) {
  496. this.url = input.url;
  497. this.method = input.method;
  498. this.headers = new Headers(input.headers);
  499. this.body = input.body;
  500. this.signal = input.signal;
  501. } else {
  502. this.url = new URL(String(input), g.location.href).href;
  503. this.method = "GET";
  504. this.headers = new Headers();
  505. this.body = null;
  506. this.signal = null;
  507. }
  508. if (init.method) this.method = String(init.method).toUpperCase();
  509. if (init.headers) this.headers = new Headers(init.headers);
  510. if (init.body !== undefined) this.body = init.body;
  511. if (init.signal) this.signal = init.signal;
  512. this.credentials = init.credentials || "same-origin";
  513. this.mode = init.mode || "cors";
  514. }
  515. clone() { return new Request(this); }
  516. }
  517. def("Request", Request);
  518. class Response {
  519. constructor(body, init) {
  520. init = init || {};
  521. this._b = body == null ? "" : partText(body);
  522. this.status = init.status === undefined ? 200 : init.status;
  523. this.statusText = init.statusText || "";
  524. this.headers = init.headers instanceof Headers ? init.headers :
  525. new Headers(init.headers);
  526. this.ok = this.status >= 200 && this.status < 300;
  527. this.url = init.url || "";
  528. this.redirected = !!init.redirected;
  529. this.type = "basic";
  530. this.bodyUsed = false;
  531. this.body = null;
  532. }
  533. text() { this.bodyUsed = true; return Promise.resolve(this._b); }
  534. json() {
  535. this.bodyUsed = true;
  536. try { return Promise.resolve(JSON.parse(this._b)); }
  537. catch (e) { return Promise.reject(e); }
  538. }
  539. blob() {
  540. this.bodyUsed = true;
  541. return Promise.resolve(new Blob([this._b],
  542. { type: this.headers.get("content-type") || "" }));
  543. }
  544. arrayBuffer() { this.bodyUsed = true; return Promise.resolve(utf8(this._b).buffer); }
  545. formData() { return Promise.reject(new TypeError("formData() is not supported")); }
  546. clone() {
  547. return new Response(this._b, { status: this.status,
  548. statusText: this.statusText, url: this.url,
  549. headers: new Headers(this.headers) });
  550. }
  551. static json(data, init) {
  552. var h = new Headers(init && init.headers);
  553. h.set("content-type", "application/json");
  554. return new Response(JSON.stringify(data),
  555. Object.assign({}, init, { headers: h }));
  556. }
  557. static error() { return new Response(null, { status: 0 }); }
  558. static redirect(url, status) {
  559. return new Response(null, { status: status || 302,
  560. headers: { location: String(url) } });
  561. }
  562. }
  563. def("Response", Response);
  564. var STATUS_TEXT = { 200: "OK", 201: "Created", 204: "No Content",
  565. 301: "Moved Permanently", 302: "Found", 303: "See Other",
  566. 304: "Not Modified", 400: "Bad Request", 401: "Unauthorized",
  567. 403: "Forbidden", 404: "Not Found", 409: "Conflict", 410: "Gone",
  568. 500: "Internal Server Error", 502: "Bad Gateway",
  569. 503: "Service Unavailable" };
  570. /* One synchronous request through the browser's fetch helpers. */
  571. function send(method, url, headers, body) {
  572. var ct = headers.get("content-type");
  573. if (body instanceof URLSearchParams) {
  574. if (!ct) ct = "application/x-www-form-urlencoded;charset=UTF-8";
  575. body = body.toString();
  576. } else if (body instanceof FormData) {
  577. var sp = new URLSearchParams();
  578. body.forEach(function (v, k) {
  579. sp.append(k, typeof v === "string" ? v : "");
  580. });
  581. body = sp.toString();
  582. ct = "application/x-www-form-urlencoded;charset=UTF-8";
  583. } else if (body instanceof Blob) {
  584. if (!ct) ct = body.type;
  585. body = body._s;
  586. } else if (body != null && typeof body !== "string") {
  587. body = partText(body);
  588. } else if (typeof body === "string" && !ct) {
  589. ct = "text/plain;charset=UTF-8";
  590. }
  591. if (method === "GET" || method === "HEAD") body = null;
  592. var r = http(method, url, ct || "", body == null ? "" : body);
  593. var h = new Headers();
  594. if (r[1]) h.set("content-type", r[1]);
  595. return { status: r[0], headers: h, url: r[2] || url, text: r[3] };
  596. }
  597. def("fetch", function (input, init) {
  598. return new Promise(function (res, rej) {
  599. var req, out;
  600. try {
  601. req = new Request(input, init);
  602. } catch (e) {
  603. rej(e);
  604. return;
  605. }
  606. if (req.signal && req.signal.aborted) {
  607. rej(req.signal.reason);
  608. return;
  609. }
  610. try {
  611. out = send(req.method, req.url, req.headers, req.body);
  612. } catch (e) {
  613. rej(new TypeError("Failed to fetch"));
  614. return;
  615. }
  616. res(new Response(out.text, { status: out.status,
  617. statusText: STATUS_TEXT[out.status] || "",
  618. headers: out.headers, url: out.url,
  619. redirected: out.url !== req.url }));
  620. });
  621. });
  622. /* XMLHttpRequest on the same synchronous path; events fire asynchronously
  623. * (from a timer) so the caller can finish wiring its handlers. */
  624. class XMLHttpRequest {
  625. constructor() {
  626. this.readyState = 0;
  627. this.status = 0;
  628. this.statusText = "";
  629. this.responseText = "";
  630. this.response = "";
  631. this.responseType = "";
  632. this.responseURL = "";
  633. this.timeout = 0;
  634. this.withCredentials = false;
  635. this._h = new Headers();
  636. this._rh = new Headers();
  637. this._l = {};
  638. this.upload = { addEventListener: function () {} };
  639. }
  640. open(method, url) {
  641. this._m = String(method).toUpperCase();
  642. this._u = new URL(String(url), g.location.href).href;
  643. this.readyState = 1;
  644. }
  645. setRequestHeader(k, v) { this._h.append(k, v); }
  646. getResponseHeader(k) { return this._rh.get(k); }
  647. getAllResponseHeaders() {
  648. var s = "";
  649. this._rh.forEach(function (v, k) { s += k + ": " + v + "\r\n"; });
  650. return s;
  651. }
  652. overrideMimeType() {}
  653. addEventListener(t, f) { (this._l[t] = this._l[t] || []).push(f); }
  654. removeEventListener(t, f) {
  655. if (this._l[t]) this._l[t] = this._l[t].filter(function (x) { return x !== f; });
  656. }
  657. _fire(t) {
  658. var ev = { type: t, target: this, currentTarget: this,
  659. lengthComputable: false, loaded: 0, total: 0 };
  660. if (typeof this["on" + t] === "function") this["on" + t](ev);
  661. (this._l[t] || []).slice().forEach((f) => f.call(this, ev));
  662. }
  663. abort() { this._aborted = true; }
  664. send(body) {
  665. var self = this, out, err = false;
  666. try {
  667. out = send(this._m, this._u, this._h, body);
  668. } catch (e) {
  669. err = true;
  670. }
  671. setTimeout(function () {
  672. if (self._aborted) return;
  673. if (err) {
  674. self.readyState = 4;
  675. self._fire("readystatechange");
  676. self._fire("error");
  677. self._fire("loadend");
  678. return;
  679. }
  680. self.status = out.status;
  681. self.statusText = STATUS_TEXT[out.status] || "";
  682. self._rh = out.headers;
  683. self.responseURL = out.url;
  684. self.responseText = out.text;
  685. if (self.responseType === "json") {
  686. try { self.response = JSON.parse(out.text); }
  687. catch (e) { self.response = null; }
  688. } else {
  689. self.response = out.text;
  690. }
  691. self.readyState = 4;
  692. self._fire("readystatechange");
  693. self._fire("load");
  694. self._fire("loadend");
  695. }, 0);
  696. }
  697. }
  698. XMLHttpRequest.UNSENT = 0;
  699. XMLHttpRequest.OPENED = 1;
  700. XMLHttpRequest.HEADERS_RECEIVED = 2;
  701. XMLHttpRequest.LOADING = 3;
  702. XMLHttpRequest.DONE = 4;
  703. def("XMLHttpRequest", XMLHttpRequest);
  704. /* ---- Web Storage, persisted by the host per origin ---- */
  705. function makeStorage(kind) {
  706. var data = {};
  707. try {
  708. var t = storage(kind);
  709. if (t) data = JSON.parse(t) || {};
  710. } catch (e) {
  711. data = {};
  712. }
  713. function save() { storage(kind, JSON.stringify(data)); }
  714. var api = {
  715. getItem: function (k) {
  716. k = String(k);
  717. return hasOwn.call(data, k) ? data[k] : null;
  718. },
  719. setItem: function (k, v) { data[String(k)] = String(v); save(); },
  720. removeItem: function (k) { delete data[String(k)]; save(); },
  721. clear: function () { data = {}; save(); },
  722. key: function (i) {
  723. var k = Object.keys(data);
  724. return i < k.length ? k[i] : null;
  725. }
  726. };
  727. return new Proxy({}, {
  728. get: function (t, k) {
  729. if (typeof k === "symbol") return undefined;
  730. if (k === "length") return Object.keys(data).length;
  731. if (hasOwn.call(api, k)) return api[k];
  732. return hasOwn.call(data, k) ? data[k] : undefined;
  733. },
  734. set: function (t, k, v) {
  735. if (typeof k === "symbol") return true;
  736. data[k] = String(v);
  737. save();
  738. return true;
  739. },
  740. has: function (t, k) {
  741. return typeof k === "string" &&
  742. (k === "length" || hasOwn.call(api, k) || hasOwn.call(data, k));
  743. },
  744. deleteProperty: function (t, k) {
  745. if (typeof k === "string" && hasOwn.call(data, k)) {
  746. delete data[k];
  747. save();
  748. }
  749. return true;
  750. },
  751. ownKeys: function () { return Object.keys(data); },
  752. getOwnPropertyDescriptor: function (t, k) {
  753. if (typeof k === "string" && hasOwn.call(data, k))
  754. return { value: data[k], writable: true,
  755. enumerable: true, configurable: true };
  756. return undefined;
  757. }
  758. });
  759. }
  760. def("localStorage", makeStorage(0));
  761. def("sessionStorage", makeStorage(1));
  762. /* ---- crypto ---- */
  763. var K256 = [
  764. 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1,
  765. 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
  766. 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
  767. 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  768. 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147,
  769. 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
  770. 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,
  771. 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  772. 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a,
  773. 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
  774. 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2];
  775. function sha256(msg) {
  776. var h = [0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
  777. 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19];
  778. var len = msg.length, n = ((len + 9 + 63) >> 6) << 6;
  779. var b = new Uint8Array(n), w = new Array(64), i, j;
  780. b.set(msg);
  781. b[len] = 0x80;
  782. b[n - 4] = (len * 8) >>> 24;
  783. b[n - 3] = (len * 8) >>> 16 & 255;
  784. b[n - 2] = (len * 8) >>> 8 & 255;
  785. b[n - 1] = (len * 8) & 255;
  786. b[n - 5] = Math.floor(len / 0x20000000) & 255;
  787. function rotr(x, r) { return (x >>> r) | (x << (32 - r)); }
  788. for (i = 0; i < n; i += 64) {
  789. for (j = 0; j < 16; j++)
  790. w[j] = (b[i + 4 * j] << 24) | (b[i + 4 * j + 1] << 16) |
  791. (b[i + 4 * j + 2] << 8) | b[i + 4 * j + 3];
  792. for (j = 16; j < 64; j++) {
  793. var s0 = rotr(w[j - 15], 7) ^ rotr(w[j - 15], 18) ^ (w[j - 15] >>> 3);
  794. var s1 = rotr(w[j - 2], 17) ^ rotr(w[j - 2], 19) ^ (w[j - 2] >>> 10);
  795. w[j] = (w[j - 16] + s0 + w[j - 7] + s1) | 0;
  796. }
  797. var a = h[0], bb = h[1], c = h[2], d = h[3], e = h[4], f = h[5],
  798. gg = h[6], hh = h[7];
  799. for (j = 0; j < 64; j++) {
  800. var S1 = rotr(e, 6) ^ rotr(e, 11) ^ rotr(e, 25);
  801. var ch = (e & f) ^ (~e & gg);
  802. var t1 = (hh + S1 + ch + K256[j] + w[j]) | 0;
  803. var S0 = rotr(a, 2) ^ rotr(a, 13) ^ rotr(a, 22);
  804. var mj = (a & bb) ^ (a & c) ^ (bb & c);
  805. var t2 = (S0 + mj) | 0;
  806. hh = gg; gg = f; f = e; e = (d + t1) | 0;
  807. d = c; c = bb; bb = a; a = (t1 + t2) | 0;
  808. }
  809. h[0] = (h[0] + a) | 0; h[1] = (h[1] + bb) | 0;
  810. h[2] = (h[2] + c) | 0; h[3] = (h[3] + d) | 0;
  811. h[4] = (h[4] + e) | 0; h[5] = (h[5] + f) | 0;
  812. h[6] = (h[6] + gg) | 0; h[7] = (h[7] + hh) | 0;
  813. }
  814. var out = new Uint8Array(32);
  815. for (i = 0; i < 8; i++) {
  816. out[4 * i] = h[i] >>> 24;
  817. out[4 * i + 1] = h[i] >>> 16 & 255;
  818. out[4 * i + 2] = h[i] >>> 8 & 255;
  819. out[4 * i + 3] = h[i] & 255;
  820. }
  821. return out;
  822. }
  823. /* The entropy is weak (Math.random seeded from the clock, stirred with the
  824. * millisecond timer), like the TLS DRBG; fine for PKCE nonces on a hobby
  825. * OS, not for long-term keys. */
  826. var rngState = sha256(utf8(String(Date.now()) + ":" + Math.random() + ":" +
  827. g.performance.now()));
  828. var rngPos = 0;
  829. function randomByte() {
  830. if (rngPos >= 32) {
  831. var seed = new Uint8Array(40);
  832. seed.set(rngState);
  833. var t = g.performance.now() * 1000 + Math.random() * 4294967296;
  834. for (var i = 0; i < 8; i++)
  835. seed[32 + i] = Math.floor(t / Math.pow(256, i)) & 255;
  836. rngState = sha256(seed);
  837. rngPos = 0;
  838. }
  839. return rngState[rngPos++];
  840. }
  841. var subtle = {
  842. digest: function (alg, data) {
  843. var name = typeof alg === "string" ? alg : alg && alg.name;
  844. if (String(name).toUpperCase().replace("-", "") !== "SHA256")
  845. return Promise.reject(new DOMException(
  846. "Unsupported algorithm " + name, "NotSupportedError"));
  847. return Promise.resolve(sha256(bytesOf(data)).buffer);
  848. }
  849. };
  850. def("crypto", {
  851. subtle: subtle,
  852. getRandomValues: function (a) {
  853. var u = new Uint8Array(a.buffer, a.byteOffset, a.byteLength);
  854. for (var i = 0; i < u.length; i++) u[i] = randomByte();
  855. return a;
  856. },
  857. randomUUID: function () {
  858. var b = new Uint8Array(16), i, s = "";
  859. this.getRandomValues(b);
  860. b[6] = (b[6] & 0x0f) | 0x40;
  861. b[8] = (b[8] & 0x3f) | 0x80;
  862. for (i = 0; i < 16; i++) {
  863. s += (b[i] + 0x100).toString(16).slice(1);
  864. if (i === 3 || i === 5 || i === 7 || i === 9) s += "-";
  865. }
  866. return s;
  867. }
  868. });
  869. /* ---- observers (no layout notifications yet: inert) ---- */
  870. class MutationObserver {
  871. constructor(cb) { this._cb = cb; }
  872. observe() {}
  873. disconnect() {}
  874. takeRecords() { return []; }
  875. }
  876. class ResizeObserver {
  877. constructor(cb) { this._cb = cb; }
  878. observe() {}
  879. unobserve() {}
  880. disconnect() {}
  881. }
  882. /* Without scroll geometry every observed target is reported visible, once
  883. * per observe(): lazy loaders and "becomes visible" components (heise's
  884. * a-img) then load their content. Capped per page against infinite-scroll
  885. * sentinels that re-observe forever. */
  886. var ioBudget = 400;
  887. /* One timer drains every deferred callback: a page with hundreds of lazy
  888. * images would otherwise overflow the browser's timer table. */
  889. var laterQ = [], laterArmed = false;
  890. function later(f) {
  891. laterQ.push(f);
  892. if (laterArmed) return;
  893. laterArmed = true;
  894. setTimeout(function () {
  895. var q = laterQ, i;
  896. laterQ = [];
  897. laterArmed = false;
  898. for (i = 0; i < q.length; i++)
  899. try { q[i](); } catch (e) { con.error(e); }
  900. }, 0);
  901. }
  902. class IntersectionObserver {
  903. constructor(cb, opts) {
  904. this._cb = cb;
  905. this._t = [];
  906. this.root = (opts && opts.root) || null;
  907. this.rootMargin = (opts && opts.rootMargin) || "0px";
  908. this.thresholds = [0];
  909. }
  910. observe(t) {
  911. var self = this;
  912. if (!t || this._t.indexOf(t) >= 0 || ioBudget <= 0) return;
  913. ioBudget--;
  914. this._t.push(t);
  915. later(function () {
  916. if (self._t.indexOf(t) < 0) return;
  917. var r = t.getBoundingClientRect();
  918. self._cb([{ target: t, isIntersecting: true,
  919. intersectionRatio: 1, boundingClientRect: r,
  920. intersectionRect: r, rootBounds: null,
  921. time: g.performance.now() }], self);
  922. });
  923. }
  924. unobserve(t) { this._t = this._t.filter(function (x) { return x !== t; }); }
  925. disconnect() { this._t = []; }
  926. takeRecords() { return []; }
  927. }
  928. def("MutationObserver", MutationObserver);
  929. def("ResizeObserver", ResizeObserver);
  930. def("IntersectionObserver", IntersectionObserver);
  931. /* Present with isIntersecting/intersectionRatio on the prototype so the
  932. * common IntersectionObserver polyfill (bundled by heise) keeps ours. */
  933. class IntersectionObserverEntry {
  934. constructor(init) { Object.assign(this, init); }
  935. get isIntersecting() { return !!this._i; }
  936. get intersectionRatio() { return this._r || 0; }
  937. }
  938. def("IntersectionObserverEntry", IntersectionObserverEntry);
  939. /* ---- DOM classes: instanceof works on the node wrappers ---- */
  940. function domClass(name, test) {
  941. var C = function () { throw new TypeError("Illegal constructor"); };
  942. Object.defineProperty(C, "name", { value: name });
  943. Object.defineProperty(C, Symbol.hasInstance, {
  944. value: function (o) {
  945. return o != null && typeof o === "object" &&
  946. typeof o.nodeType === "number" && test(o);
  947. }
  948. });
  949. C.prototype = nodeproto;
  950. def(name, C);
  951. return C;
  952. }
  953. function tag(t) { return function (o) { return o.nodeType === 1 && o.tagName === t; }; }
  954. domClass("EventTarget", function (o) { return true; });
  955. domClass("Node", function (o) { return true; });
  956. domClass("Element", function (o) { return o.nodeType === 1; });
  957. domClass("HTMLElement", function (o) { return o.nodeType === 1; });
  958. domClass("SVGElement", function (o) { return o.nodeType === 1 && o.namespaceURI === "http://www.w3.org/2000/svg"; });
  959. domClass("CharacterData", function (o) { return o.nodeType === 3 || o.nodeType === 8; });
  960. domClass("Text", function (o) { return o.nodeType === 3; });
  961. domClass("Comment", function (o) { return o.nodeType === 8; });
  962. domClass("DocumentFragment", function (o) { return o.nodeType === 11; });
  963. domClass("Document", function (o) { return o.nodeType === 9; });
  964. domClass("DocumentType", function (o) { return o.nodeType === 10; });
  965. domClass("Attr", function (o) { return o.nodeType === 2; });
  966. domClass("HTMLDocument", function (o) { return o.nodeType === 9; });
  967. domClass("HTMLInputElement", tag("INPUT"));
  968. domClass("HTMLTextAreaElement", tag("TEXTAREA"));
  969. domClass("HTMLSelectElement", tag("SELECT"));
  970. domClass("HTMLOptionElement", tag("OPTION"));
  971. domClass("HTMLButtonElement", tag("BUTTON"));
  972. domClass("HTMLFormElement", tag("FORM"));
  973. domClass("HTMLAnchorElement", tag("A"));
  974. domClass("HTMLImageElement", tag("IMG"));
  975. domClass("HTMLTemplateElement", tag("TEMPLATE"));
  976. domClass("HTMLIFrameElement", tag("IFRAME"));
  977. domClass("HTMLLinkElement", tag("LINK"));
  978. domClass("HTMLScriptElement", tag("SCRIPT"));
  979. domClass("HTMLStyleElement", tag("STYLE"));
  980. domClass("HTMLCanvasElement", tag("CANVAS"));
  981. def("Window", function Window() {});
  982. /* ---- Custom Elements ----
  983. * A registered class adopts an existing element: HTMLElement, used as the
  984. * base constructor, returns the element being upgraded (or a fresh one for
  985. * `new MyElement()`) with its prototype switched to the subclass, the way
  986. * the custom-elements polyfills do it. New elements are upgraded when
  987. * created by script and by a scan after DOM changes (js_browser.c calls
  988. * __nb_ce_scan from its tick); connectedCallback runs once the element is
  989. * in the document. attributeChangedCallback fires for the observed
  990. * attributes present at upgrade, not for later changes. */
  991. var ceRegistry = new Map(), ceNames = new Map(), ceWaiters = new Map();
  992. var cePending = [];
  993. var createEl = g.document.createElement;
  994. function HTMLElementCtor() {
  995. var el = cePending.length ? cePending[cePending.length - 1] : null;
  996. if (!el) {
  997. var name = ceNames.get(new.target);
  998. if (!name) throw new TypeError("Illegal constructor");
  999. el = createEl.call(g.document, name);
  1000. }
  1001. Object.setPrototypeOf(el, new.target.prototype);
  1002. return el;
  1003. }
  1004. HTMLElementCtor.prototype = nodeproto;
  1005. Object.defineProperty(HTMLElementCtor, "name", { value: "HTMLElement" });
  1006. Object.defineProperty(HTMLElementCtor, Symbol.hasInstance, {
  1007. value: function (o) {
  1008. if (this !== HTMLElementCtor)
  1009. return Function.prototype[Symbol.hasInstance].call(this, o);
  1010. return o != null && typeof o === "object" && o.nodeType === 1;
  1011. }
  1012. });
  1013. def("HTMLElement", HTMLElementCtor);
  1014. function ceUpgrade(el) {
  1015. var C, obs, i;
  1016. if (!el || el.nodeType !== 1 || el.__nbce) return;
  1017. C = ceRegistry.get(el.localName);
  1018. if (!C) return;
  1019. el.__nbce = 1;
  1020. cePending.push(el);
  1021. try { new C(); } catch (e) { con.error(e); }
  1022. cePending.pop();
  1023. obs = C.observedAttributes;
  1024. if (obs && typeof el.attributeChangedCallback === "function")
  1025. for (i = 0; i < obs.length; i++)
  1026. if (el.hasAttribute(obs[i]))
  1027. try { el.attributeChangedCallback(obs[i], null, el.getAttribute(obs[i])); }
  1028. catch (e) { con.error(e); }
  1029. ceConnect(el);
  1030. }
  1031. function ceConnect(el) {
  1032. if (el.__nbceConn || !el.isConnected) return;
  1033. el.__nbceConn = 1;
  1034. if (typeof el.connectedCallback === "function") {
  1035. try {
  1036. var r = el.connectedCallback();
  1037. if (r && typeof r.then === "function")
  1038. r.then(null, function (e) { con.error(e); });
  1039. } catch (e) { con.error(e); }
  1040. }
  1041. }
  1042. function ceScan() {
  1043. ceRegistry.forEach(function (C, name) {
  1044. var list = g.document.querySelectorAll(name), i;
  1045. for (i = 0; i < list.length; i++) {
  1046. if (!list[i].__nbce) ceUpgrade(list[i]);
  1047. else ceConnect(list[i]);
  1048. }
  1049. });
  1050. }
  1051. def("__nb_ce_scan", function () { if (ceRegistry.size) ceScan(); });
  1052. def("customElements", {
  1053. define: function (name, C) {
  1054. name = String(name).toLowerCase();
  1055. if (ceRegistry.has(name))
  1056. throw new DOMException("'" + name + "' is already defined", "NotSupportedError");
  1057. ceRegistry.set(name, C);
  1058. ceNames.set(C, name);
  1059. ceScan();
  1060. var w = ceWaiters.get(name);
  1061. if (w) { w.forEach(function (f) { f(C); }); ceWaiters.delete(name); }
  1062. },
  1063. get: function (name) { return ceRegistry.get(String(name).toLowerCase()); },
  1064. getName: function (C) { return ceNames.get(C) || null; },
  1065. whenDefined: function (name) {
  1066. name = String(name).toLowerCase();
  1067. if (ceRegistry.has(name)) return Promise.resolve(ceRegistry.get(name));
  1068. return new Promise(function (res) {
  1069. if (!ceWaiters.has(name)) ceWaiters.set(name, []);
  1070. ceWaiters.get(name).push(res);
  1071. });
  1072. },
  1073. upgrade: function (root) {
  1074. ceUpgrade(root);
  1075. if (root && root.querySelectorAll) ceScan();
  1076. }
  1077. });
  1078. g.document.createElement = function (name, opts) {
  1079. var el = createEl.call(g.document, name);
  1080. var n = String(name).toLowerCase();
  1081. if (ceRegistry.has(n)) ceUpgrade(el);
  1082. if (n === "img") imgLoadShim(el);
  1083. return el;
  1084. };
  1085. /* Script-created images never really load in the page's DOM; fire "load"
  1086. * once a src is set so preload-then-swap code (load a detached <img>, then
  1087. * copy its URL) proceeds. The visible <img> is loaded by the browser. */
  1088. function imgLoadShim(el) {
  1089. later(function () {
  1090. if (el.getAttribute("src"))
  1091. el.dispatchEvent(new Event("load"));
  1092. });
  1093. }
  1094. def("Image", function Image(w, h) {
  1095. var el = g.document.createElement("img");
  1096. if (w !== undefined) el.setAttribute("width", String(w));
  1097. if (h !== undefined) el.setAttribute("height", String(h));
  1098. return el;
  1099. });
  1100. def("ShadowRoot", function ShadowRoot() {});
  1101. var NodeConst = g.Node;
  1102. [["ELEMENT_NODE", 1], ["ATTRIBUTE_NODE", 2], ["TEXT_NODE", 3],
  1103. ["CDATA_SECTION_NODE", 4], ["COMMENT_NODE", 8], ["DOCUMENT_NODE", 9],
  1104. ["DOCUMENT_TYPE_NODE", 10], ["DOCUMENT_FRAGMENT_NODE", 11]]
  1105. .forEach(function (p) { NodeConst[p[0]] = p[1]; });
  1106. /* Methods every node wrapper inherits (the C has-hook answers the rest). */
  1107. var doc = g.document;
  1108. function toNode(n) {
  1109. return typeof n === "object" && n !== null ? n : doc.createTextNode(String(n));
  1110. }
  1111. Object.assign(nodeproto, {
  1112. setAttributeNS: function (ns, n, v) {
  1113. this.setAttribute(String(n).replace(/^[^:]*:/, ""), v);
  1114. },
  1115. getAttributeNS: function (ns, n) { return this.getAttribute(n); },
  1116. removeAttributeNS: function (ns, n) { this.removeAttribute(n); },
  1117. hasAttributeNS: function (ns, n) { return this.hasAttribute(n); },
  1118. toggleAttribute: function (n, force) {
  1119. var on = force === undefined ? !this.hasAttribute(n) : !!force;
  1120. if (on) this.setAttribute(n, "");
  1121. else this.removeAttribute(n);
  1122. return on;
  1123. },
  1124. getAttributeNames: function () {
  1125. var a = this.attributes, out = [], i;
  1126. for (i = 0; a && i < a.length; i++) out.push(a[i].name);
  1127. return out;
  1128. },
  1129. closest: function (sel) {
  1130. var e = this;
  1131. while (e && e.nodeType === 1) {
  1132. if (e.matches(sel)) return e;
  1133. e = e.parentNode;
  1134. }
  1135. return null;
  1136. },
  1137. append: function () {
  1138. for (var i = 0; i < arguments.length; i++)
  1139. this.appendChild(toNode(arguments[i]));
  1140. },
  1141. prepend: function () {
  1142. var first = this.firstChild;
  1143. for (var i = 0; i < arguments.length; i++)
  1144. this.insertBefore(toNode(arguments[i]), first);
  1145. },
  1146. before: function () {
  1147. var p = this.parentNode;
  1148. if (!p) return;
  1149. for (var i = 0; i < arguments.length; i++)
  1150. p.insertBefore(toNode(arguments[i]), this);
  1151. },
  1152. after: function () {
  1153. var p = this.parentNode, next = this.nextSibling;
  1154. if (!p) return;
  1155. for (var i = 0; i < arguments.length; i++)
  1156. p.insertBefore(toNode(arguments[i]), next);
  1157. },
  1158. replaceWith: function () {
  1159. var p = this.parentNode;
  1160. if (!p) return;
  1161. this.before.apply(this, arguments);
  1162. p.removeChild(this);
  1163. },
  1164. replaceChildren: function () {
  1165. while (this.firstChild) this.removeChild(this.firstChild);
  1166. this.append.apply(this, arguments);
  1167. },
  1168. insertAdjacentElement: function (pos, el) {
  1169. pos = String(pos).toLowerCase();
  1170. if (pos === "beforebegin") this.before(el);
  1171. else if (pos === "afterbegin") this.prepend(el);
  1172. else if (pos === "beforeend") this.appendChild(el);
  1173. else if (pos === "afterend") this.after(el);
  1174. return el;
  1175. },
  1176. insertAdjacentText: function (pos, text) {
  1177. this.insertAdjacentElement(pos, doc.createTextNode(String(text)));
  1178. },
  1179. insertAdjacentHTML: function (pos, html) {
  1180. var tmp = doc.createElement("div"), nodes = [], c;
  1181. tmp.innerHTML = html;
  1182. for (c = tmp.firstChild; c; c = c.nextSibling) nodes.push(c);
  1183. pos = String(pos).toLowerCase();
  1184. if (pos === "afterbegin") nodes.reverse();
  1185. for (c of nodes) {
  1186. tmp.removeChild(c);
  1187. this.insertAdjacentElement(pos, c);
  1188. }
  1189. },
  1190. getRootNode: function () { return doc; },
  1191. scrollIntoView: function () {},
  1192. scrollTo: function () {},
  1193. scrollBy: function () {},
  1194. animate: function () {
  1195. return { finished: Promise.resolve(), cancel: function () {},
  1196. play: function () {}, pause: function () {},
  1197. onfinish: null };
  1198. },
  1199. attachShadow: function () { return this; },
  1200. compareDocumentPosition: function (other) {
  1201. if (this === other) return 0;
  1202. if (this.contains(other)) return 20;
  1203. if (other.contains && other.contains(this)) return 10;
  1204. return 4;
  1205. },
  1206. isSameNode: function (o) { return this === o; },
  1207. normalize: function () {}
  1208. });
  1209. /* ---- document extras ---- */
  1210. doc.createElementNS = function (ns, name) {
  1211. var el = doc.createElement(String(name).replace(/^[^:]*:/, ""));
  1212. if (ns === "http://www.w3.org/2000/svg")
  1213. el.setAttribute("xmlns", ns);
  1214. return el;
  1215. };
  1216. doc.createRange = function () {
  1217. return {
  1218. createContextualFragment: function (html) {
  1219. var tmp = doc.createElement("div"),
  1220. frag = doc.createDocumentFragment();
  1221. tmp.innerHTML = html;
  1222. while (tmp.firstChild) frag.appendChild(tmp.firstChild);
  1223. return frag;
  1224. },
  1225. selectNodeContents: function () {},
  1226. setStart: function () {}, setEnd: function () {},
  1227. getBoundingClientRect: function () {
  1228. return { x: 0, y: 0, top: 0, left: 0, right: 0, bottom: 0,
  1229. width: 0, height: 0 };
  1230. }
  1231. };
  1232. };
  1233. doc.createTreeWalker = undefined;
  1234. doc.hasFocus = function () { return true; };
  1235. doc.getElementsByName = function (n) {
  1236. return doc.querySelectorAll("[name=\"" + String(n).replace(/"/g, "\\\"") + "\"]");
  1237. };
  1238. doc.hidden = false;
  1239. doc.visibilityState = "visible";
  1240. doc.characterSet = "UTF-8";
  1241. doc.charset = "UTF-8";
  1242. doc.contentType = "text/html";
  1243. doc.referrer = "";
  1244. doc.dir = "";
  1245. doc.fonts = { ready: Promise.resolve(), add: function () {},
  1246. load: function () { return Promise.resolve([]); },
  1247. check: function () { return true; },
  1248. addEventListener: function () {} };
  1249. doc.implementation = {
  1250. hasFeature: function () { return true; },
  1251. createHTMLDocument: function () { return doc; }
  1252. };
  1253. Object.defineProperty(doc, "activeElement", {
  1254. get: function () { return doc.body; }, configurable: true });
  1255. Object.defineProperty(doc, "currentScript", {
  1256. get: function () { return null; }, configurable: true });
  1257. Object.defineProperty(doc, "defaultView", {
  1258. get: function () { return g; }, configurable: true });
  1259. var nav = g.navigator;
  1260. nav.languages = [nav.language];
  1261. nav.onLine = true;
  1262. nav.cookieEnabled = true;
  1263. nav.maxTouchPoints = 0;
  1264. nav.hardwareConcurrency = 1;
  1265. nav.vendor = "";
  1266. nav.clipboard = {
  1267. writeText: function () { return Promise.resolve(); },
  1268. readText: function () { return Promise.resolve(""); }
  1269. };
  1270. nav.sendBeacon = function () { return true; };
  1271. g.history.state = null;
  1272. g.screen = { width: g.innerWidth, height: g.innerHeight,
  1273. availWidth: g.innerWidth, availHeight: g.innerHeight,
  1274. colorDepth: 16, pixelDepth: 16 };
  1275. g.outerWidth = g.innerWidth;
  1276. g.outerHeight = g.innerHeight;
  1277. g.name = "";
  1278. g.closed = false;
  1279. g.open = function () { return null; };
  1280. g.close = function () {};
  1281. g.focus = function () {};
  1282. g.blur = function () {};
  1283. g.print = function () {};
  1284. g.postMessage = function () {};
  1285. g.confirm = function () { return true; };
  1286. g.prompt = function () { return null; };
  1287. })(globalThis);