1
This commit is contained in:
1 parent
e45899a72c
commit
4795476546
51 files changed
+6845
-3
No files matched your search
@@ -0,0 +1,2 @@
|
||||
node_modules
|
||||
package-lock.json
|
||||
@@ -0,0 +1,65 @@
|
||||
# [HTMLifier](https://sheeptester.github.io/htmlifier/) for Node
|
||||
|
||||
Package a Scratch project inside an HTML file with no optimisations.
|
||||
|
||||
## Installation
|
||||
|
||||
```sh
|
||||
$ npm install @sheeptester/htmlifier
|
||||
```
|
||||
|
||||
## Example usage
|
||||
|
||||
The following example HTMLifies [Scratch 3.0 is
|
||||
here!](https://scratch.mit.edu/projects/276660763/) and writes the result to a
|
||||
file named `index.html`.
|
||||
|
||||
```js
|
||||
import fs from 'fs/promises'
|
||||
import Htmlifier from '@sheeptester/htmlifier'
|
||||
|
||||
async function main () {
|
||||
const html = await new Htmlifier()
|
||||
.htmlify({ type: 'id', id: '276660763' })
|
||||
.then(blob => blob.text())
|
||||
await fs.writeFile('./index.html', html)
|
||||
}
|
||||
|
||||
main()
|
||||
```
|
||||
|
||||
Because [`node-fetch`](https://www.npmjs.com/package/node-fetch) is strictly an
|
||||
ES module, this package is also an ES module. If you're using a CommonJS module,
|
||||
you can use [`esm`](https://www.npmjs.com/package/esm) to import this package.
|
||||
|
||||
```js
|
||||
require = require('esm')(module)
|
||||
const Htmlifier = require('@sheeptester/htmlifier')
|
||||
```
|
||||
|
||||
The HTMLifier was primarily written for Deno and the web, so it uses
|
||||
[`Blob`s](https://developer.mozilla.org/en-US/docs/Web/API/Blob) and
|
||||
[`File`s](https://developer.mozilla.org/en-US/docs/Web/API/File) to pass binary
|
||||
data around. You can use
|
||||
[`fetch-blob`](https://www.npmjs.com/package/fetch-blob) to create `Blob`s and
|
||||
`File`s for the HTMLifier.
|
||||
|
||||
```js
|
||||
import Htmlifier from '@sheeptester/htmlifier'
|
||||
import { fileFrom } from 'fetch-blob/from.js'
|
||||
|
||||
async function main () {
|
||||
const html = await new Htmlifier()
|
||||
.htmlify({ type: 'file', file: await fileFrom('./project.sb3') })
|
||||
.then(blob => blob.text())
|
||||
// ...
|
||||
}
|
||||
|
||||
main()
|
||||
```
|
||||
|
||||
## Documentation
|
||||
|
||||
[Auto-generated
|
||||
documentation](https://doc.deno.land/https/github.com/SheepTester/htmlifier/raw/v1.0.3/src/htmlifier.ts)
|
||||
is available courtesy of deno doc.
|
||||
@@ -0,0 +1 @@
|
||||
import fetch from"node-fetch";import Blob from"fetch-blob";class FileReader{addEventListener(_,callback){this.callback=callback}async readAsDataURL(blob){this.result=`data:${blob.type};base64,${Buffer.from(await blob.arrayBuffer()).toString("base64")}`,this.callback()}}var e3,t6,n3,r14="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:global,o9=e3={};function i(){throw Error("setTimeout has not been defined")}function u2(){throw Error("clearTimeout has not been defined")}function c2(e1){if(t6===setTimeout)return setTimeout(e1,0);if((t6===i||!t6)&&setTimeout)return t6=setTimeout,setTimeout(e1,0);try{return t6(e1,0)}catch(n1){try{return t6.call(null,e1,0)}catch(n2){return t6.call(this||r14,e1,0)}}}!function(){try{t6="function"==typeof setTimeout?setTimeout:i}catch(e1){t6=i}try{n3="function"==typeof clearTimeout?clearTimeout:u2}catch(e1){n3=u2}}();var l2,s2=[],f9=!1,a18=-1;function h4(){f9&&l2&&(f9=!1,l2.length?s2=l2.concat(s2):a18=-1,s2.length&&d4())}function d4(){if(!f9){var e1=c2(h4);f9=!0;for(var t1=s2.length;t1;){for(l2=s2,s2=[];++a18<t1;)l2&&l2[a18].run();a18=-1,t1=s2.length}l2=null,f9=!1,function(e2){if(n3===clearTimeout)return clearTimeout(e2);if((n3===u2||!n3)&&clearTimeout)return n3=clearTimeout,clearTimeout(e2);try{n3(e2)}catch(t2){try{return n3.call(null,e2)}catch(t3){return n3.call(this||r14,e2)}}}(e1)}}function m(e2,t2){(this||r14).fun=e2,(this||r14).array=t2}function p2(){}o9.nextTick=function(e2){var t2=Array(arguments.length-1);if(arguments.length>1)for(var n1=1;n1<arguments.length;n1++)t2[n1-1]=arguments[n1];s2.push(new m(e2,t2)),1!==s2.length||f9||c2(d4)},m.prototype.run=function(){(this||r14).fun.apply(null,(this||r14).array)},o9.title="browser",o9.browser=!0,o9.env={},o9.argv=[],o9.version="",o9.versions={},o9.on=p2,o9.addListener=p2,o9.once=p2,o9.off=p2,o9.removeListener=p2,o9.removeAllListeners=p2,o9.emit=p2,o9.prependListener=p2,o9.prependOnceListener=p2,o9.listeners=function(e2){return[]},o9.binding=function(e2){throw Error("process.binding is not supported")},o9.cwd=function(){return"/"},o9.chdir=function(e2){throw Error("process.chdir is not supported")},o9.umask=function(){return 0};var T2=e3,t2="function"==typeof Symbol&&"symbol"==typeof Symbol.toStringTag,e2=Object.prototype.toString,o1=function(o2){return!(t2&&o2&&"object"==typeof o2&&Symbol.toStringTag in o2)&&"[object Arguments]"===e2.call(o2)},n1=function(t3){return!!o1(t3)||null!==t3&&"object"==typeof t3&&"number"==typeof t3.length&&t3.length>=0&&"[object Array]"!==e2.call(t3)&&"[object Function]"===e2.call(t3.callee)},r1=function(){return o1(arguments)}();o1.isLegacyArguments=n1;var l1=r1?o1:n1,t$1=Object.prototype.toString,o$12=Function.prototype.toString,n$12=/^\s*(?:function)?\*/,e$1="function"==typeof Symbol&&"symbol"==typeof Symbol.toStringTag,r$1=Object.getPrototypeOf,c1=function(){if(!e$1)return!1;try{return Function("return function*() {}")()}catch(t3){}}(),u1=c1?r$1(c1):{},t$2="function"==typeof Object.create?function(t3,e3){e3&&(t3.super_=e3,t3.prototype=Object.create(e3.prototype,{constructor:{value:t3,enumerable:!1,writable:!0,configurable:!0}}))}:function(t3,e3){if(e3){t3.super_=e3;var o2=function(){};o2.prototype=e3.prototype,t3.prototype=new o2,t3.prototype.constructor=t3}},i$11=function(e3){return e3&&"object"==typeof e3&&"function"==typeof e3.copy&&"function"==typeof e3.fill&&"function"==typeof e3.readUInt8},o$2={},u$11=i$11,f1=l1,a1=function(c2){return"function"==typeof c2&&(!!n$12.test(o$12.call(c2))||(e$1?r$1(c2)===u1:"[object GeneratorFunction]"===t$1.call(c2)))};function c$11(e3){return e3.call.bind(e3)}var s1="undefined"!=typeof BigInt,p1="undefined"!=typeof Symbol,y1=p1&&void 0!==Symbol.toStringTag,l$1="undefined"!=typeof Uint8Array,d1="undefined"!=typeof ArrayBuffer;if(l$1&&y1)var g3=Object.getPrototypeOf(Uint8Array.prototype),b4=c$11(Object.getOwnPropertyDescriptor(g3,Symbol.toStringTag).get);var m1=c$11(Object.prototype.toString),h1=c$11(Number.prototype.valueOf),j=c$11(String.prototype.valueOf),A=c$11(Boolean.prototype.valueOf);if(s1)var w=c$11(BigInt.prototype.valueOf);if(p1)var v1=c$11(Symbol.prototype.valueOf);function O1(e3,t3){if("object"!=typeof e3)return!1;try{return t3(e3),!0}catch(e4){return!1}}function S1(e3){return l$1&&y1?void 0!==b4(e3):B3(e3)||k(e3)||E(e3)||D1(e3)||U3(e3)||P3(e3)||x3(e3)||I1(e3)||M(e3)||z1(e3)||F1(e3)}function B3(e3){return l$1&&y1?"Uint8Array"===b4(e3):"[object Uint8Array]"===m1(e3)||u$11(e3)&&void 0!==e3.buffer}function k(e3){return l$1&&y1?"Uint8ClampedArray"===b4(e3):"[object Uint8ClampedArray]"===m1(e3)}function E(e3){return l$1&&y1?"Uint16Array"===b4(e3):"[object Uint16Array]"===m1(e3)}function D1(e3){return l$1&&y1?"Uint32Array"===b4(e3):"[object Uint32Array]"===m1(e3)}function U3(e3){return l$1&&y1?"Int8Array"===b4(e3):"[object Int8Array]"===m1(e3)}function P3(e3){return l$1&&y1?"Int16Array"===b4(e3):"[object Int16Array]"===m1(e3)}function x3(e3){return l$1&&y1?"Int32Array"===b4(e3):"[object Int32Array]"===m1(e3)}function I1(e3){return
Line truncated
|
||||
@@ -0,0 +1,29 @@
|
||||
{
|
||||
"name": "@sheeptester/htmlifier",
|
||||
"version": "1.0.3",
|
||||
"description": "Packages a Scratch project in an HTML file with no optimisations.",
|
||||
"type": "module",
|
||||
"main": "index.min.js",
|
||||
"scripts": {
|
||||
"test": "node test.js"
|
||||
},
|
||||
"keywords": [
|
||||
"htmlifier",
|
||||
"scratch",
|
||||
"sb3"
|
||||
],
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/SheepTester/htmlifier.git"
|
||||
},
|
||||
"author": "SheepTester",
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://github.com/SheepTester/htmlifier/issues"
|
||||
},
|
||||
"homepage": "https://sheeptester.github.io/htmlifier/",
|
||||
"dependencies": {
|
||||
"fetch-blob": "^3.1.2",
|
||||
"node-fetch": "^2.6.1"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
// This should not throw an error
|
||||
|
||||
import Htmlifier from './index.min.js'
|
||||
import fetch from 'node-fetch'
|
||||
|
||||
function checkForReasonableHtml (html) {
|
||||
if (html.length < 1_000_000) {
|
||||
throw new RangeError(
|
||||
`Shouldn't the result be at least a megabyte instead of ${html.length} characters?`
|
||||
)
|
||||
}
|
||||
if (!html.startsWith('<!DOCTYPE html>')) {
|
||||
throw new SyntaxError(
|
||||
`HTML does not start with DOCTYPE. Instead, it starts with ${html.slice(
|
||||
0,
|
||||
20
|
||||
)}...`
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
new Htmlifier()
|
||||
.htmlify({ type: 'id', id: '276660763' })
|
||||
.then(blob => blob.text())
|
||||
.then(checkForReasonableHtml)
|
||||
|
||||
fetch('https://sheeptester.gitlab.io/test/special_cloud_behaviours_example.sb3')
|
||||
.then(r => r.blob())
|
||||
.then(blob => new Htmlifier().htmlify({ type: 'file', file: blob }))
|
||||
.then(blob => blob.text())
|
||||
.then(checkForReasonableHtml)
|
||||
|
||||
new Htmlifier()
|
||||
.htmlify({
|
||||
type: 'url',
|
||||
url:
|
||||
'https://sheeptester.gitlab.io/test/special_cloud_behaviours_example.sb3'
|
||||
})
|
||||
.then(blob => blob.text())
|
||||
.then(checkForReasonableHtml)
|
||||
Reference in new issue
Block a user