Last active
February 25, 2016 05:49
-
-
Save PaleNeutron/3ca527c5bce95f88b68f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name WIPS | |
// @namespace pn | |
// @description expand result | |
// @include http://www.wipsglobal.com/servicecn/toc/basicResult.wips | |
// @grant none | |
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js | |
// @require https://raw.githubusercontent.com/placemarker/jQuery-MD5/master/jquery.md5.js | |
// @version 0.3.0 | |
// ==/UserScript== | |
this.$ = this.jQuery = jQuery.noConflict(true); | |
//是否使用百度翻译 | |
var translate_baidu = 1; | |
var search_pattern = ''; | |
var company = ''; | |
var mycontent = ''; | |
var patentlist = []; | |
var patent_titles = []; | |
var patent_contents = []; | |
var pdfs = []; | |
var fin_rep_num = 0; | |
var fin_pdf_num = 0; | |
var raw_elements = []; | |
var trans_patent_titles = []; | |
var cur_pos = 0; | |
var trans_result = []; | |
var baidu_url = 'http://api.fanyi.baidu.com/api/trans/vip/translate'; | |
var tran_list = []; | |
var patent_locales = ''; | |
var div_for_button = document.createElement("div"); | |
div_for_button.style.textAlign = "center"; | |
document.body.appendChild(div_for_button); | |
var report_button = document.createElement("input"); | |
report_button.type = "button"; | |
report_button.value = "生成报告"; | |
report_button.onclick = generate_report; | |
div_for_button.appendChild(report_button); | |
var pdf_button = document.createElement("input"); | |
pdf_button.type = "button"; | |
pdf_button.value = "下载全部PDF"; | |
pdf_button.onclick = generate_pdf; | |
div_for_button.appendChild(pdf_button); | |
function generate_pdf() { | |
for (var i = 0; i < patentlist.length; i++) { | |
var skey = patentlist[i]; | |
getpdf(skey, i); | |
}; | |
} | |
function generate_report() { | |
for (var i = 0; i < patentlist.length; i++) { | |
var skey = patentlist[i]; | |
get_detail(skey, i); | |
}; | |
} | |
$(document).ready( | |
function() { | |
search_pattern = $('#srch_qry').val(); | |
company = search_pattern.split(' (').pop().replace(').AP.', ''); | |
var a_elements = $('.resultlist_tabletype > tbody:nth-child(3) td.align_left_ch a'); | |
patent_locales = [i.innerHTML.toLowerCase() for (i of $('.resultlist_tabletype > tbody:nth-child(3) .width4').toArray())]; | |
for (var i = patent_locales.length - 1; i >= 0; i--) { | |
if (patent_locales[i] != 'cn') { | |
raw_elements.push(a_elements[i]); | |
} | |
} | |
$('.resultlist_tabletype > tbody:nth-child(3) td.align_left_ch a').each(function(i) { | |
var a = this; | |
var title_url = $(this).attr('href').split('&')[0]; | |
var skey = title_url.split('=')[1]; | |
patentlist.push(skey); | |
//用中文专利名替换英文专利名 | |
if (patent_locales[i] == 'cn') { | |
$.get(title_url, function(response) { | |
var t = $(response).find('.tit02 p:eq(1)').text(); | |
$(a).html(t); | |
patent_titles[i] = t; | |
}); | |
} else { | |
patent_titles[i] = $(a).text(); //依旧是用英文标题来生成report | |
} | |
}); | |
tran_list = [$(i).text() | |
for (i of raw_elements) | |
]; | |
if (translate_baidu) { | |
get_baidu(tran_list.slice(0, 50)); | |
}; | |
} | |
); | |
function get_detail(skey, i) { | |
if (patent_locales[i] == 'jp') { | |
var detail_url = 'http://www.wipsglobal.com/servicecn/doc/doc' + 'pa' + '.wips?skey=' + skey; | |
} else { | |
var detail_url = 'http://www.wipsglobal.com/servicecn/doc/doc' + patent_locales[i] + '.wips?skey=' + skey; | |
} | |
//获取专利信息和摘要 | |
$.get(detail_url, function(response) { | |
var h = $('<html></html>').append(response + '</div>') | |
$(h).find('.under').each(function(index) { | |
$(this).attr('class', 'case' + index % 3) | |
}); | |
$(h).find('.no_under').remove(); | |
$(h).find('img').remove(); | |
patent_contents[i] = $(h).html(); | |
fin_rep_num += 1; | |
gen_report(); | |
}); | |
} | |
function getpdf(skey, i) { | |
var pdf_url = 'http://www.wipsglobal.com/servicecn/dwn/pdfDirectDownload.wips?skey=' + skey; | |
var xhr = new XMLHttpRequest(); | |
xhr.open("GET", pdf_url); | |
xhr.responseType = "blob"; | |
xhr.onreadystatechange = function() { | |
if (xhr.response.size > 50000) { | |
if (xhr.readyState == xhr.DONE) { | |
pdfs[i] = xhr.response; | |
fin_pdf_num += 1; | |
gen_pdf(); | |
} | |
} else { | |
// getpdf(skey, i); | |
console.log(skey + ' is banded! size: ' + xhr.response.size); | |
fin_pdf_num += 1; | |
gen_pdf(); | |
} | |
} | |
xhr.send(); | |
} | |
function gen_pdf() { | |
console.log("pdf: " + (patentlist.length - fin_pdf_num)); | |
if (fin_pdf_num == patentlist.length) { | |
for (var i = 0, len = patentlist.length; i < len; i++) { | |
auto_download(patent_titles[i] + '.pdf', pdfs[i]); | |
} | |
fin_pdf_num = 0; | |
} | |
} | |
function gen_report() { | |
//整合专利名和专利基本信息,生成报告 | |
console.log("report: " + (patentlist.length - fin_rep_num)); | |
if (fin_rep_num == patentlist.length) { | |
var myindex = $('<div><ol></ol></div>'); | |
$(myindex).attr('id', 'content_index'); | |
trans_patent_titles = [i.innerHTML | |
for (i of $('.resultlist_tabletype > tbody:nth-child(3) td.align_left_ch a').toArray()) | |
]; | |
for (var i = 0, len = patentlist.length; i < len; i++) { | |
//制作PDF链接 | |
var a = document.createElement('a'); | |
var p = document.createElement('div'); | |
$(p).attr('class', 'pdf_link'); | |
$(a).attr('href', "pdf/" + patent_titles[i] + ".pdf"); | |
$(a).attr('target', '_blank'); | |
$(a).text("PDF文档链接:" + patent_titles[i]); | |
p.appendChild(a); | |
//制作标题 | |
var h2 = document.createElement('h2'); | |
$(h2).html(trans_patent_titles[i]); | |
$(h2).attr('id', trans_patent_titles[i]); | |
//添加目录项 | |
var indexlink = document.createElement('a'); | |
$(myindex).find('ol').append($('<li></li>').append(indexlink)); | |
indexlink.href = '#' + trans_patent_titles[i]; | |
$(indexlink).html(trans_patent_titles[i]); | |
mycontent = mycontent + '<div class = "patent">' + h2.outerHTML + '\n' + patent_contents[i] + p.outerHTML + '</div>'; | |
} | |
mycontent = myindex[0].outerHTML + mycontent; | |
mycontent = $('<h2></h2>').html('搜索条件:' + search_pattern)[0].outerHTML + '\n' + mycontent; | |
mycontent = format_patent(company, mycontent); | |
//创建自动下载 | |
auto_download(company + '.html', mycontent); | |
mycontent = ''; | |
fin_rep_num = 0; | |
} | |
} | |
function get_baidu(para_list) { | |
var sign; | |
var str; | |
var appid = "20160225000013419"; | |
var key = "zo_m0kE8B4weeGlfCLtg"; | |
str = para_list.join('\n'); | |
salt = Math.floor((Math.random() * 1000000000)) | |
sign = $.md5(appid + str + salt +key); | |
$.ajax({ | |
url: baidu_url, | |
type: 'get', | |
dataType: 'jsonp', | |
data: { | |
q: str, | |
from: "auto", | |
to: "auto", | |
appid: appid, | |
salt : salt, | |
sign: sign | |
}, | |
success: next_trans, | |
error: function(xhr, str, err) { | |
console.log(str); | |
console.log(err); | |
}, | |
}); | |
} | |
function next_trans(data) { | |
cur_pos += 1; | |
trans_result = trans_result.concat(data.trans_result); | |
if (tran_list.length - 1 > cur_pos * 50) { | |
get_baidu(tran_list.slice(cur_pos * 50, (cur_pos + 1) * 50)); | |
} else { | |
change_elements(trans_result); | |
} | |
} | |
function change_elements(trans_result) { | |
for (var i = trans_result.length - 1; i >= 0; i--) { | |
$(raw_elements[i]).attr('title', $(raw_elements[i]).text()); | |
$(raw_elements[i]).html(trans_result[i].dst + '(百度翻译)' + '<br />' + $(raw_elements[i]).text()); | |
} | |
} | |
auto_download = function(fileName, mycontent) { | |
//创建自动下载链接 | |
var aLink = document.createElement('a'); | |
var blob = new Blob([mycontent]); | |
var evt = document.createEvent("MouseEvents"); | |
evt.initEvent("click", false, false); //initEvent 不加后两个参数在FF下会报错, 感谢 Barret Lee 的反馈 | |
aLink.download = fileName; | |
aLink.href = URL.createObjectURL(blob); | |
$(document.body).append(aLink); | |
aLink.dispatchEvent(evt); | |
} | |
format_patent = function(title, patent) { | |
//修饰html,增加head | |
patent = patent.replace(/<br>/g, ''); | |
patent = '<head>\n<meta charset="utf-8">\n<title>' + title + '</title>\n<link href="patent.css" type="text/css" rel="stylesheet">\n</head><body><h1>' + title + '</h1>' + patent + '</body>'; | |
patent = '<!DOCTYPE html>\n<html>' + patent + '</html>'; | |
return patent; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment