// ==UserScript==
// @name 百度云插件+APIKey
// @namespace
// @version 4.3 beta
// @description 在百度云网盘的页面添加一个搜索框,调用谷歌的搜索API搜索所有公开分享文件// To add a search frame that calls the google cse api for searching some public shared files in BaiduYun cloud netdisk.
// @require http://code.jquery.com/jquery-2.1.1.min.js
// @description For more imformation,please email me at wang0xinzhe@gmail.com.
// @include http://pan.baidu.com/disk/*
// @include https://pan.baidu.com/disk/*
// @include https://yun.baidu.com/#from=share_yun_logo/
// @include http://yun.baidu.com/#from=share_yun_logo/
// @grant GM_xmlhttpRequest
// @run-at document-end
// @copyright 2014,04,20 __By Wang Hsin-che
// @downloadURL none
// ==/UserScript==
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// my custom jquery draggable plugin//////////////////////////////////////////////////////////////////////////////
// by Wang Hsin-che @ 2014.07
// version : 0.1
// usage: $("#element").draggable({handle:"#childElement"}); or $("#element").draggable();
(function($) {
$.fn.draggable = function(option) {
var x0, y0, ele, setting;
ele = this; //save the selector
setting = $.extend({
handle: this.selector
}, option); //deal with the "handle" parameter,default is the selector
ele.css({
position: "absolute"
}); //make sure that the "postion" is "absolute"
$(setting.handle).bind('mousedown', function(e0) {
$(setting.handle).css({
cursor: "move"
}); //set the appearance of cursor
x0 = ele.offset().left - e0.pageX; //*1
y0 = ele.offset().top - e0.pageY; //*1
$(document).bind('mousemove', function(e1) { //bind the mousemove event, caution:this event must be bind to "document"
$(document).bind('mouseup', function() { //when the mouse up,unbind the mousemove event
$(document).unbind('mousemove');
$(setting.handle).css({
cursor: "auto"
});
});
ele.css({
left: x0 + e1.pageX,
top: y0 + e1.pageY
}); //this expression and the expression of *1 equal to "ele.origin_offset+mouse.current_offset-mouse.origin_offset"
});
});
return this;
};
}(jQuery));
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////定义
var SearchObject = (function($, replaceEle) {
var
url = 'https://www.googleapis.com/customsearch/v1element?key=AIzaSyCVAXiUzRYsML1Pv6RwSG1gunmMikTzQqY&rsz=filtered_cse&num=10&hl=en&prettyPrint=true&source=gcsc&gss=.com&sig=ee93f9aae9c9e9dba5eea831d506e69a&cx=018177143380893153305:yk0qpgydx_e&q=',
keyword = '',
//搜索框的html
html_1 = '
\
',
//显示页面的html
html_2 = '\
\
',
//插入的css
cssText = '\
\
';
return {
searchClear: function() {
$('#wxz_myDiv').slideUp();
$('#wxz_input').val('');
keyword = '';
$('.wxz-content').empty(); //清空原来的内容
console.log('clear');
},
search_2: function(keyword, startIndex) {
console.log('search');
var self = this;
if (keyword === '') {
console.log('fail');
return 0;
}
//显示loading条
$('.wxz-content').html('
');
$('#wxz_myDiv').slideDown();
GM_xmlhttpRequest({
method: "GET",
url: url + keyword + '&start=' + startIndex,
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) {
var
data = JSON.parse(response.responseText),
showList,
tempNode,
totalPage = 1,
totalResults = parseInt(data.cursor.estimatedResultCount, 10);
//把json数据转为html,存入缓存showlist
if (parseInt(data.cursor.resultCount, 10) === 0) {
$('.wxz-content').html('无搜索结果...换个关键词重新试试?
');
} //无结果时提示
else {
totalPage = parseInt((totalResults - 1) / 10, 10) + 1 > 10 ? 10 : parseInt((totalResults - 1) / 10, 10) + 1; //坑比,google自定义搜索只提供最大10页(每页10条)
showList = "---- by Google Custome Search
keyword is '" + keyword + "' found '" + data.cursor.resultCount + "' Results
--------------------------------------------------
";
$.each(data.results, function(index, element) {
tempNode = '' + element.titleNoFormatting + '';
showList += '
' + tempNode + '
';
showList += '' + element.contentNoFormatting + '
';
});
showList += '-------------------------------------------------------------
"' + data.results.length + '" items have been load
About me
';
$('.wxz-content').html(showList); //替换原来内容,之所以用了showlist作为缓存是为了提升速度
$('.wxz-content').scrollTop(0); //滚到顶端
}
self.pageBar(parseInt(startIndex / 10, 10) + 1, totalPage);
data = null;
tempNode = null;
totalPage = null;
totalResults = null;
showList = null;
},
onerror: function() {
$('.wxz-content').html('出错了......google登陆不了?!
');
console.log("error");
return 0;
}
});
},
pageBar: function(page, totalPage) {
var
html = '\
\
',
pageNodeHtml = '',
i, c,
self = this,
startPage = 10 * parseInt((page - 1) / 10, 10) + 1;
$('#wxz-pagese').replaceWith(html);
c = $('#wxz-pagese').find('.page-content').eq(0);
for (i = 0; i < 10; i++) {
if (i + startPage > totalPage) {
break;
}
if (i + startPage === page) {
$(pageNodeHtml).html(i + startPage).addClass('global-disabled').appendTo(c);
} else {
$(pageNodeHtml).html(i + startPage)
.bind('click', {
msg: i + startPage
}, function(e) {
self.search_2(keyword, (e.data.msg - 1) * 10);
})
.appendTo(c);
}
}
if (page <= 1) {
$('#wxz-pagese').find('.page-prev').eq(0).addClass('global-disabled');
} else {
$('#wxz-pagese').find('.page-prev').eq(0).bind('click', {
msg: page - 1
}, function(event) {
self.search_2(keyword, (event.data.msg - 1) * 10);
});
}
if (page >= totalPage) {
$('#wxz-pagese').find('.page-next').eq(0).addClass('global-disabled');
} else {
$('#wxz-pagese').find('.page-next').eq(0).bind('click', {
msg: page + 1
}, function(event) {
self.search_2(keyword, (event.data.msg - 1) * 10);
});
}
html = null;
pageNodeHtml = null;
i = null;
c = null;
startPage = null;
},
setUI: function() {
//根据屏幕设置div的大小位置
var
myDivHeight = window.innerHeight / 3 * 2,
myDivLeft = -window.innerWidth / 6,
myDivTop = window.innerHeight / 8;
//应用大小和页面
$('.wxz-content').css({
height: myDivHeight
});
$('.wxz-myDiv').css({
top: myDivTop,
left: myDivLeft
});
//应用拖拽
$("#wxz_myDiv").draggable({
handle: "#wxz_myDiv_title"
});
myDivHeight = null;
myDivLeft = null;
myDivTop = null;
},
replaceAndInsert: function() {
$(replaceEle).replaceWith(html_1); //搜索栏替换了广告
$('body').append(html_2);
$('head:first').append(cssText); //插入css
},
bind_2: function() {
var self = this;
//绑定各种函数
$('#wxz_searchButton').click(function() {
keyword = $('#wxz_input').val();
self.search_2(keyword, 0);
});
$('#wxz_closeButton').click(function() {
self.searchClear();
});
$('#wxz_input').keyup(function(event) {
if (event.which == 13) {
$('#wxz_searchButton').trigger('click');
}
});
},
init: function() {
var
self = this,
t = window.setInterval(function() { //百度云把一些内容放到后面加载,因此我设置了一个延时循环,每隔100ms选择一下所需的元素,当所需的元素存在时,开始脚本,同时停止延时循环
if ($(replaceEle).length > 0) {
window.clearInterval(t);
self.replaceAndInsert();
self.setUI();
self.bind_2();
}
console.log('waiting');
}, 100);
},
};
}(jQuery, '#top_menu_other'));
//启动
SearchObject.init();