1+ // ==UserScript==
2+ // @name 百Bing图
3+ // @name :en BingBgForBaidu
4+ // @namespace hoothin
5+ // @version 1.0
6+ // @description 把百度首页背景图换成Bing的
7+ // @description :en Just change the background image of baidu.com to bing.com
8+ // @author hoothin
9+ // @grant GM_xmlhttpRequest
10+ // @grant GM_setValue
11+ // @grant GM_getValue
12+ // @grant GM_addStyle
13+ // @connect cn.bing.com
14+ // @run -at document-body
15+ // @include *://www.baidu.com/
16+ // @include *://www.baidu.com/home*
17+ // @include *://www.baidu.com/?tn=*
18+ // @include *://www.baidu.com/index.php*
19+ // ==/UserScript==
20+
21+ ( function ( ) {
22+ 'use strict' ;
23+ var MutationObserver = window . MutationObserver || window . WebKitMutationObserver || window . MozMutationObserver ;
24+ var head = document . querySelector ( "#head" ) ;
25+ if ( GM_getValue ( "baiBing" ) === undefined ) {
26+ GM_setValue ( "baiBing" , true ) ;
27+ GM_setValue ( "baiBingWhiteLogo" , true ) ;
28+ }
29+ if ( GM_getValue ( "baiBing" ) ) {
30+ setBingBg ( ) ;
31+ }
32+ var setObserver = new MutationObserver ( function ( records ) {
33+ var definedBtn = document . querySelector ( ".defined-btn-no" ) ;
34+ var check = document . querySelector ( "#bingCheck" ) ;
35+ if ( definedBtn && ! check ) {
36+ var label = document . createElement ( "label" ) ;
37+ label . setAttribute ( "style" , "float:left;margin-top:25px;margin-left:10px;" ) ;
38+ label . innerHTML = "<input id='bingCheck' type='checkbox'>Bing图" ;
39+ label . classList . add ( "recommend" ) ;
40+ definedBtn . after ( label ) ;
41+ check = document . querySelector ( "#bingCheck" ) ;
42+ document . querySelector ( ".defined-btn-yes" ) . addEventListener ( "click" , function ( ) {
43+ if ( check . checked ) {
44+ if ( ! GM_getValue ( "baiBing" ) ) setBingBg ( ) ;
45+ } else {
46+ if ( GM_getValue ( "baiBing" ) ) location . reload ( ) ;
47+ }
48+ GM_setValue ( "baiBing" , check . checked ) ;
49+ GM_setValue ( "baiBingWhiteLogo" , document . querySelector ( "#s_skin_exhibition_options" ) . classList . contains ( "white" ) ) ;
50+ } ) ;
51+ check . checked = ! ! GM_getValue ( "baiBing" ) ;
52+ }
53+ } ) ;
54+ var option = {
55+ 'childList' : true
56+ } ;
57+ setObserver . observe ( head , option ) ;
58+
59+ function setBingBg ( ) {
60+ var ctnerContents = document . querySelector ( "#s_ctner_contents" ) ;
61+ var menuContainer = document . querySelector ( ".s-menu-container" ) ;
62+ if ( ! ctnerContents || ! menuContainer ) return ;
63+ if ( ! head . classList . contains ( "s-skin-hasbg" ) ) {
64+ head . classList . add ( "s-skin-hasbg" ) ;
65+ GM_addStyle ( ".s-mod-weather .unknown-text, .unknown-city, .city-wather a, .city-wather em, .city-wather span, .show-pollution a, .show-pollution em, .show-pollution span, .s-upfunc-menus .s-icons a {color: #fff!important;}" ) ;
66+ GM_addStyle ( ".s-skin-hasbg .s-top-wrap {border: none;background: none;background-image: -webkit-gradient(linear,left top,left bottom,from(rgba(15,25,50,.3)),to(rgba(15,25,50,.3)));background-image: -moz-linear-gradient(rgba(15,25,50,.3) 0,rgba(15,25,50,.3) 100%);background-image: -ms-linear-gradient(rgba(15,25,50,.3) 0,rgba(15,25,50,.3) 100%);background-image: -o-linear-gradient(rgba(15,25,50,.3) 0,rgba(15,25,50,.3) 100%);background-image: linear-gradient(rgba(15,25,50,.3) 0,rgba(15,25,50,.3) 100%);filter: progid:DXImageTransform.Microsoft.gradient(startColorstr=#330f1932,endColorstr=#330f1932);}" ) ;
67+ GM_addStyle ( ".s-opacity-50 .s-ctner-menus .s-menu-item:hover {color: #333 !important;background-color: transparent !important;background-image: -webkit-gradient(linear,left top,left bottom,from(rgba(240,240,240,.2)),to(rgba(240,240,240,.2))) !important;background-image: -moz-linear-gradient(rgba(240,240,240,.2) 0,rgba(240,240,240,.2) 100%) !important;background-image: -ms-linear-gradient(rgba(240,240,240,.2) 0,rgba(240,240,240,.2) 100%) !important;background-image: -o-linear-gradient(rgba(240,240,240,.2) 0,rgba(240,240,240,.2) 100%) !important;background-image: linear-gradient(rgba(240,240,240,.2) 0,rgba(240,240,240,.2) 100%) !important;filter: progid:DXImageTransform.Microsoft.gradient(startColorstr=#33f0f0f0,endColorstr=#33f0f0f0) !important;}" ) ;
68+ ctnerContents . setAttribute ( "style" , "background: none !important;background-image: -webkit-gradient(linear,left top,left bottom,from(rgba(255,255,255,0.5)),to(rgba(255,255,255,0.5))) !important;background-image: -moz-linear-gradient(rgba(255,255,255,0.5) 0%,rgba(255,255,255,0.5) 100%) !important;background-image: -ms-linear-gradient(rgba(255,255,255,0.5) 0%,rgba(255,255,255,0.5) 100%) !important;background-image: -o-linear-gradient(rgba(255,255,255,0.5) 0%,rgba(255,255,255,0.5) 100%) !important;background-image: linear-gradient(rgba(255,255,255,0.5) 0%,rgba(255,255,255,0.5) 100%) !important;filter: progid:DXImageTransform.Microsoft.gradient(startColorstr=#7FFFFFFF,endColorstr=#7FFFFFFF) !important;" ) ;
69+ menuContainer . setAttribute ( "style" , "background: none !important;background-image: -webkit-gradient(linear,left top,left bottom,from(rgba(255,255,255,0.5)),to(rgba(255,255,255,0.5))) !important;background-image: -moz-linear-gradient(rgba(255,255,255,0.5) 0%,rgba(255,255,255,0.5) 100%) !important;background-image: -ms-linear-gradient(rgba(255,255,255,0.5) 0%,rgba(255,255,255,0.5) 100%) !important;background-image: -o-linear-gradient(rgba(255,255,255,0.5) 0%,rgba(255,255,255,0.5) 100%) !important;background-image: linear-gradient(rgba(255,255,255,0.5) 0%,rgba(255,255,255,0.5) 100%) !important;filter: progid:DXImageTransform.Microsoft.gradient(startColorstr=#7FFFFFFF,endColorstr=#7FFFFFFF) !important;" ) ;
70+ }
71+ GM_xmlhttpRequest ( {
72+ method : 'GET' ,
73+ url : "http://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&pid=hp&video=1&n=1" ,
74+ onload : function ( result ) {
75+ var jsonData = null ;
76+ try {
77+ jsonData = JSON . parse ( result . responseText ) ;
78+ var bgUrl = "url(\"" + jsonData . images [ 0 ] . url + "\")" ;
79+ var skinContainer = document . querySelector ( ".s-skin-container" ) ;
80+ if ( ! skinContainer ) {
81+ return ;
82+ }
83+ var bingBgLink = document . createElement ( "a" ) ;
84+ bingBgLink . title = "Bing图" ;
85+ bingBgLink . href = jsonData . images [ 0 ] . url ;
86+ bingBgLink . innerHTML = "<span class='title'>Bing图</span>" ;
87+ document . querySelector ( "#s_icons" ) . appendChild ( bingBgLink ) ;
88+ if ( skinContainer . style . backgroundImage != bgUrl ) {
89+ skinContainer . style . backgroundImage = bgUrl ;
90+ }
91+ var observer = new MutationObserver ( function ( records ) {
92+ if ( skinContainer . style . backgroundImage != bgUrl ) {
93+ skinContainer . style . backgroundImage = bgUrl ;
94+ }
95+ } ) ;
96+ var option = {
97+ 'attributes' : true
98+ } ;
99+ observer . observe ( skinContainer , option ) ;
100+ if ( GM_getValue ( "baiBingWhiteLogo" ) ) {
101+ var logo = document . querySelector ( "#s_lg_img" ) ;
102+ logo . src = logo . src . replace ( / \/ [ ^ \/ ] + $ / , "/logo_white.png" ) ;
103+ }
104+ } catch ( e ) {
105+ console . log ( e ) ;
106+ }
107+ }
108+ } ) ;
109+ }
110+ } ) ( ) ;
0 commit comments