效果: 源码:
<!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>
1
liuwenxu OP mac 下载链接:https://pan.baidu.com/s/1qyVP5qvnotls30Zkso3seg 密码:opvd
|
2
jecshcier 2018-10-15 09:00:34 +08:00 via iPhone
楼主加油。。功能上有点简单。
|