Skip to content

Instantly share code, notes, and snippets.

@suxiaogang
Created September 7, 2013 10:30
Show Gist options
  • Save suxiaogang/6474510 to your computer and use it in GitHub Desktop.
Save suxiaogang/6474510 to your computer and use it in GitHub Desktop.
douban_baidupan_inject.js
//条目的讨论页面,豆列页面不显示搜索结果
var currentURL = document.URL;
if(currentURL.indexOf('discussion') != -1 || currentURL.indexOf('doulist') != -1 ){
return;
} else {
inject();
}
function inject(){
var title = $('html head title').text();
var keyword = title.replace( '(豆瓣)', '' ).trim();
var dck = encodeURIComponent(keyword);
var imgURL = chrome.extension.getURL("static/icons/icon_128.png");
var html_title = '<div id="dbbd" class="da3" style="margin-bottom:0px;padding-bottom:1px;background-color:#E9F3FA;">'
+ '<dl><dt style="display:inline;font-size:11px;color:#999">'
+ '<img id="toggleIMG" src="'+imgURL+'" style="margin-bottom:1px;margin-right:5px;width:16px;/>'
+ '<b style="color:#888">' + keyword + '</b> 的搜索结果· · ·</dt> ['
+ '<a href="http://www.baidu.com/s?wd='+dck+'+site%3Apan.baidu.com" target="_blank">全部</a>'
+ ']<img style="float:right;margin-top:4px;cursor:pointer;opacity:0.3;" id="toggleIcon" src="http://img3.douban.com/pics/add-doulist.gif" title="试试其他关键字?">'
+ '<div id="baidu-search" style="display:none">'
+ ' <input id="query-keywords" type="text">'
+ ' <img id="searchIcon" src="http://img3.douban.com/pics/icon/bn_srh_1.png" style="cursor:pointer">'
+ '</div>'
+ '</dl></div>';
var html_body_start = '<div class="indent" id="db-doulist-section" style="padding-left:5px;padding-right:5px;padding-bottom:8px;border:1px #F4F4EC solid;"><ul class="bs bdresult">';
var url='http://www.baidu.com/s?wd='+dck+'+site%3Apan.baidu.com';
$.ajax({
url:url,
dataType: "html",
success: function(data){
for(var i = 1; i < 6; i ++){
var content = "#content_left table#" + i + " tr td.f div.c-abstract";
var tempTitle = $(content, data).text().replace("文件名:", "");
var tempURL = $("#content_left table#" + i + " tr td.f h3.t a", data).attr("href");
//搜索结果不为空时,加载显示...
if (tempTitle != "") {
$("ul.bdresult").append('<li><span class="badge badge-error">'+ i +'</span><a href='+tempURL+' target="_blank">' + tempTitle + '</li>');
} else if (tempTitle == "" && i == 1) {
$("ul.bdresult").append('<li>哇哦~,可能是该资源过于冷门,什么都没找到呀...</li>');
return;
} else {
$("ul.bdresult").append('<li style="text-align:center">未找到更多搜索结果</li>');
return;
}
}
},
error: function(responseData, textStatus, errorThrown) {
$("ul.bdresult").append('<li>未能找到的' + keyword +'相关结果</li>');
}
});
var html_body_end = '</ul></div>';
$('.aside').prepend( html_title + html_body_start + html_body_end);
var toggle_more_button = document.getElementById("toggleIcon");
toggle_more_button.addEventListener("click", function() {
$('#baidu-search').fadeToggle("fast");
}, false);
var submit_search = document.getElementById("searchIcon");
submit_search.addEventListener("click", function() {
var queryWords = $('#query-keywords').val();
if(queryWords.trim() == ""){
alert("请输入搜索关键字哦^_^");
}else{
url_final = 'http://www.baidu.com/s?wd=' + queryWords + '+site:pan.baidu.com';
window.open(url_final);
}
}, false);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment