百Bing图

我只想把百度首页背景换成Bing的

目前為 2016-11-28 提交的版本,檢視 最新版本

// ==UserScript==
// @name         百Bing图
// @namespace    hoothin
// @version      0.1
// @description  我只想把百度首页背景换成Bing的
// @author       hoothin
// @match        http://*/*
// @grant        GM_xmlhttpRequest
// @grant        GM_setValue
// @grant        GM_getValue
// @connect      cn.bing.com
// @include 	 *://www.baidu.com/
// @include 	 *://www.baidu.com/home*
// @include 	 *://www.baidu.com/?tn=*
// @include 	 *://www.baidu.com/index.php*
// ==/UserScript==

(function() {
    'use strict';
    var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
    if(GM_getValue("baiBing")){
        setBingBg();
    }
    var setObserver = new MutationObserver(function(records){
        var definedBtn=document.querySelector(".defined-btn-no");
        var check=document.querySelector("#bingCheck");
        if(definedBtn && !check){
            var label=document.createElement("label");
            label.setAttribute("style","float:left;margin-top:25px;margin-left:10px;");
            label.innerHTML="<input id='bingCheck' type='checkbox'>Bing图";
            label.classList.add("recommend");
            definedBtn.after(label);
            check=document.querySelector("#bingCheck");
            document.querySelector(".defined-btn-yes").addEventListener("click", function(){
                if(check.checked){
                    if(!GM_getValue("baiBing"))setBingBg();
                }else{
                    if(GM_getValue("baiBing"))location.reload();
                }
                GM_setValue("baiBing",check.checked);
            });
            check.checked=!!GM_getValue("baiBing");
        }
    });
    var option = {
        'childList': true
    };
    var head=document.querySelector("#head");
    setObserver.observe(head, option);

    function setBingBg(){
        GM_xmlhttpRequest({
            method: 'GET',
            url: "http://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&pid=hp&video=1&n=1",
            onload: function(result) {
                var jsonData = null;
                try {
                    jsonData = JSON.parse(result.responseText);
                    var bgUrl="url(\""+jsonData.images[0].url+"\")";
                    var skinContainer=document.querySelector(".s-skin-container");
                    if(!skinContainer){
                        return;
                    }
                    if(skinContainer.style.backgroundImage != bgUrl){
                        skinContainer.style.backgroundImage = bgUrl;
                    }
                    var observer = new MutationObserver(function(records){
                        if(skinContainer.style.backgroundImage != bgUrl){
                            skinContainer.style.backgroundImage = bgUrl;
                        }
                    });
                    var option = {
                        'attributes': true
                    };
                    observer.observe(skinContainer, option);
                }catch (e) {
                    console.log(e);
                }
            }
        });
    }
})();