{
"translatorID":"fc353b26-8911-4c34-9196-f6f567c93901",
"translatorType":4,
"label":"Douban",
"creator":"Ace Strong<acestrong@gmail.com>",
"target":"^https?://www.douban.com/subject",
"minVersion":"1.0.0b6",
"maxVersion":"",
"priority":100,
"inRepository":true,
"lastUpdated":"2009-3-2 15:20:00"
}
function detectWeb(doc, url) {
var articleRe = /subject_search/;
var s = articleRe.exec(url);
if(s) {
return "multiple";
} else {
return "book";
}
return false;
}
function scrape(doc) {
var namespace = doc.documentElement.namespaceURI;
var nsResolver = namespace ? function(prefix) {
if (prefix == "x") return namespace; else return null;
} : null;
var nsResolver = null;
var itemType = "book";
var newItem = new Zotero.Item(itemType);
Zotero.debug(itemType);
//Zotero.debug(doc);
Zotero.debug("Title:");
// 标题,/html/body/div/h1
var titleTag = doc.evaluate('//html/body/div/h1', doc, nsResolver, XPathResult.ANY_TYPE, null).iterateNext();
//Zotero.debug("stage2:");
var title = Zotero.Utilities.trimInternal(titleTag.textContent);
Zotero.debug(title);
newItem.title = title;
// 附件,网页链接
newItem.attachments.push({url:doc.location.href, snapshot:false, title:doc.title, mimeType:"text/html"});
// url信息
newItem.url = doc.location.href;
// 其他信息,//*[@id="info"]
var info = doc.evaluate('//*[@id="info"]', doc, nsResolver, XPathResult.ANY_TYPE, null).iterateNext();
//Zotero.debug(info);
// 找出作者信息(包括译者)
var dataRows = info.getElementsByTagName("span");
//Zotero.debug(dataRows.length);
var dataRow;
var i = 0;
while(dataRow = dataRows[i]) {
i = i + 1;
var spanTags = dataRow.getElementsByTagName("span");
//Zotero.debug(spanTags.length);
if (spanTags.length>0){
// 作者或译者
var authorType = Zotero.Utilities.trimInternal(spanTags[0].textContent);
var name = Zotero.Utilities.trimInternal(dataRow.getElementsByTagName("a")[0].textContent);
//Zotero.debug(authorType);
//Zotero.debug(name);
if(authorType == "作者"){
newItem.creators.push(Zotero.Utilities.cleanAuthor(name, "author", true));
}else if(authorType == "译者"){
newItem.creators.push(Zotero.Utilities.cleanAuthor(name, "translator", true));
}
}
}
// 提取ISBN,页数,定价,出版社,装帧,出版年信息,副标题
var obmo = info.getElementsByTagName("div")[0];
var content = obmo.textContent;
//Zotero.debug(content);
dataRows = obmo.getElementsByTagName("span");
Zotero.debug(dataRows.length);
var pagesIndex;
var isbnIndex;
var publisherIndex;
var dateIndex;
i = 0;
while(dataRow = dataRows[i]) {
var infoType = dataRow.textContent;
if(infoType == "ISBN:"){
isbnIndex = i;
}else if(infoType == "页数:"){
pagesIndex = i;
}else if(infoType == "出版社:"){
publisherIndex = i;
}else if(infoType == "出版年:"){
dateIndex = i;
}
i = i + 1;
//Zotero.debug(infoType);
// 去除无用的信息
content = content.replace(infoType,"@@@");
//Zotero.debug(content);
}
// 去除前后空格
content = content.replace(/(^\s*)|(\s*$)/g, "");
// 分离信息
var infoContents = content.split("@@@");
Zotero.debug("detail info:");
var realInfo = "";
for (x in infoContents){
Zotero.debug(infoContents[x]);
if (infoContents[x] != ""){
realInfo = realInfo + infoContents[x].replace(/(^\s*)|(\s*$)/g, "") + "@@@";
}
}
//Zotero.debug(realInfo);
realInfo = realInfo.split("@@@");
//Zotero.debug(realInfo.length);
// ISBN
newItem.ISBN = realInfo[isbnIndex];
// 页数
newItem.pages = realInfo[pagesIndex];
// 出版社
newItem.publisher = realInfo[publisherIndex];
// 出版年
newItem.date = realInfo[dateIndex];
// 简介
// //div[@class="related_info"]
var relatedInfo = doc.evaluate('//div[@class="related_info"]', doc, nsResolver, XPathResult.ANY_TYPE, null).iterateNext();
var intros = relatedInfo.getElementsByTagName("div");
var h2s = relatedInfo.getElementsByTagName("h2");
if(h2s.length>0){
// 内容简介
var bookIntro;
if(h2s[0].textContent.indexOf('简介')!=-1){
if(intros[0].getElementsByTagName("span").length>0){
// 简介太长,有隐藏部分
bookIntro = "简介:\n" + intros[0].getElementsByTagName("span")[1].textContent + "\n";
}else{
bookIntro = "简介:\n" + intros[0].textContent + "\n";
}
// 正确处理段落
bookIntro = bookIntro.replace(/\s{2,}/g, "\n");
}else{
bookIntro = "";
}
// 作者简介
var authorIntro;
for(i=0;i<h2s.length;i++){
Zotero.debug(h2s[i].textContent);
}
if(h2s[1].textContent.indexOf('作者简介')!=-1){
authorIntro = "作者简介:\n" + intros[1].textContent + "\n";
// 正确处理段落
authorIntro = authorIntro.replace(/\s{2,}/g, "\n");
}else{
authorIntro = "";
}
newItem.abstractNote = bookIntro + authorIntro;
}
newItem.complete();
}
function doWeb(doc, url) {
var namespace = doc.documentElement.namespaceURI;
var nsResolver = null;
if(detectWeb(doc, url) == "multiple") {
Zotero.debug("Enter multiple~");
// search page
var items = new Array();
// //*[@id="in_tablem"]
var tablemTag = doc.evaluate('//*[@id="in_tablem"]', doc, nsResolver, XPathResult.ANY_TYPE, null).iterateNext();
var tableTags = tablemTag.getElementsByTagName("table");
Zotero.debug(tableTags.length);
var tableTag;
Zotero.debug("begin to fetch multiple title and link");
var i = 0;
while(tableTag = tableTags[i]) {
i = i + 1;
var tds = tableTag.getElementsByTagName("td");
var title ="";
var link = "";
var as = tds[1].getElementsByTagName("a");
link = as[0].href;
title = as[0].textContent;
Zotero.debug(title);
Zotero.debug(link);
if(link) {
items[link] = Zotero.Utilities.cleanString(title);
}
}
// 让用户选择要保存哪些文献
items = Zotero.selectItems(items);
if(!items) return true;
Zotero.debug("go on processing.");
var urls = new Array();
for(var url in items) {
urls.push(url);
}
} else {
var urls = [url];
}
Zotero.debug(urls);
// 下面对每条url进行解析
Zotero.Utilities.processDocuments(urls, scrape, function() { Zotero.done(); });
Zotero.wait();
}