Bing图

百度首页变成bing

2014/09/09のページです。最新版はこちら。

このスクリプトの質問や評価の投稿はこちら通報はこちらへお寄せください。
// ==UserScript==
// @name        Bing图
// @namespace   firefox
// @author      林小鹿吧
// @contributor twhdomain
// @include 	http://www.baidu.com/
// @include 	http://www.baidu.com/home*
// @include 	http://www.baidu.com/?tn=*
// @include 	http://www.baidu.com/index.php*
// @version     3.1
// @run-at 	document-end
// @require http://code.jquery.com/jquery-latest.min.js
// @grant 	GM_xmlhttpRequest
// @grant	GM_registerMenuCommand
// @description 百度首页变成bing
// ==/UserScript==
//2014/05/29  	兼容新版百度首页
//2014/06/10 	添加一个初始化命令
var _bing = {
		name : '_baiduLogo',
		X : 0,
		Y : 0,
		current:{idx:0,url:''},
		formerly:{idx:0,url:''},
		idx:0,
		defultOpacity:40
	};
var loading = false;

function save(_bing){
	localStorage._bing = JSON.stringify(_bing);
}
function load(){
	localStorage._bing && (_bing = JSON.parse(localStorage._bing));
	return _bing;
}
function getIdx(type) {
	var idx = 0;
	"prev" == type ? idx = _bing.current.idx + 1 : "next" == type && (idx = _bing.current.idx - 1);
	return idx;
}
function loadResponse(e){
	console.log(e);

	try{
		var d = JSON.parse(e.responseText);
		var image = d.images[0];
		var imageUrl = image.url.replace(new RegExp('^(\w{3,5}:\/\/[^/]*)'), bing_uri);
		"/" == imageUrl[0] ? imageUrl=bing_uri+imageUrl : null;
		var	video = image.vid ? image.vid : null;
		//console.log(image.copyrightlink)
		//console.log(imageUrl)
		$('#sh_cp').attr('href',image.copyrightlink.replace(new RegExp('.*\\?q=([^&]*).*', 'i'), 's?wd=$1')).attr('title',image.copyright);
		changMsg(image.msg[0].title,image.msg[0].link,image.msg[0].text);
	}catch(error){
		console.log(error);return;
	}
	if(_bing.current.url != imageUrl){
		changeBg(imageUrl,video);
	} else{
		openLoadingLock();
	}
}
function prevImg(){
	loadBgJson(getIdx('prev'),loadResponse);
}
function nextImg(){
	loadBgJson(getIdx('next'),loadResponse);
}
function openLoadingLock(){
	loading = false;
	$("#sh_igl").attr('title','上一页');
	$("#sh_igr").attr('title','下一页');
}
function downloadImg(){
	var imageUrl = _bing.current.url;
	_window.open(imageUrl);
}
function changMsg(title,link,text){
	$('#hp_bottomCell #hp_pgm h3').text(title);
	$('#hp_bottomCell #hp_pgm a').attr('href',bing_uri + link).text(text);
}
function changeBg(imageUrl,video){
		var bg = new Image(),
		time = 2000,
		showV,
		hideV,
		a = $("#bg1").css("background-image"),
		b = $("#bg2").css("background-image");
	"none" == a && "none" == b && (time = 500, showV = $("#bg1"), hideV = $("#bg2"), showV.css("opacity", 0));
	"none" != a && "none" == b && (console.log('jin ru 2'),showV = $("#bg2"), hideV = $("#bg1"),showV.css("opacity", 0));
	"none" != a && "none" != b && (a = $("#bg1").css("opacity"), $("#bg2").css("opacity"), showV = a < 1 ? $("#bg1") : $("#bg2"), hideV = "bg1" == showV.attr("id") ? $("#bg2") : $("#bg1"));

	bg.addEventListener("load",function(e){
		var v = $('#bg3 video');
		if(video){
			v.attr('loop',video.loop).attr('src',bing_uri + video.codecs[0][1]);
			$('#bg3,#bg3 video').fadeIn(time)
		}else{
			$('#bg3,#bg3 video').fadeOut(time,function(){v.attr('loop',"").attr('src','')});
		}
		video==null&&showV.css("background-image",'url('+imageUrl+')');
		setTimeout(openLoadingLock,time/2);
		hideV.animate({opacity: '0'},time);  
		showV.animate({opacity: '1'},time);
	})
	bg.src = imageUrl;
	_bing.formerly = _bing.current;
	_bing.current = {idx:_bing.idx,url:imageUrl};
	save(_bing);
}

