效果:
<!DOCTYPE html>
<html>
<meta charset="utf-8">
<head>
<title>应用搜索</title>
</head>
<body style="width: 100%; height: 100%;">
<div id="search"><input name="key" type="text" id="word"><button id="sbt">搜索</button></div>
<div id="searchtext"></div>
<div id="menu">等待加载</div>
<div id="content"></div>
</body>
<script>
var c={}
const StreamZip = require('node-stream-zip');
// Create an empty context menu
function newcontent(strs){
//alert(strs);
document.querySelector("#content").innerHTML=c[strs];
//alert(c[strs]);
for(var i= 0; i< document.querySelectorAll("a").length; i ++){
document.querySelectorAll("a")[i].onclick= function(ev){
ev.preventDefault();
var kry=ev.target.href;
var urls=kry.split("/");
newcontent(urls.pop());
};
}
}
var path=require("path");
var fs = require("fs");
var cwdPath = path.dirname(path.dirname(path.dirname(path.dirname(process.cwd()))));
cwdPath+="/";
//alert(cwdPath);
var base=cwdPath+"../books/";
fs.readdir(base,function(err, files){
if (err) {
return console.error(err);
}
files.forEach( function (files){
//alert(files);
var zip = new StreamZip({
file: base+files,
storeEntries: true
});
zip.on('ready', () => {
// Take a look at the files
console.log('Entries read: ' + zip.entriesCount);
var str="";
for (const entry of Object.values(zip.entries())) {
const desc = entry.isDirectory ? 'directory' : `${entry.size} bytes`;
//console.log(`Entry ${entry.name}: ${desc}`);
//str+="<span onClick='newcontent(\""+entry.name+"\")'>"+entry.name+"</span>"
//document.querySelector("#menu").innerHTML=str;
if(entry.name.indexOf('html')!=-1){
c[entry.name]=zip.entryDataSync(entry.name);
}
}
zip.close()
});
});
document.querySelector("#menu").innerHTML="加载完成";
});
//
document.querySelector("#sbt").addEventListener('click', function(ev) {
var w=document.querySelector("#word").value;
var ss=""
for (i in c) {
if(c[i].indexOf(w)!=-1){
var old=c[i].toString();
var news=old.replace(/<[^>]*>/g,"");
var num=news.indexOf(w);
if(num>10){
news=news.substring(num-10,num+20);
}else{
news=news.substring(num-3,num+20);
}
ss+="<li onClick='newcontent(\""+i+"\")'>"+news+"</li>";
}
}
document.querySelector("#searchtext").innerHTML=ss;
return false;
}, false);
var lock=0;
document.querySelector("#word").addEventListener('input', function(ev) {
if(lock){
return false;
}
lock=1;
var w=document.querySelector("#word").value;
var ss=""
for (i in c) {
if(c[i].indexOf(w)!=-1){
var old=c[i].toString();
var news=old.replace(/<[^>]*>/g,"");
var num=news.indexOf(w);
if(num>10){
news=news.substring(num-10,num+20);
}else{
news=news.substring(num-3,num+20);
}
ss+="<li onClick='newcontent(\""+i+"\")'>"+news+"</li>";
}
}
document.querySelector("#searchtext").innerHTML=ss;
lock=0;
return false;
})
</script>
</html>
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.