油猴脚本首战—给百度加一个皮肤
先上代码:
// ==UserScript==
// @name 给百度加个皮肤
// @namespace http://tampermonkey.net/
// @version 0.1
// @description 去除chrome浏览器百度的每日新闻,设置背景和颜色主题
// @author Michael
// @match https://www.baidu.com/
// @match https://www.baidu.com/*
// @run-at document-start
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Your code here..
var test = window.location.href;
if(test!="https://www.baidu.com/")//如果不是主页的话,不执行
{
var x = document.getElementsByTagName("body");
x[0].style.backgroundColor = "#DCDCDC";
x[0].style.backgroundImage = "url(https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1046555063,1533124755&fm=26&gp=0.jpg)";
x[0].style.backgroundSize= "1400px 900px" ;
x[0].style.backgroundRepeat = "no-repeat";
x[0].style.backgroundAttachment= "fixed";
x[0].style.opacity = "1.4";
}else{
var y = document.getElementById("s_main");
y.remove();
var z = document.getElementById("s_top_wrap")
z.remove();
var e = document.getElementById("bottom_layer")
e.remove();
var w = document.getElementById("hotsearch-content-wrapper")
w.remove();
var s = document.getElementById("su")
s.style.backgroundColor = "#fffff";
var xx = document.getElementsByTagName("body");
xx[0].style.backgroundImage = "url(https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1046555063,1533124755&fm=26&gp=0.jpg)";
xx[0].style.backgroundSize= "1300px 900px" ;
xx[0].style.backgroundRepeat = "no-repeat";
xx[0].style.backgroundAttachment= "fixed";
}
})();
写的并不是很好,作为自己的第一个作品,先做一下记录。
记录一下使用到的几个方法和积累的一些经验
1.var test = window.location.href;
获取当前的url的方法
2.浏览器网页有很多ajax的方法,会导致页面不能够刷新
其实使用 if(test!=“https://www.baidu.com/”)
的方法是不妥的,会造成背景的失灵。
3.背景图片的设置方法
这里有详细的解释:css设置背景图片格式
`
由于对于Ajax的理解并不是非常的好,这里没有使用,也没有非常好的解决应对百度ajax的方法,这里以后也会加强学习。