function loadBgJson(i/*1-7 | 0为当天 -1为明天*/,callback){
	if(loading == true){console.error('is loading '+_bing.idx)};
	if(i >= -1 && i <= 7 && loading == false){
		loading = true;
		$("#sh_igl,#sh_igr").attr('title','加载中...');
		_bing.idx = i;
		var url = bing_uri + '/HPImageArchive.aspx?format=js&idx=' + i + '&n=1&nc=' + new Date().getTime() + '&pid=hp&video=1';
		var opt = {
			method:'GET',
			url:url,
			onload:callback,
			onerror:function(e){console.log(e);ajax(opt);openLoadingLock()},
			onabort:function(e){console.log(e);ajax(opt);openLoadingLock()}
		}
		switch(i){
			case 7:{$('#sh_igl').css('opacity',0.3);break;}
			case -1:{$('#sh_igr').css('opacity',0.3);break;}
			case 6:{$('#sh_igl').css('opacity',1);break;}
			case 0:{$('#sh_igr').css('opacity',1);break;}
		}
		ajax(opt);
	}
}
function changHasBgStyle(){
	if(($('#content').attr('class')).indexOf('opacity')==-1)$("#content").addClass("s-skin-user s-skin-hasbg s-skin-dark opacity-40 white-logo");
	//新版本
	if(!$('#content').hasClass('s-skin-user')){
		$('#content').addClass('s-skin-user s-skin-hasbg s-skin-dark opacity-40 white-logo s-opacity-'+_bing.defultOpacity);
	}
}

