MCPcopy Create free account
hub / github.com/arfct/itty-bitty / renderContentWithScript

Function renderContentWithScript

docs/index.js:425–486  ·  view source on GitHub ↗
(params)

Source from the content-addressed store, hash-verified

423
424 const SCRIPT_LOADER = `<!doctype html><meta charset=utf-8><script src="${location.origin}/render.js"></script>`
425 async function renderContentWithScript(params) {
426
427 params.script = params.renderer.script;
428 params.originalURL = location.href;
429 if (params.script.indexOf("/") == -1) params.script = location.origin + '/render/' + params.script + '.js'
430
431 if (params.overwrite) {
432
433 if (params.script.endsWith(".html")) {
434 fetch(params.script, /*, options */)
435 .then((response) => response.text())
436 .then((html) => {
437 console.log("html", html)
438 document.documentElement.innerHTML = html;
439 })
440 .catch((error) => {
441 console.warn(error);
442 });
443 } else {
444 // Overwrite the current page with the script. Dangerous, but required for browsers on apple watch to scroll.
445 let scriptEl = document.createElement("script")
446 scriptEl.src = "/render.js"
447 scriptEl.addEventListener('load', function(e) {
448 console.log("Loaded script", scriptEl.src);
449 renderScriptContent(params, "*");
450 });
451 document.head.appendChild(scriptEl);
452 }
453 } else {
454 // Render in an iframe, either via sandboxed subdomain or a data url (disables storage APIs)
455 iframe.onload = (() => {
456 iframe.contentWindow.postMessage(params, "*");
457 delete iframe.onload
458 iframe.contentWindow.focus();
459 // showLoader(false)
460 });
461 // writeDocContent(iframe.contentWindow.document, SCRIPT_LOADER)
462 // iframe.srcdoc = SCRIPT_LOADER;
463
464 let src = window.scriptDomain ?? location.origin;
465 src += "/render";
466
467 let sandbox = params.renderer?.sandbox;
468
469 if (params.script.endsWith(".html")) {
470 src = params.script;
471 if (!sandbox) sandbox = "none";
472 }
473
474 if (sandbox == "none") { // passthrough
475 } else if (sandbox == "hash") { // Generate sandbox based off of body hash
476 let hash = await bitty.hashString(params.body);
477 src = src.replace("https://", "https://script-" + hash + ".");
478 } else if (sandbox) { // Use named sandbox
479 src = src.replace("https://", "https://" + sandbox + ".");
480 } else { // Render using data url (storage disabled)
481 src = "data:text/html," + SCRIPT_LOADER;
482 }

Callers 1

renderContentFunction · 0.85

Calls 1

renderScriptContentFunction · 0.85

Tested by

no test coverage detected