1 <!DOCTYPE html
><html><head><meta charset=
"UTF-8">
9 flex-direction: column;
14 flex-direction: column;
17 webview{display: none;width:
100%;height:
100%}
18 .curWV{display: inherit !important;}
21 const fs
= require('fs');
22 const path
= require('path');
23 const readline
= require('readline');
28 var defaultSE
= "https://www.bing.com/search?q=";
30 let lastKeys_millis
= 0;
32 fs
.readFile(path
.join(__dirname
,'search.json'), 'utf8', (err
, jsonString
) => {
34 initSearchEngines(jsonString
,false);
36 fs
.readFile(path
.join(__dirname
,'mapkeys.json'), 'utf8', (err
, jsonStr
) => {
39 mapKeys
= JSON
.parse(jsonStr
);
42 appendAutoc_rec(path
.join(__dirname
,'default.autoc'),null);
43 appendAutoc_rec(path
.join(__dirname
,'bookmark.rec'),' ');
45 function initSearchEngines(jsonStr
){
48 engines
=JSON
.parse(jsonStr
, (key
, value
)=>{
49 if(!val1st
&& !(/^\d+$/u.test(key
))) val1st
=value
;
52 if(val1st
) defaultSE
=val1st
;
55 function save(filePath
, u8array
){
56 //alert(Object.prototype.toString.call(u8array))
57 fs
.writeFile (filePath
, u8array
, (err
) => {
64 function print2PDF(filePath
, options
){
65 tabs
.children
[iTab
].printToPDF(options
)
66 .then(u8array
=>save(filePath
,u8array
));
68 function bookmark(args
){//b [filenamestem] url title :bookmark
69 let bmFileName
= "bookmark.rec";
70 let tab
= tabs
.children
[iTab
];
71 let url
= tab
.getURL();
73 bmFileName
= args
[1]+".rec";
74 let title
= tab
.getTitle();
75 let line
= title
+ " " + url
+ "\n";
76 fs
.appendFile(path
.join(__dirname
,bmFileName
), line
, (err
)=>{});
78 function switchTab(i
){
79 let tab
= tabs
.children
[iTab
];
80 if(document
.activeElement
== tab
) tab
.blur();
81 tab
.classList
.remove('curWV');
83 tabs
.children
[iTab
].classList
.add('curWV');
86 var tab
= document
.createElement('webview');
87 tab
.allowpopups
= true;
88 tabs
.appendChild(tab
);
91 let nTabs
= tabs
.children
.length
;
98 let nTabs
= tabs
.children
.length
;
105 let nTabs
= tabs
.children
.length
;
106 if(nTabs
<2) return "";//no remain tab
107 let tab
= tabs
.children
[iTab
];
108 if(document
.activeElement
== tab
) tab
.blur();
109 tabs
.removeChild(tab
);
111 if(iTab
>=nTabs
) iTab
=iTab
-1;
112 tabs
.children
[iTab
].classList
.add('curWV');
113 return getWinTitle();
115 function getWinTitle(){
116 let t
=tabs
.children
[iTab
];
117 let title
= (iTab
+1) + '/' + tabs
.children
.length
;
118 try{title
=title
+' '+t
.getTitle()+' '+t
.getURL()}catch(e
){}
121 async
function appendAutoc_rec(filename
, delimit
){
123 const readInterface
= readline
.createInterface ({
124 input
: fs
.createReadStream (filename
, 'utf8'),
127 for await (const line
of readInterface
) {
128 let opt
= document
.createElement('option');
130 if(delimit
&& (iS
=line
.lastIndexOf(delimit
))>0){
131 opt
.value
= line
.substring(iS
+delimit
.length
);
132 opt
.textContent
= line
.substring(0,iS
);
135 document
.forms
[0].children
[0].appendChild(opt
);
139 function keyPress(e
){
140 var inputE
= document
.forms
[0].q
;
141 if (e
.altKey
||e
.metaKey
)
147 tabs
.children
[iTab
].src
= "javascript:window.scrollTo(0,0)";
150 tabs
.children
[iTab
].src
="javascript:window.scrollTo(0,document.body.scrollHeight)"
158 tabs
.children
[iTab
].src
=
159 "javascript:window.scrollBy(0,3*document.documentElement.clientHeight/4)";
162 tabs
.children
[iTab
].src
= "javascript:window.scrollBy(0,-3*document.documentElement.clientHeight/4)";
165 tabs
.children
[iTab
].src
="javascript:window.scrollBy(0,32)";
168 tabs
.children
[iTab
].src
="javascript:window.scrollBy(0,-32)";
172 if(inputE
=== document
.activeElement
) return;
173 if(1!=key
.length
) return;
174 var curMillis
= Date
.now();
175 if(curMillis
-lastKeys_millis
>500)
177 lastKeys_millis
= curMillis
;
196 lastKeys
= lastKeys
+ key
;
197 let cmds
= mapKeys
[lastKeys
];
200 for(var cmd
of cmds
.split("\n"))
204 function getQ(){return document
.forms
[0].q
.value
;}
205 function bang(query
, iSpace
){
207 let name
= query
.slice(0,iSpace
);
208 let engine
= engines
[name
];
210 return engine
+query
.substring(iSpace
+1);
212 return defaultSE
+query
;
214 function coloncommand(q
){
217 function coloncommand_render(cmd
){
218 args
= cmd
.substring(1).split(/\s+/);
235 function autoc(args
){
236 if(2!=args
.length
) return;
237 appendAutoc_rec(path
.join(__dirname
,args
[1]+".rec"),' ');
240 if(2!=args
.length
) return;
241 let filename
= args
[1]+".js";
242 fs
.readFile(path
.join(__dirname
,filename
), 'utf8', (err
,str
) => {
244 tabs
.children
[iTab
].executeJavaScript(str
,false);
247 function savePdf(args
){
248 let filename
= "ebrowser.pdf";
251 let c0
= args
[1].charCodeAt(0);
253 if(123!=c0
){//not '{' options then it is filename
254 filename
= args
[1] + ".pdf";
257 if(i
==args
.length
-1){//:Pdf [filename] {...}
258 if(2==args
[i
].length
){// '{}'
259 let width
= document
.body
.clientWidth
/96;
260 tabs
.children
[iTab
].executeJavaScript("document.documentElement.scrollHeight",
263 printBackground
:true,
264 pageSize
:{width
:width
,height
:h
/96}};
265 print2PDF(filename
,opts
);
270 options
= JSON
.parse(args
[i
]);
275 print2PDF(filename
,options
);
278 function handleQuery(q
){
280 let c0
=q
.charCodeAt(0);
283 tabs
.children
[iTab
].findInPage(q
.substring(1));
286 let c1
=q
.charCodeAt(1);
290 coloncommand_render(q
);
297 let c6
= q
.charCodeAt(6);
299 let c5
= q
.charCodeAt(5);
300 if(47==c5
&& 58==q
.charCodeAt(4))//http/file urls
302 if(58==c5
&& 47==q
.charCodeAt(7))//https://
304 }else if(q
.startsWith("javascript:")){
305 tabs
.children
[iTab
].executeJavaScript(q
.substring(11),false);
309 let iS
= q
.indexOf(' ');
312 if(58==q
.charCodeAt(5))
315 if(q
.indexOf('.')>0){
322 tabs
.children
[iTab
].src
=url
;
327 <form action=
"javascript:handleQuery(getQ())">
328 <datalist id=
"autoc"></datalist>
329 <input type=
"text" list=
"autoc" name=q
style=
"width:100%" autofocus
></form>
330 <div class=
"webviews">
331 <webview class=
"curWV" allowpopups
></webview>
334 tabs
= document
.body
.children
[1];
335 document
.addEventListener('keydown', keyPress
);