function bing(){
	var clientHeight=document.body.clientHeight,
		clientWidth=document.body.clientWidth,
		width=clientHeight*clientWidth/720,
		height=clientHeight*clientWidth/1280,
		left=0-(width-clientWidth)/2,
		top=0-(height-clientHeight)/2;
	var css = 'div.bing_bg{background-attachment: fixed;background-position: center 0;background-repeat: no-repeat;background-size: cover;height: 100%;left: 0;position: absolute;top: 0;width: 100%;}\
	.bing_video{overflow:hidden;position:fixed;width:'+width+'px;height:'+height+'px;left:'+left+'px;top:'+top+'px;}\
	div#sh_rdiv{position:fixed;right:15px;bottom:15px;}div#sh_rdiv A{position:relative;width:32px;height:32px;margin:0 5px;float:left}\
	a[id^="sh_"]{background:url(http://cn.bing.com/s/a/hpc12.png) no-repeat;}\
	a#sh_igl{background-position:-192px -53px}\
	a#sh_igr{background-position: -160px -53px}\
	a#sh_cp{background-position: -64px -85px}\
	a#sh_igd{background-position: 0 -53px}\
#s_user_name_menu > DIV A {background:rgba(0,0,0,0.5)!important;color:#fff !important;}\
#s_user_name_menu > DIV A:hover {background:rgba(0,0,0,0.7)!important;}\
//.show-city {opacity:0 !important;margin-left:-30px !important;}\
//.show-city:hover {opacity:1 !important;margin-left:0px !important;}\
.weather-setting-content {background:rgba(0,0,0,0.7)!important;width:100% !important;}\
.weather-setting-content SPAN,.weather-setting-content A {color:#EEE !important;}\
.everyday-mod P {color:#FFF !important;}\
#nv a {color:#FFFFFF !important;text-shadow:2px 2px 5px rgba(0,0,0,0.8) !important;text-decoration:none !important;}\
#kw1 {background:rgba(288,288,288,0.3)!important;border-radius:0px !important;margin-left:-2px !Important;}\
#kw1:hover {background:rgba(288,288,288,0.7)!important;}\
#kw1:focus {background:rgba(288,288,288,0.7)!important;}\
#su1,.btn_wr {background:rgba(288,288,288,0.3)!important;border-radius:0px !important;margin-left:2px !Important;}\
#su1:hover,.btn_wr:hover {background:rgba(288,288,288,0.6)!important;}\
.bdsug {background:rgba(288,288,288,0.8)!important;border-radius:0px !important;}\
#s_content_1{background:rgba(20,20,20,0.5)!important;}\
#s_content_1 *{color:#eee!important;}\
.s-nplus-inner:hover{text-shadow:0px 0px 4px black!important;}\
#s_nav_opts *:hover,.s-nadd-opt:hover,.s-nadd-nav:hover,#s_nav_edit_back:hover,.addform-add-btn:hover,.addform-inner-cbx-container:hover{text-shadow:0px 0px 2px #0079F5!important;}\
.s-sug .mo{background:rgba(0,0,0,0.85)!important;}\
.s-sug,.s-sug *{background:transparent!important;}\
#s_nadd_btn_add{opacity:.8;}\
#s_nadd_btn_add:hover{opacity:.9;}\
#s_content_2{background:rgba(20,20,20,0.5)!important;}\
#s_content_2 .words-item-content,.s-opacity-border1-bottom *,.topic-item-content{color:#eee!important;}\
.words-title:hover{text-shadow:0px 0px 1px grey!important;}\
.viewed{text-shadow:0px 0px 1px blue!important;}\
.suggest-main{background:rgba(250,250,250,0.9)!important;}\
.s-opacity-white-background,#s_xmancard_music>*>*>*,#s_content_3{background:transparent!important;}\
.s-xman-baidu-fm{background:rgba(20,20,20,0.5)!important;}\
#s_content_3 *{color:#eee!important;}\
.s-xman-baidu-fm .lrc-panel .lrc-wrap .current {text-shadow:0px 0px 1px yellow!important;}\
.title-change-words:hover{text-shadow:0px 0px 2px #0079F5!important;}\
.change-panel a:hover{text-shadow:0px 0px 1px yellow!important;}\
#s_content_7{background:rgba(20,20,20,0.5)!important;}\
#s_content_7 *{color:#eee!important;}\
.s-xz-btn-set:hover{text-shadow:0px 0px 2px #0079F5!important;}\
#s_content_8{background:rgba(20,20,20,0.5)!important;}\
.banner *,.s-soccer-hot-title,.s-soccer-mngr .mngr-cards * {color:#eee!important;}\
.s-soccer-top-link a.s-soccer-hot-title:hover, .s-soccer-hot-link a.s-soccer-hot-title:hover,.s-static-link a:hover{text-shadow:0px 0px 1px pink!important;}\
.option{background:rgba(120,120,120,0.7)!important;}\
.option:hover{background:rgba(56,156,255,0.7)!important;}\
.mngr-btn:hover,.jczq-link:hover{text-shadow:0px 0px 2px #0079F5!important;}\
#s_content_6{background:rgba(20,20,20,0.5)!important;}\
.words-item-content{color:#eee!important;}\
.words-item-content:hover,.change-btn:hover{text-shadow:0px 0px 1px yellow!important;}\
.change-btn:hover{text-shadow:0px 0px 2px #0079F5!important;}\
#s_content_5{background:rgba(20,20,20,0.5)!important;}\
#s_content_5 *{color:#eee!important;}\
#s_content_5 a:hover{text-shadow:0px 0px 1px yellow!important;}\
#s_content_11{background:rgba(20,20,20,0.5)!important;}\
#s_content_11 *{color:#eee!important;}\
.s-opacity-background1{background:rgba(20,20,20,0.3)!important;}\
.chat-input{background:rgba(20,20,20,0.3)!important;}\
.question-input>table *{background:rgba(20,20,20,0.2)!important;}\
.s-gk-info-link:hover{text-shadow:0px 0px 2px #0079F5!important;}\
.s-opacity-70 .s-opacity-white-background,.s-gk-chat-window .chat-content{background:none!important;}\
#s_content_10{background:rgba(20,20,20,0.5)!important;}\
#s_content_10 *{color:#eee!important;}\
.s-lottery-hot-title:hover{text-shadow:0px 0px 1px yellow!important;}\
#s_lottery_prizes_info:hover{background:rgba(120,120,120,0.4)!important;}\
.s-lottery-user .user-showall, .user-balance .text:hover , .user-balance{background:rgba(120,120,120,0.9)!important;}\
.s-lottery-user-on *{background:transparent!important;}\
.user-home:hover{text-shadow:0px 0px 2px #0079F5!important;}\
#s_content_12{background:rgba(20,20,20,0.5)!important;}\
.tags *,.stock-news *,.line{color:#eee!important;}\
.lineshadow{background:rgba(250,250,250,0.1)!important;}\
.s-stock-hot-title:hover{text-shadow:0px 0px 1px yellow!important;}\
.refresh-btn:hover{text-shadow:0px 0px 2px #0079F5!important;}\
.ac_results {background:rgba(250,250,250,0.8)!important;}\
.ac_over{background:rgba(20,50,150,0.8)!important;}\
.add-btn{opacity:.8;}\
.add-btn:hover{opacity:.9;}\
#s_content_13{background:rgba(20,20,20,0.5)!important;}\
#s_content_13 *{color:#eee!important;}\
.back:hover,.gift:hover,.mygame:hover{text-shadow:0px 0px 2px #0079F5!important;}\
#s_content_99,#s_content_99>*>*{background:transparent!important;}\
#s_content_99>*{background:rgba(20,20,20,0.5)!important;}\
.subscribe{background:rgba(20,20,20,0.9)!important;}\
.mod-title{background:rgba(250,250,250,0.7)!important;}\
#s_hide_allmods:hover{text-shadow:0px 0px 2px #0079F5!important;}\
#nv a {text-shadow:none !important;}\
#s_main{opacity: 0!important;-moz-transition: opacity .4s ease-in-out;-webkit-transition: opacity .25s ease-in-out;-o-transition: opacity .4s ease-in-out;}\
#s_main:hover {opacity:1 !important;}\
a.s-treasure,a.s-set-page{display: none!important;}\
//#lg>img{opacity:.5!important;height:30%!important;width:30%!important;}\
#lg{background:url(http://su.bdimg.com/static/superplus/img/logo_white.png?v=md5) no-repeat center / auto 100px !important;opacity:.6!important;}\
#lg>img{opacity:0!important;}\
	#hp_bottomCell{position:fixed;left:0px;bottom:15px;}\
	#hp_pgm{margin-left:20px;bottom:0px;}\
	#hp_pgm h3{text-align:left;color: rgb(255, 255, 255);font-size: 16px;font-weight: normal;margin: 0;padding: 0;}\
	#hp_pgm a{float:left;text-align:left;color: rgb(255, 255, 255);font-size: 14px;font-weight: normal;margin-top: 5px;padding: 0;text-decoration:none;}'
	;
	$("body").before('<style type="text/css">'+css+'</style>');
	$("#content").append('<div id="bg1" class="bing_bg" style="z-index:-5"/><div id="bg2" class="bing_bg" style="z-index:-5"/><div id="bg3" class="bing_bg" style="z-index:-4"><video autoplay="true" class="bing_video"/></div>');
	$("#bg2,#bg1").css("background-color",'rgba(89, 83, 87, 0)');
	if($("#bg2").css("background-image")!='none'){
		$(".s-skin-container")&&$(".s-skin-container").remove();
	}
	$('body').append('<div id="hp_bottomCell"><div id="hp_pgm"><h3></h3><a  target="_blank"></a></div><div id="sh_rdiv"><a id="sh_igl" href="javascript:void(0)" title="上一页"/><a id="sh_igr" href="javascript:void(0)" title="下一页"/><a id="sh_cp" href="javascript:void(0)" target="_blank"/><a id="sh_igd" href="javascript:void(0)" title="下载壁纸"/></div></div>');
	(document.querySelector('.btn_wr')).addEventListener('click', function(event) {
		 (document.querySelector('#form1')).submit();
	});
	(document.getElementById('sh_rdiv')).addEventListener('click', function(event) {
		switch (event.target.id) {
			case 'sh_igl':prevImg();break;
			case 'sh_igr':nextImg();break;
			case 'sh_igd':downloadImg();break;
		}
	})
}

try{
	if(document.documentElement.hasAttribute('xmlns')){
		var bing_uri = 'http://cn.bing.com';
		var _window = typeof unsafeWindow != 'undefined'?unsafeWindow:window;
		//var $ = _window.$;
		var ajax = GM_xmlhttpRequest;
		bing();
		changHasBgStyle();
		_bing = load();
		if(_bing.current.url){
			$("#bg1").css("background-image",'url('+_bing.current.url+')');
		}
		loadBgJson(0,loadResponse);//_bing.idx
		
	}
}catch(ee){
	console.log(ee)
}
function initBing () {
	var _init_bing = {
		name : '_baiduLogo',
		X : 0,
		Y : 0,
		current:{idx:0,url:''},
		formerly:{idx:0,url:''},
		idx:0,
		defultOpacity:40
	};
	save(_init_bing);
	_window.location.href=_window.location.href;
}
GM_registerMenuCommand("Bing图初始化",initBing);