Bing图

百度首页变成bing

  1. // ==UserScript==
  2. // @name Bing图
  3. // @namespace firefox
  4. // @author 林小鹿吧
  5. // @include *://www.baidu.com/
  6. // @include *://www.baidu.com/home*
  7. // @include *://www.baidu.com/?tn=*
  8. // @include *://www.baidu.com/index.php*
  9. // @version 1.4
  10. // @run-at document-end
  11. // @require http://code.jquery.com/jquery-latest.min.js
  12. // @grant GM_xmlhttpRequest
  13. // @grant GM_registerMenuCommand
  14. // @description 百度首页变成bing
  15. // ==/UserScript==
  16. //2014/05/29 兼容新版百度首页
  17. //2014/06/10 添加一个初始化命令
  18. var _bing = {
  19. name : '_baiduLogo',
  20. X : 0,
  21. Y : 0,
  22. current:{idx:0,url:''},
  23. formerly:{idx:0,url:''},
  24. idx:0,
  25. defultOpacity:40
  26. };
  27. var loading = false;loading
  28.  
  29. function save(_bing){
  30. localStorage._bing = JSON.stringify(_bing);
  31. }
  32. function load(){
  33. localStorage._bing && (_bing = JSON.parse(localStorage._bing));
  34. return _bing;
  35. }
  36. function getIdx(type) {
  37. var idx = 0;
  38. "prev" == type ? idx = _bing.current.idx + 1 : "next" == type && (idx = _bing.current.idx - 1);
  39. return idx;
  40. }
  41. function loadResponse(e){
  42. console.log(e);
  43.  
  44. try{
  45. var d = JSON.parse(e.responseText);
  46. var image = d.images[0];
  47. var imageUrl = image.url.replace(new RegExp('^(\w{3,5}:\/\/[^/]*)'), bing_uri);
  48. "/" == imageUrl[0] ? imageUrl=bing_uri+imageUrl : null;
  49. var video = image.vid ? image.vid : null;
  50. //console.log(image.copyrightlink)
  51. //console.log(imageUrl)
  52. $('#sh_cp').attr('href',image.copyrightlink.replace(new RegExp('.*\\?q=([^&]*).*', 'i'), 's?wd=$1')).attr('title',image.copyright);
  53. changMsg(image.msg[0].title,image.msg[0].link,image.msg[0].text);
  54. }catch(error){
  55. console.log(error);return;
  56. }
  57. if(_bing.current.url != imageUrl){
  58. changeBg(imageUrl,video);
  59. } else{
  60. openLoadingLock();
  61. }
  62. }
  63. function prevImg(){
  64. loadBgJson(getIdx('prev'),loadResponse);
  65. }
  66. function nextImg(){
  67. loadBgJson(getIdx('next'),loadResponse);
  68. }
  69. function openLoadingLock(){
  70. loading = false;
  71. $("#sh_igl").attr('title','上一页');
  72. $("#sh_igr").attr('title','下一页');
  73. }
  74. function downloadImg(){
  75. var imageUrl = _bing.current.url;
  76. _window.open(imageUrl);
  77. }
  78. function changMsg(title,link,text){
  79. $('#hp_bottomCell #hp_pgm h3').text(title);
  80. $('#hp_bottomCell #hp_pgm a').attr('href',bing_uri + link).text(text);
  81. }
  82. function changeBg(imageUrl,video){
  83. var bg = new Image(),
  84. time = 2000,
  85. showV,
  86. hideV,
  87. a = $("#bg1").css("background-image"),
  88. b = $("#bg2").css("background-image");
  89. "none" == a && "none" == b && (time = 500, showV = $("#bg1"), hideV = $("#bg2"), showV.css("opacity", 0));
  90. "none" != a && "none" == b && (console.log('jin ru 2'),showV = $("#bg2"), hideV = $("#bg1"),showV.css("opacity", 0));
  91. "none" != a && "none" != b && (a = $("#bg1").css("opacity"), $("#bg2").css("opacity"), showV = a < 1 ? $("#bg1") : $("#bg2"), hideV = "bg1" == showV.attr("id") ? $("#bg2") : $("#bg1"));
  92.  
  93. bg.addEventListener("load",function(e){
  94. var v = $('#bg3 video');
  95. if(video){
  96. v.attr('loop',video.loop).attr('src',bing_uri + video.codecs[0][1]);
  97. $('#bg3,#bg3 video').fadeIn(time)
  98. }else{
  99. $('#bg3,#bg3 video').fadeOut(time,function(){v.attr('loop',"").attr('src','')});
  100. }
  101. video==null&&showV.css("background-image",'url('+imageUrl+')');
  102. setTimeout(openLoadingLock,time/2);
  103. hideV.animate({opacity: '0'},time);
  104. showV.animate({opacity: '1'},time);
  105. })
  106. bg.src = imageUrl;
  107. _bing.formerly = _bing.current;
  108. _bing.current = {idx:_bing.idx,url:imageUrl};
  109. save(_bing);
  110. }
  111.  
  112. function loadBgJson(i/*1-7 | 0为当天 -1为明天*/,callback){
  113. if(loading == true){console.error('is loading '+_bing.idx)};
  114. if(i >= -1 && i <= 7 && loading == false){
  115. loading = true;
  116. $("#sh_igl,#sh_igr").attr('title','加载中...');
  117. _bing.idx = i;
  118. var url = bing_uri + '/HPImageArchive.aspx?format=js&idx=' + i + '&n=1&nc=' + new Date().getTime() + '&pid=hp&video=1';
  119. var opt = {
  120. method:'GET',
  121. url:url,
  122. onload:callback,
  123. onerror:function(e){console.log(e);ajax(opt);openLoadingLock()},
  124. onabort:function(e){console.log(e);ajax(opt);openLoadingLock()}
  125. }
  126. switch(i){
  127. case 7:{$('#sh_igl').css('opacity',0.3);break;}
  128. case -1:{$('#sh_igr').css('opacity',0.3);break;}
  129. case 6:{$('#sh_igl').css('opacity',1);break;}
  130. case 0:{$('#sh_igr').css('opacity',1);break;}
  131. }
  132. ajax(opt);
  133. }
  134. }
  135. function changHasBgStyle(){
  136. if(($('#head').attr('class')).indexOf('opacity')==-1)$("#head").addClass("s-skin-user s-skin-hasbg s-skin-dark opacity-40 white-logo");
  137. //新版本
  138. if(!$('#head').hasClass('s-skin-user')){
  139. $('#head').addClass('s-skin-user s-skin-hasbg s-skin-dark opacity-40 white-logo s-opacity-'+_bing.defultOpacity);
  140. }
  141. }
  142.  
  143. function bing(){
  144. var clientHeight=document.body.clientHeight,
  145. clientWidth=document.body.clientWidth,
  146. width=clientHeight*clientWidth/720,
  147. height=clientHeight*clientWidth/1280,
  148. left=0-(width-clientWidth)/2,
  149. top=0-(height-clientHeight)/2;
  150. var css = 'body{overflow:scroll;}\
  151. div.bing_bg{background-attachment: scroll;background-position: center 0;background-repeat: repeat-y;background-size: cover;height: 100%;left: 0;position: absolute;top: 0;width: 100%;}\
  152. .bing_video{position:fixed;width:'+width+'px;height:'+height+'px;left:'+left+'px;top:'+top+'px;}\
  153. div#sh_rdiv{position:fixed;right:15px;bottom:15px;}div#sh_rdiv A{position:relative;width:32px;height:32px;margin:0 5px;float:left}\
  154. a[id^="sh_"]{background:url(http://cn.bing.com/s/a/hpc12.png) no-repeat;}\
  155. #s_form_wrapper{position: absolute;top: 100px;}\
  156. #head_wrapper .s-p-top{position:absolute;top:0%;left:-33%;width:175px;height: 45px !important;padding: 0px!important;margin: 0px!important;background-position: 0 0px;min-height:45px !important;}\
  157. #s_top_wrapper {position: absolute;top: 0;}\
  158. .s-center-box{display: none !important;}\
  159. #s_mancard_main{background-color:white;}\
  160. #lg img{display: none !important;}\
  161. #head_wrapper {position: absolute;height: 0;width: 0px;}\
  162. #s_wrap{width: 0%;}\
  163. .s_form_wrapper{height:50%;}\
  164. #lg{/*-moz-transition:all 1s ease;-webkit-transition:all 1s ease;*/background:url("http://imgsrc.baidu.com/forum/w%3D580/sign=108ffbc58644ebf86d716437e9fbd736/9fc170d12f2eb938e0bfb834d4628535e7dd6fc9.jpg") no-repeat;}\
  165. .s-skin-hasbg #kw{background:none !important;width:423px !important;box-shadow: none !important;height: 26px!important;line-height: 26px!important;padding: 4px 9px 4px 7px!important;}\
  166. #form .bdsug{width:421px!important;}#form .bdsug li{width:404px!important;}\
  167. #form .bdsug{border-left:0px!important;border-right:0px!important;top:32px!important;}\
  168. div#s_fm {margin: 0 0 0 200px;padding: 0;position: absolute;top: -15%;width: 421px;height:45px!important;min-height: 45px;}\
  169. #form{background-color: #FFFFFF;margin-top: 5px!important;}\
  170. .btn_wr {background: url("http://imgsrc.baidu.com/forum/pic/item/7da86b899e510fb3f9270773d833c895d0430cf9.jpg") no-repeat scroll -149px -37px rgba(0, 0, 0, 0) !important;}\
  171. #su{display: none !important;}\
  172. span.btn_wr {float: right;left: -1px;position: relative;top: -30px;width: 28px !important;height:28px!important;}\
  173. #nv a,#nv b{margin-left: 10px!important;}\
  174. #nv a, #tb_mr b { text-decoration: none!important;font-size: 13px!important;}\
  175. #nv{position: absolute;left:-30px;top:0px;width: 480px !important;}\
  176. .s-notify-pnl .s-pk-mod,.s-new-weather-pnl{left:450px!important;}\
  177. .s-mod-weather{left:450px!important;top: 6px;}/*新版本首页*/\
  178. #bottom_container{display: none !important;height:0px!important;}\
  179. #s_wrap.s-isindex-wrap{ padding-bottom: 0px!important;margin-top:250px !important;}\
  180. a#sh_igl{background-position:-192px -53px}\
  181. a#sh_igr{background-position: -160px -53px}\
  182. a#sh_cp{background-position: -64px -85px}\
  183. a#sh_igd{background-position: 0 -53px}\
  184. #s_mod_weather .s-mod-weather-bear,#wt_inst_1 .new-weather-bear{display: none !important;}\
  185. #hp_bottomCell{position:fixed;left:0px;bottom:0px;width:100%;height:60px;background:linear-gradient(rgba(0, 0, 0, 0.2) 0px, rgba(0, 0, 0, 0.2) 100%) repeat scroll 0 0 rgba(0, 0, 0, 0)}\
  186. #hp_pgm{margin-left:20px;bottom:0px;}\
  187. #hp_pgm h3{text-align:left;color: rgb(255, 255, 255);font-size: 16px;font-weight: normal;margin: 0;padding: 0;}\
  188. #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;}'
  189. ;
  190. $("body").before('<style id="bingtu" type="text/css">'+css+'</style>');
  191. $("#head").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>');
  192. $("#bg2,#bg1").css("background-color",'rgba(89, 83, 87, 0.3)');
  193. $(".s-skin-container")&&$(".s-skin-container").remove();
  194. $('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>');
  195. (document.querySelector('.btn_wr')).addEventListener('click', function(event) {
  196. (document.querySelector('#form')).submit();
  197. });
  198. (document.getElementById('sh_rdiv')).addEventListener('click', function(event) {
  199. switch (event.target.id) {
  200. case 'sh_igl':prevImg();break;
  201. case 'sh_igr':nextImg();break;
  202. case 'sh_igd':downloadImg();break;
  203. }
  204. });
  205. function removeStyle(){
  206. $('#bingtu').empty();
  207. $('#hp_bottomCell').empty();
  208. }
  209. $("#form").submit(function(event){
  210. removeStyle();
  211. });
  212. $("#kw").change(function(){
  213. $(".bdsug-overflow").click(removeStyle)
  214. });
  215. }
  216.  
  217. try{
  218. //if(document.documentElement.hasAttribute('xmlns')){
  219. var bing_uri = 'http://cn.bing.com';
  220. var _window = typeof unsafeWindow != 'undefined'?unsafeWindow:window;
  221. //var $ = _window.$;
  222. var ajax = GM_xmlhttpRequest;
  223. bing();
  224. changHasBgStyle();
  225. _bing = load();
  226. if(_bing.current.url){
  227. $("#bg1").css("background-image",'url('+_bing.current.url+')');
  228. }
  229. loadBgJson(0,loadResponse);//_bing.idx
  230. //}
  231.  
  232. }catch(ee){
  233. console.log(ee)
  234. }
  235. function initBing () {
  236. var _init_bing = {
  237. name : '_baiduLogo',
  238. X : 0,
  239. Y : 0,
  240. current:{idx:0,url:''},
  241. formerly:{idx:0,url:''},
  242. idx:0,
  243. defultOpacity:40
  244. };
  245. save(_init_bing);
  246. _window.location.href=_window.location.href;
  247. }
  248. GM_registerMenuCommand("Bing图初始化",initBing);
  249.  
  250. $(".s-mancacrd-main").click(
  251. function(){
  252. setInterval(chk(),1500);
  253. function chk(){
  254. var gaodu=$("#wrapper").outerHeight(true);
  255. $("#bg1").css("background-attachment","fixed");
  256. $("#bg1").css("background-position","center center");
  257. $("#bg1").css("background-size","auto");
  258. $("#head").css("height",gaodu*2.5);
  259. }});