// ==UserScript==
// @name 百度云插件+APIKey
// @namespace
// @version 5.0.3
// @description 在百度云网盘的页面添加一个搜索框,调用搜索API搜索所有公开分享文件// To add a search frame that calls some api for searching some public shared files in BaiduYun cloud netdisk.
// @description For more imformation,please email me at wanghsinche@hotmail.com.
// @include /https?\:\/\/(pan|yun)\.baidu\.com.*/
// @grant GM_xmlhttpRequest
// @run-at document-end
// @copyright 2014,04,20 __By Wang Hsin-che
// @downloadURL https://update.greasyfork.icu/scripts/496/%E7%99%BE%E5%BA%A6%E4%BA%91%E6%8F%92%E4%BB%B6%2BAPIKey.user.js
// @updateURL https://update.greasyfork.icu/scripts/496/%E7%99%BE%E5%BA%A6%E4%BA%91%E6%8F%92%E4%BB%B6%2BAPIKey.meta.js
// ==/UserScript==
/*thanks to the tutorial of mvc at
https://alexatnet.com/articles/model-view-controller-mvc-javascript*/
//Event is a simple class for implementing the Observer pattern:
function Event(sender) {
this._sender = sender;
this._listeners = [];
}
Event.prototype = {
attach : function (listener) {//push the callback function into _listeners[];
this._listeners.push(listener);
},
notify : function (args) {//pop all the callback functions and execute the functions with same args?
var index;
for (index = 0; index < this._listeners.length; index += 1) {
this._listeners[index](this._sender, args);//auto pass sender and args into callback function, so the default form of callback function is function(sender,args){}
}
}
};
//Javascript-template-engine-in-just-20-line
//by by Krasimir http://krasimirtsonev.com/blog/article/Javascript-template-engine-in-just-20-line
var TemplateEngine = function(html, options) {
var re = /<%([^%>]+)?%>/g, reExp = /(^( )?(if|for|else|switch|case|break|{|}))(.*)?/g, code = 'var r=[];\n', cursor = 0, match;
var add = function(line, js) {
js? (code += line.match(reExp) ? line + '\n' : 'r.push(' + line + ');\n') :
(code += line != '' ? 'r.push("' + line.replace(/"/g, '\\"') + '");\n' : '');
return add;
};
while(match = re.exec(html)) {
add(html.slice(cursor, match.index))(match[1], true);
cursor = match.index + match[0].length;
}
add(html.substr(cursor, html.length - cursor));
code += 'return r.join("");';
return new Function(code.replace(/[\r\t\n]/g, '')).apply(options);
};
/**
* The Model. Model stores items and notifies
* observers about changes.
*/
var BaseModel=function(engineLst){
this.keyword="";
this.engine="default";
this.engineLst=engineLst;
this.jsonObj={
cursor: {
estimatedResultCount: 0,
resultCount: 0 },
results: []
};
this.curr=1;
this.totalPage=1;
this.urls={default:'http://',};
this.state=false;
this.contentUpdated=new Event(this);
this.requestEvent=new Event(this);
};
BaseModel.prototype={
setEngine:function(engine){
var tempObj = {};
this.engine=engine;
tempObj['engine'] = engine;
window.localStorage.setItem('wxzYunpanSearcher',JSON.stringify(tempObj));
tempObj = null;
},
updateCurr:function(curr){
this.curr=curr;
},
updateKeyword:function(keyword){
this.keyword=keyword;
},
request:function(){
this.requestEvent.notify();
var _this=this;
GM_xmlhttpRequest({
method: "GET",
url: _this.compileUrl[_this.engine](_this),
headers: {
"User-Agent": "Mozilla/5.0", // If not specified, navigator.userAgent will be used.
"Accept": "text/xml" // If not specified, browser defaults will be used.
},
onload: function(response) {
_this.jsonObj=_this.toJson[_this.engine](response.responseText);
_this.totalPage=(parseInt(_this.jsonObj.cursor.resultCount)-parseInt(_this.jsonObj.cursor.resultCount)%10)/10+1;
_this.state=true;
var _self=_this;
_this.contentUpdated.notify(_self.state);
},
onerror: function() {
_this.jsonObj={
cursor: {
estimatedResultCount: 0,
resultCount: 0 },
results: []
};
_this.totalPage=1;
_this.state=false;
var _self=_this;
_this.contentUpdated.notify(_self.state);
}
});
},
destory:function(){
this.jsonObj={};
this.totalPage=0;
this.state=false;
this.curr=0;
this.keyword="";
},
toJson:{
default:function(text){
var jsonObj = {
cursor: {
estimatedResultCount: 0,
resultCount: 0 },
results: []
};
return jsonObj;
},
},
compileUrl:{
default:function(_this){
return _this.url + _this.keyword + '+site%3Apan.baidu.com' + '&first=' + _this.curr;
},
},
};
var Viewer=function(model,UIelements){
this.model=model;
this.UI=UIelements;
this.searchClick=new Event(this);
this.closeClick=new Event(this);
this.nextClick=new Event(this);
this.preClick=new Event(this);
this.toPageClick=new Event(this);
this.engineOptChange=new Event(this);
var bodyNode = document.querySelector('body');
var _self=this;
this.UI.searchBtn.addEventListener('click',function(event) {
/* Act on the event */
var curr=1;
var keyword=_self.UI.inputEle.value;
if(keyword.replace(/\s*/,'')!==''){
_self.searchClick.notify({curr:curr,keyword:keyword});
}
});
this.UI.closeBtn.addEventListener('click',function(event) {
/* Act on the event */
_self.closeClick.notify();
});
// this.UI.inputEle.addEventListener('keyup',function(event) {
// var clickEvent = new MouseEvent("click");
// if (event.which == 13) {
// _self.UI.searchBtn.dispatchEvent(clickEvent);
// }
// clickEvent = null;
// });
bodyNode.addEventListener('click',function(event){
if (event.target.className.indexOf(_self.UI.engineBtnClassName) !== -1 ) {
_self.engineOptChange.notify(event.target.getAttribute('data-engine'));
}
if (event.target.className.indexOf(_self.UI.toPageBtnClassName) !== -1 ) {
_self.toPageClick.notify(parseInt(event.target.getAttribute('data-page'),10));
}
});
};
Viewer.prototype={
refleshEngineLst:function(){
var template='<%for(var i in this){%>'+
'
'+
'<%}%>';
var html=TemplateEngine(template,this.model.engineLst);
this.UI.engineLst.innerHTML = html;
},
updateEngine:function(){
this.UI.menu.textContent = this.model.engine;
},
show:function(){
this.UI.myDiv.style.display = 'block';
this.UI.bgNode.style.display = 'block';
this.UI.myDiv.style.top = '50%';
this.UI.myDiv.style.marginTop = (this.UI.myDiv.clientHeight / 2 * -1) + 'px';
},
reflesh:function(success){
var template="---- by <%this.engine%>.com Search
keyword is '<%this.keyword%>' found '<%this.jsonObj.cursor.resultCount%>' Results
--------------------------------------------------
";
template+='<%for(var i in this.jsonObj.results){%>'+
'
'+
'<%this.jsonObj.results[i].titleNoFormatting%>'+
'
'+
'<%this.jsonObj.results[i].contentNoFormatting%>
'+
'<%}%>';
template+='-------------------------------------------------------------
" <%this.jsonObj.results.length%> " items have been load
';
var html;
if(success===false){
html='出错了......
';
}else{
if(this.model.jsonObj.results.length===0){
html='无搜索结果...换个关键词重新试试?
';
}else{
html=TemplateEngine(template,this.model);
}
}
this.UI.myContent.innerHTML = html;
},
close:function(){
this.UI.myDiv.style.display = 'none';
this.UI.bgNode.style.display = 'none';
this.UI.inputEle.value = '';
},
loading:function(){
this.UI.myContent.innerHTML = '
';
},
refleshPageNavi:function(){
var page={curr:1,totalPage:1,pre:true,next:true,lst:[]};
page.curr=this.model.curr;
page.totalPage=this.model.totalPage>=10?10:this.model.totalPage;
page.pre=page.curr>1?true:false;
page.next=page.totalPage>page.curr?true:false;
for(var i=1;i<=page.totalPage;i++){
page.lst.push(i);
}
var template= '\
\
\
上一页\
<%for(var i in this.lst){%>\
<%this.lst[i]%>\
<%}%>\
\
下一页\
\
';
var html=TemplateEngine(template,page);
this.UI.pagese.innerHTML = html;
}
};
var Controller=function(model,viewer){
this.model=model;
this.viewer=viewer;
var _self=this;
this.viewer.searchClick.attach(function(sender,args){
_self.search(args.curr,args.keyword);
});
this.viewer.closeClick.attach(function(){
_self.close();
});
this.viewer.engineOptChange.attach(function(sender,args){
_self.setEngine(args);
});
this.viewer.toPageClick.attach(function(sender,args){
_self.toPage(args);
});
this.model.requestEvent.attach(function(){
_self.loading();
});
this.model.contentUpdated.attach(function(state){
_self.reflesh(state);
});
};
Controller.prototype={
search:function(curr,args){
this.model.updateCurr(curr);
this.model.updateKeyword(args);
this.model.request();
this.viewer.show();
},
setEngine:function(engine){
this.model.setEngine(engine);
this.viewer.updateEngine();
},
toPage:function(toPageNum){
this.model.updateCurr(toPageNum);
this.model.request();
},
close:function(){
this.model.destory();
this.viewer.close();
},
loading:function(){
this.viewer.loading();
},
reflesh:function(){
this.viewer.reflesh();
this.viewer.refleshPageNavi();
},
refleshEngineLst:function(){
this.viewer.refleshEngineLst();
},
};
function newInit () {
//remove advs
//create search bar
var targetNode = document.querySelector('.find-light-icon').parentNode.parentNode;
if (targetNode.parentNode.lastChild.getAttribute("node-type")=="header-union") {
targetNode.parentNode.lastChild.remove();
}
var wxzSearchBarNode = document.createElement('dd');
wxzSearchBarNode.setAttribute('class','header-wxzbar header-info');
//wxzSearchBarNode.setAttribute('node-type','header-apps');
wxzSearchBarNode.innerHTML =
'\
';
//insert after target node
targetNode.parentNode.insertBefore(wxzSearchBarNode,targetNode.nextSlibing);
//background
var wxzbgNode = document.createElement('div');
wxzbgNode.setAttribute('class','wxz-bg');
wxzbgNode.style.display = 'none';
document.querySelector('body').appendChild(wxzbgNode);
//create display frame
var wxzDialogNode = document.createElement('div');
wxzDialogNode.setAttribute('class','dialog dialog-gray');
wxzDialogNode.setAttribute('id','wxz_myDiv');
wxzDialogNode.setAttribute('style','z-index:99;postion:absolute;');
wxzDialogNode.style.width = window.innerWidth / 3 * 2 + 'px';
wxzDialogNode.style.left = '50%';
wxzDialogNode.style.marginLeft = (-1 * window.innerWidth / 3) + 'px';
wxzDialogNode.innerHTML =
'\
\
\
\
\
\
';
//append to body
document.querySelector('body').appendChild(wxzDialogNode);
var wxzStyleNode = document.createElement('style');
wxzStyleNode.textContent =
'\
.wxz-menu{cursor:pointer; height:100%; display:inline-block; vertical-align:middle;position:relative;}\
.wxz-menu:hover .wxz-menu-content{display:block; z-index:99}\
.wxz-menu:hover .icon-dropdown-arrow{transform:rotate(180);}\
.wxz-menu-option{text-align:center;line-height:30px;cursor:pointer;background:white;color:black;border:1px solid #eff4f8;border-collapse: collapse;}\
#wxz_searchForm{display:inline-block; vertical-align:middle;}\
#wxz_input{padding:0 4px; border: 1px solid #c0d9fe;border-radius: 4px;line-height: 22px;}\
#wxz_searchButton{cursor:pointer; background: #3b8cff;border: 2px solid #3b8cff;color: #f8fbff;border-radius: 6px;}\
.wxz-menu-content{display:none; position:absolute;top:100%;left:0;width:80px;}\
#wxzMenuDisplay{line-height: 40px; display:inline-block; width:40px;}\
.wxz-bg{position: fixed; left: 0px; top: 0px; bottom: 0px; right: 0px; z-index: 50; background: rgb(0, 0, 0); opacity: 0.5;}\
.wxz-content{height: 500px;line-height: 200%;text-align: left;white-space: normal;padding:0 10px;overflow:auto;}\
.wxz-close{margin-right:20px;important;height:20px;cursor:pointer}\
.wxz-next{margin-right:20px;float:right;height:20px;cursor:pointer}\
.wxz-front{margin-right:40px;float:right;height:20px;cursor:pointer}\
.wxz-content a{color:#0066FF!important;font: 14px/1.5 arial,sans-serif!important;}\
';
document.querySelector('head').appendChild(wxzStyleNode);
var bdModel=new BaseModel(['bing','google']);
bdModel.urls.bing='https://cn.bing.com/search?q=';
bdModel.urls.google='https://www.googleapis.com/customsearch/v1element?key=AIzaSyCVAXiUzRYsML1Pv6RwSG1gunmMikTzQqY&rsz=filtered_cse&num=10&hl=en&prettyPrint=true&source=gcsc&gss=.com&sig=bb73d6800fca299b36665ebff4d01037&cx=018177143380893153305:yk0qpgydx_e&cse_tok=AHKYotWVmvV1wohA3g8oNFAm_6cK:1495660148313&q=';
bdModel.toJson.bing=function(html){
var data = { cursor: { estimatedResultCount: 0, resultCount: 0 }, results: [] };
//其中一条结果:
//
//文件名:YFK-RK3368-8189-20150821.rar 文件大小:497.55M 分享者:晨芯FAE 分享时间:2015-8-21 14:07 下载次数:5 ... 登录百度云客户端送2T空间 电脑版
//
pan.baidu.com/wap/link?uk=2923110658&shareid=3468815834&...
//
//http://www.jb51.net/article/49083.htm在JS中解析HTML字符串示例代码:
var rawResultHTML = html.match(/
(.*?)<\/li>/g);
var b_results = document.createElement('ul');
b_results.innerHTML = rawResultHTML;
var b_algo_Arry = Array.prototype.slice.call(b_results.getElementsByClassName('b_algo'));
b_algo_Arry.forEach(function(ele, index) {
var tempResult = {
unescapedUrl: "",
titleNoFormatting: "",
contentNoFormatting: ""
};
tempResult.unescapedUrl = ele.querySelector('h2 a').getAttribute('href');
tempResult.titleNoFormatting = ele.querySelector('h2 a').textContent;
tempResult.contentNoFormatting = ele.querySelector('.b_caption p').textContent;
data.results.push(tempResult);
});
////处理统计结果,
var rawResultCount=(html.match(/"sb_count">([^<]+)/) && html.match(/"sb_count">([^<]+)/)[1]) || '0';
var matchLst=[];
matchLst=rawResultCount.match(/([0-9]{1,3}(,[0-9]{3})+)/g);
if(matchLst!==null){//匹配100,000,111之类的情况
data.cursor.resultCount=matchLst[0].replace(',','');
}else{
matchLst=rawResultCount.match(/\d+/g);
if(matchLst!==null){//匹配10 个结果之类的情况,以及1-11,共11个的情况
data.cursor.resultCount=matchLst.pop();
}else{//匹配无的情况
data.cursor.resultCount=0;
}
}
data.cursor.resultCount = parseInt( data.cursor.resultCount.toString(),10);
data.cursor.estimatedResultCount = data.cursor.resultCount;
return data;
};
bdModel.toJson.google=function(responseText){
var data=JSON.parse(responseText);
data.cursor.resultCount=parseInt(data.cursor.resultCount.split(',').join(''));
return data;
};
bdModel.compileUrl.bing=function(_self){
//return 'https://cn.bing.com/search?q=lala+site%3Apan.baidu.com&first=10';
return _self.urls.bing + _self.keyword + '+site%3Apan.baidu.com' + '&first=' + (_self.curr-1)*10;
};
bdModel.compileUrl.google=function(_self){
return _self.urls.google + _self.keyword + '&start=' + (_self.curr-1)*10;
};
var bdView=new Viewer(bdModel,{
inputEle:document.querySelector('#wxz_input'),
searchBtn:document.querySelector('#wxz_searchButton'),
closeBtn:document.querySelector('#wxz_closeButton'),
myDiv:document.querySelector('#wxz_myDiv'),
myContent:document.querySelector('.wxz-content'),
menu:document.querySelector('#wxzMenuDisplay'),
engineBtnClassName:'wxz-menu-option',
engineLst:document.querySelector('#wxz_engineLst'),
pagese:document.querySelector('#wxz-pagese'),
toPageBtnClassName:'page-number',
bgNode:wxzbgNode
});
var bdController=new Controller(bdModel,bdView);
bdController.refleshEngineLst();
//这里写的不好
var tempStr = '';
var tempObj;
tempStr = window.localStorage.getItem('wxzYunpanSearcher');
if (tempStr) {
tempObj = JSON.parse(tempStr);
bdController.setEngine(tempObj.engine);
}
else{
bdController.setEngine('bing');
}
}
var counter = 0;
var
t = window.setInterval(function() { //百度云把一些内容放到后面加载,因此我设置了一个延时循环,每隔200ms选择一下所需的元素,当所需的元素存在时,开始脚本,同时停止延时循环
if (document.querySelector(".find-light-icon") !== null) {
window.clearInterval(t);
newInit();
}
else{
if(counter < 100){
console.log('waiting');
counter ++;
}
else{
window.clearInterval(t);
console.log('out of time');
}
}
}, 200);