@acepad/browser 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -0
- package/acepad-browser.js +169 -114
- package/dom.js +69 -63
- package/package.json +12 -9
- package/test/index.html +4 -0
- package/test/index.js +3 -0
- package/test/sub.js +4 -0
- package/test/test-browser.js +20 -0
- package/url.js +32 -32
package/README.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
NOTE: This package is supporsed to work in [acepad](https://github.com/hoge1e3/acepad-dev), not a regular node envronment.
|
|
2
|
+
|
|
3
|
+
# @acepad/browser
|
|
4
|
+
|
|
5
|
+
Render Webpage(HTML file) using filesystems in [acepad](https://github.com/hoge1e3/acepad-dev/blob/main/README.md)/[petit-node](https://github.com/hoge1e3/petit-node). Link/Images/Javascript from HTML files are also referred within file systems.
|
|
6
|
+
|
|
7
|
+
## functions
|
|
8
|
+
|
|
9
|
+
- `open`(file, options)
|
|
10
|
+
- `file` HTML [File object](https://github.com/hoge1e3/sfile?tab=readme-ov-file#quick-reference-of-file-object) to be rendered.
|
|
11
|
+
- `options`
|
|
12
|
+
- `doc` target document object to be rendered,default, `globalThis.document`
|
|
13
|
+
- `pNode` Set true to use [petit-node](https://github.com/hoge1e3/petit-node) module loading features that enables import/require other local file in the Javascript files referred from the HTML file.
|
|
14
|
+
|
package/acepad-browser.js
CHANGED
|
@@ -1,114 +1,169 @@
|
|
|
1
|
-
import {appendNode} from "./dom.js";
|
|
2
|
-
import {createModuleURL} from "petit-node";
|
|
3
|
-
import {createURL} from "./url.js";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
let
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
let
|
|
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
|
-
s
|
|
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
|
-
|
|
1
|
+
import {appendNode} from "./dom.js";
|
|
2
|
+
import {createModuleURL, getCore} from "petit-node";
|
|
3
|
+
import {createURL} from "./url.js";
|
|
4
|
+
import {sibling,file} from "@acepad/here";
|
|
5
|
+
export async function open(f,_opt={}){
|
|
6
|
+
let p=new DOMParser();
|
|
7
|
+
let html=f.text();
|
|
8
|
+
const colon=":";
|
|
9
|
+
let d=p.parseFromString(html,"text/html");
|
|
10
|
+
let _g=_opt.globalThis||globalThis;
|
|
11
|
+
let opt={
|
|
12
|
+
globalThis:_g,
|
|
13
|
+
doc:_opt.doc||_opt.document||_g.document,
|
|
14
|
+
resolve(p){
|
|
15
|
+
return f.sibling(p);
|
|
16
|
+
},
|
|
17
|
+
async toURL(p,n){
|
|
18
|
+
let t=n.getAttribute("type");
|
|
19
|
+
if(t=="module"){
|
|
20
|
+
return await createModuleURL(opt.resolve(p));
|
|
21
|
+
}
|
|
22
|
+
return await createURL(opt.resolve(p));
|
|
23
|
+
},
|
|
24
|
+
protocol(href,n){
|
|
25
|
+
|
|
26
|
+
},
|
|
27
|
+
open:(f)=>{
|
|
28
|
+
return open(f,_opt);
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
if(_opt.pNode){
|
|
32
|
+
const g=opt.globalThis;
|
|
33
|
+
const pn=await loadPNode(g, getCore());
|
|
34
|
+
const {open}=await pn.importModule(file(import.meta.url));
|
|
35
|
+
await open(f);
|
|
36
|
+
return ;
|
|
37
|
+
}
|
|
38
|
+
//const s=programmableReadyState();
|
|
39
|
+
//s.value="loading";
|
|
40
|
+
//let h=hack();
|
|
41
|
+
for(let t of ["head","body"]){
|
|
42
|
+
let hd=opt.doc.querySelector(t);
|
|
43
|
+
if(!_opt.keep)hd.innerHTML="";
|
|
44
|
+
let hs=d.querySelector(t);
|
|
45
|
+
await appendNode(hs, hd, opt);
|
|
46
|
+
}
|
|
47
|
+
/*h.setReadyState("interactive");
|
|
48
|
+
h.setReadyState("complete");
|
|
49
|
+
|
|
50
|
+
s.value="complete";
|
|
51
|
+
document.dispatchEvent(new Event("readystatechange"));
|
|
52
|
+
const le=new Event("load");
|
|
53
|
+
window.dispatchEvent(le);*/
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function loadPNode(g, core){
|
|
57
|
+
if(g.pNode)return g.pNode;
|
|
58
|
+
//console.log("loadPNode",g,core);
|
|
59
|
+
const f=new g.Function ("core", `
|
|
60
|
+
async function main(){
|
|
61
|
+
${pNodeHeader()};
|
|
62
|
+
globalThis.pNode=pNode;
|
|
63
|
+
globalThis.FS=pNode.FS;
|
|
64
|
+
return pNode;
|
|
65
|
+
}
|
|
66
|
+
return main();
|
|
67
|
+
`);
|
|
68
|
+
//console.log(f+"");
|
|
69
|
+
return f(core);
|
|
70
|
+
}
|
|
71
|
+
const cdn="https://cdn.jsdelivr.net/npm/";
|
|
72
|
+
function pNodeHeader(){
|
|
73
|
+
return `${header()}
|
|
74
|
+
globalThis.pNode=pNode;
|
|
75
|
+
(${hackTimeouts})();
|
|
76
|
+
await pNode.boot({core});
|
|
77
|
+
`;
|
|
78
|
+
//const FS=pNode.getFS();
|
|
79
|
+
//await FS.mountAsync("/idb/","idb");`;
|
|
80
|
+
|
|
81
|
+
function header(){
|
|
82
|
+
let purl=process.env.PNODE_URL;
|
|
83
|
+
// static import does not reports error
|
|
84
|
+
// using relative path for import does not work in BlobURL?
|
|
85
|
+
if (!purl ||
|
|
86
|
+
( !purl.match(/^https:\/\//) &&
|
|
87
|
+
!purl.includes("127.0.0.1") )
|
|
88
|
+
) purl=cdn+"petit-node@latest/dist/index.js";
|
|
89
|
+
return `
|
|
90
|
+
const pNode=await import(${JSON.stringify(purl)});`;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function hackTimeouts(){
|
|
94
|
+
for(let k of ["setTimeout","setInterval",
|
|
95
|
+
"clearTimeout","clearInterval",]){
|
|
96
|
+
globalThis[k]=globalThis[k].bind(globalThis);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
//
|
|
101
|
+
|
|
102
|
+
function hack(){
|
|
103
|
+
let h=document.addEventListener.hacked;
|
|
104
|
+
h.readyState=document.readyState;
|
|
105
|
+
if(!h){
|
|
106
|
+
document.addEventListener.hacked=h={};
|
|
107
|
+
Object.defineProperty(document,"readyState",{
|
|
108
|
+
get(){
|
|
109
|
+
return h.readyState;
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
h.readystatechange=hackEventListener(
|
|
113
|
+
document,"readystatechange");
|
|
114
|
+
h.load=hackEventListener(
|
|
115
|
+
window,"load");
|
|
116
|
+
h.domcontentloaded=hackEventListener(
|
|
117
|
+
document,"domcontentloaded");
|
|
118
|
+
h.setReadyState=(s)=>{
|
|
119
|
+
h.readyState=s;
|
|
120
|
+
if(h.readyState==="interactive"){
|
|
121
|
+
const e=new Event("DOMContentLoaded");
|
|
122
|
+
for(let f of h.domcontentloaded){
|
|
123
|
+
f.call(document,e);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
if(h.readyState==="complete"){
|
|
127
|
+
const e=new Event("load");
|
|
128
|
+
for(let f of h.load){
|
|
129
|
+
f.call(window,e);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
const e=new Event("readystatechange");
|
|
133
|
+
for(let f of h.readystatechange){
|
|
134
|
+
f.call(document,e);
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
h.readystatechange.length=0;
|
|
139
|
+
h.readystatechange.push((e)=>{
|
|
140
|
+
if(typeof
|
|
141
|
+
document.onreadystatechange==="function"){
|
|
142
|
+
document.onreadystatechange(e);
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
delete document.onreadystatechange;
|
|
146
|
+
h.domcontentloaded.length=0;
|
|
147
|
+
h.load.length=0;
|
|
148
|
+
h.load.push((e)=>{
|
|
149
|
+
if(typeof
|
|
150
|
+
window.onload==="function"){
|
|
151
|
+
window.onload(e);
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
delete window.onload;
|
|
155
|
+
h.readyState="loading";
|
|
156
|
+
return h;
|
|
157
|
+
}
|
|
158
|
+
function hackEventListener(o,type){
|
|
159
|
+
const old=o.addEventListener;
|
|
160
|
+
const listeners=[];
|
|
161
|
+
o.addEventListener=function (t,...a){
|
|
162
|
+
if(type.toLowerCase()!==t.toLowerCase()){
|
|
163
|
+
return old.apply(this,[t,...a]);
|
|
164
|
+
}
|
|
165
|
+
alert(t+a);
|
|
166
|
+
listeners.push(a[0]);
|
|
167
|
+
};
|
|
168
|
+
return listeners;
|
|
169
|
+
}
|
package/dom.js
CHANGED
|
@@ -1,64 +1,70 @@
|
|
|
1
|
-
function loadPromise(n){
|
|
2
|
-
return new Promise((s,e)=>{
|
|
3
|
-
n.onload=s;
|
|
4
|
-
n.onerror=e;
|
|
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
|
-
|
|
1
|
+
function loadPromise(n){
|
|
2
|
+
return new Promise((s,e)=>{
|
|
3
|
+
n.onload=s;
|
|
4
|
+
n.onerror=e;
|
|
5
|
+
});
|
|
6
|
+
}
|
|
7
|
+
function hasProtocol(url) {
|
|
8
|
+
return url.match(/^\w+:/);
|
|
9
|
+
}
|
|
10
|
+
function isRelativePath(p) {
|
|
11
|
+
return !p.startsWith("/");
|
|
12
|
+
}
|
|
13
|
+
export async function appendNode(src,dst,opt) {
|
|
14
|
+
opt.doc=opt.doc||document;
|
|
15
|
+
let {doc,open,resolve,toURL}=opt;
|
|
16
|
+
const colon=":";
|
|
17
|
+
const c=src.childNodes;
|
|
18
|
+
const loadings=[];
|
|
19
|
+
//console.log("apn",src.innerHTML,dst);
|
|
20
|
+
for(let n of c){
|
|
21
|
+
switch (n.nodeType) {
|
|
22
|
+
case Node.ELEMENT_NODE:
|
|
23
|
+
let nn=doc.createElement(n.tagName);
|
|
24
|
+
let at=Array.from(n.attributes);
|
|
25
|
+
// should charset must be set first than src
|
|
26
|
+
const names=at.map(a=>a.name);
|
|
27
|
+
var idx=names.indexOf("charset");
|
|
28
|
+
if (idx>=0) {
|
|
29
|
+
names.splice(idx,1);
|
|
30
|
+
names.unshift("charset");
|
|
31
|
+
}
|
|
32
|
+
let w;
|
|
33
|
+
for(let name of names) {
|
|
34
|
+
let value=n.getAttribute(name);
|
|
35
|
+
let isrel=!hasProtocol(value) && isRelativePath(value);
|
|
36
|
+
let tag=n.tagName.toLowerCase();
|
|
37
|
+
if (name=="href" && isrel) {
|
|
38
|
+
if(tag=="a"){
|
|
39
|
+
let f=resolve(value);
|
|
40
|
+
|
|
41
|
+
nn.addEventListener(
|
|
42
|
+
"click",
|
|
43
|
+
()=>{
|
|
44
|
+
open(f);
|
|
45
|
+
});
|
|
46
|
+
value="javascript"+colon+";";
|
|
47
|
+
}else{
|
|
48
|
+
value=await toURL(value,n);
|
|
49
|
+
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
if (name=="src") {
|
|
53
|
+
if(isrel)value=await toURL(value,n);
|
|
54
|
+
if (tag=="script") {
|
|
55
|
+
//alert(value);
|
|
56
|
+
w=loadPromise(nn);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
nn.setAttribute(name, value);
|
|
60
|
+
}
|
|
61
|
+
dst.appendChild(nn);
|
|
62
|
+
await w;
|
|
63
|
+
await appendNode(n ,nn,opt);
|
|
64
|
+
break;
|
|
65
|
+
case Node.TEXT_NODE:
|
|
66
|
+
dst.appendChild(doc.createTextNode(n.textContent));
|
|
67
|
+
break;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
64
70
|
}
|
package/package.json
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
|
-
{
|
|
2
|
-
"main": "acepad-browser.js",
|
|
3
|
-
"name": "@acepad/browser",
|
|
4
|
-
"type": "module",
|
|
5
|
-
"
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
{
|
|
2
|
+
"main": "acepad-browser.js",
|
|
3
|
+
"name": "@acepad/browser",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"test": "run test/test-browser.js"
|
|
7
|
+
},
|
|
8
|
+
"version": "1.0.1",
|
|
9
|
+
"description": "Show Webpage in acepad",
|
|
10
|
+
"dependencies": {
|
|
11
|
+
"petit-node": "^1.0.0"
|
|
12
|
+
}
|
|
10
13
|
}
|
package/test/index.html
ADDED
package/test/index.js
ADDED
package/test/sub.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import {t} from "@hoge1e3/dom";
|
|
2
|
+
import {show} from "@acepad/widget";
|
|
3
|
+
import {open} from "@acepad/browser";
|
|
4
|
+
import {sibling} from "@acepad/here";
|
|
5
|
+
|
|
6
|
+
export async function main(){
|
|
7
|
+
const element=(t.iframe());
|
|
8
|
+
await new Promise(s=>{
|
|
9
|
+
element.addEventListener("load",s);
|
|
10
|
+
show(t.div(element));
|
|
11
|
+
});
|
|
12
|
+
const document=element.contentWindow.document;
|
|
13
|
+
await open(sibling(import.meta.url,"index.html"),{
|
|
14
|
+
globalThis: element.contentWindow,
|
|
15
|
+
document,
|
|
16
|
+
pNode:true,
|
|
17
|
+
|
|
18
|
+
});
|
|
19
|
+
console.log(document.body.innerHTML);
|
|
20
|
+
}
|
package/url.js
CHANGED
|
@@ -1,33 +1,33 @@
|
|
|
1
|
-
|
|
2
|
-
class Entry{
|
|
3
|
-
constructor(f,url){
|
|
4
|
-
this.f=f;
|
|
5
|
-
this.url=url;
|
|
6
|
-
this.time=f.lastUpdate();
|
|
7
|
-
}
|
|
8
|
-
upToDate(){
|
|
9
|
-
return this.f.exists()&&
|
|
10
|
-
this.f.lastUpdate()==this.time;
|
|
11
|
-
}
|
|
12
|
-
dispose(){
|
|
13
|
-
URL.revokeObjectURL(this.url);
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
const cache=new Map();
|
|
17
|
-
export function createURL(f){
|
|
18
|
-
const k=f.path();
|
|
19
|
-
let e=cache.get(k);
|
|
20
|
-
if(e) {
|
|
21
|
-
if(e.upToDate()) return e.url;
|
|
22
|
-
e.dispose();
|
|
23
|
-
cache.delete(k);
|
|
24
|
-
}
|
|
25
|
-
if(!f.exists()){
|
|
26
|
-
throw new Error(`Cannot createURL ${f}: not exists`);
|
|
27
|
-
}
|
|
28
|
-
const blob=f.getBlob();
|
|
29
|
-
const blobUrl = URL.createObjectURL(blob);
|
|
30
|
-
e=new Entry(f,blobUrl);
|
|
31
|
-
cache.set(k,e);
|
|
32
|
-
return blobUrl;
|
|
1
|
+
|
|
2
|
+
class Entry{
|
|
3
|
+
constructor(f,url){
|
|
4
|
+
this.f=f;
|
|
5
|
+
this.url=url;
|
|
6
|
+
this.time=f.lastUpdate();
|
|
7
|
+
}
|
|
8
|
+
upToDate(){
|
|
9
|
+
return this.f.exists()&&
|
|
10
|
+
this.f.lastUpdate()==this.time;
|
|
11
|
+
}
|
|
12
|
+
dispose(){
|
|
13
|
+
URL.revokeObjectURL(this.url);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
const cache=new Map();
|
|
17
|
+
export function createURL(f){
|
|
18
|
+
const k=f.path();
|
|
19
|
+
let e=cache.get(k);
|
|
20
|
+
if(e) {
|
|
21
|
+
if(e.upToDate()) return e.url;
|
|
22
|
+
e.dispose();
|
|
23
|
+
cache.delete(k);
|
|
24
|
+
}
|
|
25
|
+
if(!f.exists()){
|
|
26
|
+
throw new Error(`Cannot createURL ${f}: not exists`);
|
|
27
|
+
}
|
|
28
|
+
const blob=f.getBlob();
|
|
29
|
+
const blobUrl = URL.createObjectURL(blob);
|
|
30
|
+
e=new Entry(f,blobUrl);
|
|
31
|
+
cache.set(k,e);
|
|
32
|
+
return blobUrl;
|
|
33
33
|
}
|