read-comic-book-list.html
Author
Zhou Renjian
Create@
2005-03-20 17:02
Modified@
2005-03-20 17:04
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"/>
<title>Read comic's booklist.asp</title>
<style>
* {
font-family:Arial, sans-serif;
font-size:10pt;
}
</style>
<script type="text/vbscript" src="encoding.vbs"></script>
</head>
<body>
<table height="100%*" width="100%*" valign="middle">
<tr valign="middle">
<td>
<table width="*" border="0" align="left" valign="middle">
<tr>
<td>
<div id="processingStatus"></div>
<button id="btnResumeLast" onclick="javascript:resumeLast();">Resume Last</button>
<button id="btnRetrieve" onclick="javascript:retrieve();">Retrieve</button>
<button id="btnPause" disabled="disabled" onclick="javascript:pause();">Pause</button>
<button id="btnResume" disabled="disabled" onclick="javascript:resume();">Resume</button>
</td>
</tr>
</table>
</td>
</tr>
</table>
<script type="text/javascript">
function resumeLast() {
document.getElementById('btnResumeLast').disabled = true;
document.getElementById('btnRetrieve').disabled = true;
document.getElementById('btnPause').disabled = false;
Ebook.resumeLast();
}
function retrieve() {
document.getElementById('btnPause').disabled = false;
document.getElementById('btnRetrieve').disabled = true;
document.getElementById('btnResumeLast').disabled = true;
Ebook.retrieve();
}
function pause() {
document.getElementById('btnResume').disabled = false;
document.getElementById('btnPause').disabled = true;
Ebook.pause();
}
function resume() {
document.getElementById('btnResume').disabled = true;
document.getElementById('btnPause').disabled = false;
Ebook.resume();
}
var ebookGFolder = null;
var ebookXMLHttp = null;
Ebook = new Object();
Ebook.queue = new Array();
Ebook.resetQueue = function() {
Ebook.queue = new Array();
}
Ebook.queueIndex = -1;
Ebook.waitInQueue = function(o) {
Ebook.queue[Ebook.queue.length] = o;
//Ebook.queue.push(o);
}
Ebook.nextInQueue = function() {
if (Ebook.queue[Ebook.queueIndex + 1] != null) {
return Ebook.queue[++Ebook.queueIndex];
} else {
return null;
}
//return Ebook.queue.shift();
}
Ebook.asyncLoadQueue = function() {
var next = Ebook.nextInQueue();
if (next != null) {
Ebook.XMLHttpLoaded = function() {
ebookGFolder.save();
document.getElementById("processingStatus").innerHTML = "[" + next.getLocalURI() + "/" + Ebook.existedFolderNumber + "] To retrieve " + next.name + " ... Done!";
if (Ebook.isToPause) {
Ebook.saveStatus2File();
document.getElementById("processingStatus").innerHTML += " {Pause...}";
} else {
//ebookGFolder.asyncLoadFolders();
window.setTimeout(Ebook.asyncLoadQueue, 50);
}
};
document.getElementById("processingStatus").innerHTML = "[" + next.getLocalURI() + "/" + Ebook.existedFolderNumber + "] To retrieve " + next.name + " ...";
next.asyncLoad();
} else {
document.getElementById("processingStatus").innerHTML = "[Done!/" + Ebook.existedFolderNumber + "]";
}
}
/*public abstract*/
Ebook.XMLHttpLoaded = function() {
};
Ebook.saveStatus2File = function() {
Ebook.processingStatus = "" + Ebook.existedFolderNumber + "\r\n\r\n";
for (var i = 0; i < Ebook.rootFolders.length; i++) {
Ebook.traverseFolders(Ebook.rootFolders[i]);
}
var txt=FSO.OpenTextFile(Ebook.home + "\\process.status.txt", 2, true);
txt.Write(Ebook.processingStatus);
txt.Close();
}
Ebook.loadStatusFromFile = function() {
try {
txt = FSO.OpenTextFile(Ebook.home + "\\process.status.txt", 1, false);
var str = txt.ReadLine();
Ebook.existedFolderNumber = parseInt(str);
str = txt.ReadLine();
var roots = new Array();
while (!txt.AtEndOfLine) {
str = txt.ReadLine();
var folder = new ebookFolder();
folder.parseStatusString(str);
roots[roots.length] = folder;
Ebook.waitInQueue(folder);
}
Ebook.rootFolders = roots;
txt.Close();
} catch (e) {
}
}
Ebook.retrieve = function() {
}
Ebook.pause = function() {
Ebook.isToPause = true;
Ebook.saveStatus2File();
document.getElementById("processingStatus").innerHTML += " {Pause...}";
}
Ebook.resumeLast = function() {
Ebook.isToPause = false;
Ebook.resetQueue();
Ebook.loadStatusFromFile();
if (Ebook.rootFolders == null || Ebook.rootFolders.length == 0) {
Ebook.retrieve();
} else {
Ebook.asyncLoadQueue();
}
}
Ebook.resume = function() {
if (Ebook.isToPause) {
Ebook.isToPause = false;
//Ebook.resetQueue();
//Ebook.loadStatusFromFile();
Ebook.asyncLoadQueue();
}
}
Ebook.processingStatus = "";
Ebook.traverseFolders = function(eFolder) {
if (eFolder != null) {
if (!eFolder.isLoaded) {
Ebook.processingStatus += eFolder.toStatusString() + "\r\n";
} else {
for (var i = 0; i < eFolder.folders.length; i++) {
Ebook.traverseFolders(eFolder.folders[i]);
}
}
}
}
Ebook.existedFolderNumber = 0;
Ebook.home = "W:\\ebooks\\ebook";
Ebook.isToPause = false;
Ebook.rootFolders = new Array();
var FSO = new ActiveXObject("Scripting.FileSystemObject");
var ebookGlobalParent = null;
var ebookGlobalFolders = new Array();
var ebookGlobalFiles = new Array();
var ebookComicFolderBase = "http://comic.sjtu.edu.cn/booklist.asp?key=";
var ebookComicFolderBase2 = "http://202.120.1.113/book.asp?key=";
var ebookComicFileBase = "http://comic.sjtu.edu.cn/";
function ebookFile(name, url) {
this.name = name;
this.url = url;
this.toString = function() {
return name + "@" + url;
}
}
function ebookFolder(name, url) {
this.name = name;
this.url = url;
this.localuri = null;
this.parent = null;
this.folders = new Array();
this.files = new Array();
this.isLoaded = false;
this.toString = function() {
return this.name + "@" + this.url;
}
this.toStatusString = function() {
if (this.localuri != null) {
return "[" + this.localuri + "]" + this.name + "@" + this.url;
} else {
// Should never get here!
return "[]" + this.name + "@" + this.url;
}
}
this.parseStatusString = function(statusStr) {
if (statusStr != null || statusStr.length != 0) {
var bracketIndex = statusStr.indexOf("]");
var atIndex = statusStr.indexOf("@");
var uri = statusStr.substring(1, bracketIndex);
if (uri.length != 0) {
this.localuri = uri;
}
this.name = statusStr.substring(bracketIndex + 1, atIndex);
this.url = statusStr.substring(atIndex + 1);
}
}
/*protected static*/
this.parseString4XML = function(str) {
if (str != null && str.length != 0) {
return str.replace(/</g, "<").replace(/>/g, ">").replace(/&/g, "&").replace(/"/g, """).replace(/'/g, "'");
} else {
return str;
}
}
this.toXMLString = function() {
var xmlStr = "";
xmlStr += "<?xml version=\"1.0\" encoding=\"gb2312\"?>\r\n";
xmlStr += "<?xml-stylesheet type=\"text/xsl\" href=\"category.xsl\"?>\r\n";
if (this.folders.length == 0 && this.files.length == 0) {
var index = this.url.indexOf("?key=");
var myURL = null;
if (index != 0) {
myURL = this.url.substring(index + 5);
myURL = this.parseString4XML(myURL.replace(/\\/g, "/").replace(/\+/g, "%20"));
if (this.url.indexOf("202.120.1.113") != -1) {
myURL = "http://202.120.1.113:8000/" + myURL;
} else {
//if (this.url.indexOf("comic.sjtu.edu.cn") != -1)
myURL = ebookComicFileBase + "ebook/" + myURL;
}
} else {
myURL = this.url;
}
xmlStr += "<folder name=\"" + this.parseString4XML(this.name) + "\" index=\"" + myURL + "\" parent=\"" + this.parent.getLocalURI() + "\"/>\r\n";
return xmlStr;
}
if (this.parent != null) {
xmlStr += "<folder name=\"" + this.parseString4XML(this.name) + "\" url=\"" + this.getLocalURI() + "\" parent=\"" + this.parent.getLocalURI() + "\">\r\n";
} else {
xmlStr += "<folder name=\"" + this.parseString4XML(this.name) + "\" url=\"" + this.getLocalURI() + "\">\r\n";
}
for (var i = 0; i < this.folders.length; i++) {
var folder = this.folders[i];
xmlStr += "\t<folder name=\"" + this.parseString4XML(folder.name) + "\" url=\"" + folder.getLocalURI() + "\"/>\r\n";
}
for (var i = 0; i < this.files.length; i++) {
var file = this.files[i];
xmlStr += "\t<file name=\"" + this.parseString4XML(file.name) + "\" url=\"" + this.parseString4XML(file.url) + "\"/>\r\n";
}
xmlStr += "</folder>\r\n";
return xmlStr;
}
this.setParent = function(p) {
this.parent = p;
}
/*private static*/
this.parseEbookFolders = function(strHTML) {
ebookGlobalParent = this;
ebookGlobalFolders = new Array();
strHTML.replace(/\s<a href=booklist\.asp\?key=(.*) onMouseOut=(.*)\r?\n(.*)class=bodybbb>(.*)<\/font>/ig, function ($0, $1, $2, $3, $4) {
var eFolder = new ebookFolder($4, ebookComicFolderBase + $1);
eFolder.setParent(ebookGlobalParent);
ebookGlobalFolders[ebookGlobalFolders.length] = eFolder;
return $0;
});
strHTML.replace(/\s<a href=http:\/\/202\.120\.1\.113\/book\.asp\?key=(.*) onMouseOut=(.*)\r?\n(.*)class=bodybbb>(.*)<\/font>/ig, function ($0, $1, $2, $3, $4) {
var eFolder = new ebookFolder($4, ebookComicFolderBase2 + $1);
eFolder.setParent(ebookGlobalParent);
ebookGlobalFolders[ebookGlobalFolders.length] = eFolder;
return $0;
});
this.folders = ebookGlobalFolders;
}
/*private static*/
this.parseEbookFiles = function(strHTML) {
ebookGlobalFiles = new Array();
strHTML.replace(/<a href=([^\s"]*)>(.*)<\/FONT>/ig, function ($0, $1, $2) {
if ($1 != null) {
if ($1.indexOf("http://") != 0) {
ebookGlobalFiles[ebookGlobalFiles.length] = new ebookFile($2, ebookComicFileBase + $1);
} else if ($1.indexOf("http://202.120.1.113:8000/") == 0) {
ebookGlobalFiles[ebookGlobalFiles.length] = new ebookFile($2, $1);
}
}
return $0;
});
this.files = ebookGlobalFiles;
}
this.loadHTML = function(strHTML) {
this.isLoaded = true;
this.parseEbookFolders(strHTML);
this.parseEbookFiles(strHTML);
}
this.load = function() {
if (this.isLoaded) {
return ;
}
this.isLoaded = true;
var xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
xmlHttp.open("GET", this.url, false);
xmlHttp.send(null);
var strHTML = bytes2BSTR(xmlHttp.responseBody);
this.parseEbookFolders(strHTML);
this.parseEbookFiles(strHTML);
}
this.asyncLoad = function() {
if (this.isLoaded) {
return ;
}
this.isLoaded = true;
ebookXMLHttp = new ActiveXObject("Msxml2.XMLHTTP");
ebookXMLHttp.open("GET", this.url, true);
ebookXMLHttp.onreadystatechange = function () {
if (ebookXMLHttp.readyState == 4) {
var strHTML = bytes2BSTR(ebookXMLHttp.responseBody);
ebookGFolder.parseEbookFolders(strHTML);
ebookGFolder.parseEbookFiles(strHTML);
Ebook.XMLHttpLoaded();
}
}
ebookGFolder = this;
ebookXMLHttp.send(null);
}
/*
this.loadFolders = function() {
for (var i = 0; i < this.folders.length; i++) {
this.folders[i].load();
}
}
this.asyncLoadFolders = function() {
for (var i = 0; i < this.folders.length; i++) {
Ebook.waitInQueue(this.folders[i]);
}
Ebook.asyncLoadQueue();
}
*/
this.getLocalURI = function() {
if (this.localuri == null) {
var numberStr = null;
Ebook.existedFolderNumber++;
var num = Ebook.existedFolderNumber;
if (num < 10) {
numberStr = "000" + num;
} else if (num < 100) {
numberStr = "00" + num;
} else if (num < 1000) {
numberStr = "0" + num;
} else {
numberStr = "" + num;
}
this.localuri = "category-" + numberStr + ".xml";
}
return this.localuri;
}
this.generateXMLFileName = function() {
return Ebook.home + "\\" + this.getLocalURI();
}
this.save = function() {
var txt=FSO.OpenTextFile(this.generateXMLFileName(), 2, true);
txt.Write(this.toXMLString());
txt.Close();
for (var i = 0; i < this.folders.length; i++) {
Ebook.waitInQueue(this.folders[i]);
}
}
}
//Ebook.savedFolderNumber = 0;
Ebook.retrieve = function() {
var uri = "http://comic.sjtu.edu.cn/booklist.asp";
//uri = "http://comic.sjtu.edu.cn/booklist.asp?key=Linux+OS";
var ebooks = new ebookFolder("Comic Ebooks", uri);
var roots = new Array();
roots[0] = ebooks;
Ebook.rootFolders = roots;
Ebook.waitInQueue(ebooks);
//ebooks.load();
//ebooks.save();
Ebook.asyncLoadQueue();
//ebooks.asyncLoadFolders();
}
</script>
</body>
</html>