diff --git a/build-v2.js b/build-v2.js new file mode 100755 index 0000000..9344698 --- /dev/null +++ b/build-v2.js @@ -0,0 +1,27 @@ +#! node + +const { execSync } = require("child_process"); +const fs = require('fs'); + +function embedInlines(src, dest) { + try { + let data = fs.readFileSync(src, 'utf8'); + data = data.replace(/src="\/?(.*)" inline>/g, (g, h) => { + return `>` + execSync(`terser ${h}`) + }) + data = data.replace(//g, (g, h) => { + console.log(h); + return `` + }) + console.log(data); + fs.writeFileSync(dest,data) + } catch (err) { + console.error(err); + } +} + +process.chdir("docs"); +embedInlines('index.html', 'index.min.html') + +process.chdir("render"); +embedInlines('recipe.html', 'recipe.min.html') diff --git a/docs/bitty.js b/docs/bitty.js index 3923e54..18e8216 100644 --- a/docs/bitty.js +++ b/docs/bitty.js @@ -161,7 +161,7 @@ async function testCompression(rawData) { async function compressData(data, encoding = GZIP_MARKER, callback) { console.debug("Compressing with", encoding) if (encoding == GZIP_MARKER) { - return import("/js/gzip/pako.js").then((module) => { + return import("/js/gzip/pako.min.js").then((module) => { return pako.deflate(data, {level:"9"}); }); } else if (encoding == BROT_MARKER) { @@ -188,9 +188,36 @@ function byteArrayToString(bytes) { return String.fromCharCode.apply(null, new Uint8Array(bytes)); } +function browserDecompressData(data) { + + const cs = new DecompressionStream("gzip"); + const writer = cs.writable.getWriter(); + writer.write(data); + writer.close(); + + return new Response(cs.readable).arrayBuffer().then(function (arrayBuffer) { + return new TextDecoder().decode(arrayBuffer); + }); + + // const stream = new Response(data).body.pipeThrough(new DecompressionStream('gzip')); + // return (new Response(stream).arrayBuffer()); + + + // var blob = new Blob([data], {type: "octet/stream"}); + // const ds = new DecompressionStream("gzip"); + // const decompressedStream = blob.stream().pipeThrough(ds); + // data = await new Response(decompressedStream).arrayBuffer(); + // console.log(blob, data); + + // console.log(blob, data); + + // return data +} + async function decompressData(data, encoding, callback) { if (encoding == GZIP_MARKER) { - return import("/js/gzip/pako.js").then((module) => { + // return await browserDecompressData(data); + return import("/js/gzip/pako.min.js").then((module) => { let byteArray = pako.inflate(data); return byteArray; }); diff --git a/docs/index.html b/docs/index.html index a2ee847..0fcd8a1 100644 --- a/docs/index.html +++ b/docs/index.html @@ -4,15 +4,15 @@ - - + + - - - + + + diff --git a/docs/index.js b/docs/index.js index 00e29d9..15e84e3 100644 --- a/docs/index.js +++ b/docs/index.js @@ -1,4 +1,4 @@ - import * as bitty from './bitty.js'; + import * as bitty from '/bitty.js'; window.bitty = bitty; @@ -64,7 +64,7 @@ } const renderers = { - "application/ld+json": {script:"recipe"}, + "application/ld+json": {script:"/render/recipe.min.html"}, "text/canvas+javascript": {script:"canvas"}, "text/javascript": {script:"script"}, "application/bitsy": {script:"/render/bitsy.html", sandbox:"bitsy"}, @@ -404,13 +404,15 @@ let src = window.scriptDomain ?? location.origin; src += "/render"; + let sandbox = params.renderer?.sandbox; + if (params.script.endsWith(".html")) { src = params.script; + if (!sandbox) sandbox = "none"; } - let sandbox = params.renderer?.sandbox; - // if (sandbox == "none") { // passthrough - // } else - if (sandbox == "hash") { // Generate sandbox based off of body hash + + if (sandbox == "none") { // passthrough + } else if (sandbox == "hash") { // Generate sandbox based off of body hash let hash = await bitty.hashString(params.body); src = src.replace("https://", "https://script-" + hash + "."); } else if (sandbox) { // Use named sandbox diff --git a/docs/index.min.html b/docs/index.min.html new file mode 100644 index 0000000..9d99352 --- /dev/null +++ b/docs/index.min.html @@ -0,0 +1,24 @@ + + +
+ + + + + + + + + + + + + + + + + + diff --git a/docs/render/color.js b/docs/render/color.js index 777f0f2..2d866a4 100644 --- a/docs/render/color.js +++ b/docs/render/color.js @@ -44,7 +44,7 @@ function edit() { document.body.classList.toggle("edit"); } function render() { - let colors = params.body.substring(2).split(";"); + let colors = decodeURIComponent(params.body.substring(2)).split(";"); document.body.style.backgroundColor = colors[0]; document.body.style.color = colors[1]; currentColor = colors[0]; diff --git a/docs/render/recipe.html b/docs/render/recipe.html new file mode 100644 index 0000000..f86ad46 --- /dev/null +++ b/docs/render/recipe.html @@ -0,0 +1,43 @@ + + + + + diff --git a/docs/render/recipe.js b/docs/render/recipe.js index a352cc6..8395685 100644 --- a/docs/render/recipe.js +++ b/docs/render/recipe.js @@ -179,6 +179,26 @@ const replacements = { } let lastNoun = undefined; + +window.addEventListener("click", (e) => { + let target = e.target; + + if (target.classList.contains("noun")) { + let els = document.querySelectorAll("#" + e.target.id); + + let isIngredient = target.closest(".ingredients"); + for (const noun of els) { + noun.classList.add("hovered"); + if (noun.closest(".ingredients") != isIngredient) { + noun.scrollIntoView({behavior:"smooth", block: "center"}) + console.log("focusing", noun) + if (noun == lastNoun) break; + } + } + lastNoun = target; + } +}) + window.addEventListener("mouseover", (e) => { let target = e.target; @@ -190,11 +210,11 @@ window.addEventListener("mouseover", (e) => { noun.classList.add("hovered"); noun.closest(".substep")?.classList.add("hovered") noun.closest(".ingredient")?.classList.add("hovered") - if (noun.closest(".ingredients") != isIngredient) { - noun.scrollIntoView({behavior:"smooth", block: "center"}) - console.log("focusing", noun) - if (noun == lastNoun) break; - } + // if (noun.closest(".ingredients") != isIngredient) { + // noun.scrollIntoView({behavior:"smooth", block: "center"}) + // console.log("focusing", noun) + // if (noun == lastNoun) break; + // } } lastNoun = target; } @@ -400,6 +420,7 @@ function faviconForTitle(title) { } return undefined; } + function render() { try { let data = JSON.parse(window.params.body); @@ -414,7 +435,8 @@ function render() { console.error("Data", e, {e, body: window.params.body}); return; } - document.head.appendChild(el("base", {target: "_blank"})); + // console.log("head", document.head, .el, el("base", {target: "_blank"})) + document.head.appendChild(m("base", {target: "_blank"})); delete document.documentElement.style.display; document.body.childNodes.forEach((c) => document.body.removeChild(c)) @@ -597,7 +619,7 @@ function render() { let originalURL = json.mainEntityOfPage?.["@id"] ?? ((json.mainEntityOfPage == true) ? false : json.mainEntityOfPage) ?? json.url; let hostname = originalURL ? new URL(originalURL).hostname.replace("www.","") : "" - let qrImage = QRCodeURL(params.originalURL, {margin:0}); + let qrImage = undefined // QRCodeURL(params.originalURL, {margin:0}); let publisherImage = json.publisher?.image ?.[0]?.url ?? json.publisher ?.logo ?.url; document.body.appendChild( @@ -696,16 +718,16 @@ function keepAwake() { } -var path = window.script.substring(0, window.script.lastIndexOf(".")); -var cssURL = path + ".css"; -loadScript(path + '/../../js/qrious.min.js', null, "").then(() => { - console.log("qrious loaded", params.originalURL.length); - var qr = new QRious({ - element: document.getElementById("qr"), - background: 'transparent', - foreground: 'currentColor', - size: 512, - value: params.originalUrl.substring(0), - }); -}) -loadSyle(cssURL).then(render); \ No newline at end of file +// var path = window.script.substring(0, window.script.lastIndexOf(".")); +// var cssURL = path + ".css"; +// loadScript(path + '/../../js/qrious.min.js', null, "").then(() => { +// console.log("qrious loaded", params.originalURL.length); +// var qr = new QRious({ +// element: document.getElementById("qr"), +// background: 'transparent', +// foreground: 'currentColor', +// size: 512, +// value: params.originalUrl.substring(0), +// }); +// }) +// loadSyle(cssURL).then(render); \ No newline at end of file diff --git a/docs/render/recipe.min.html b/docs/render/recipe.min.html new file mode 100644 index 0000000..2303023 --- /dev/null +++ b/docs/render/recipe.min.html @@ -0,0 +1,45 @@ + + + + +