教学视频https://space.bilibili.com/519965290?spm_id_from=..0.0
通过xpath获取元素
// ==UserScript==
// @name 百度文字变色
// @namespace http://tampermonkey.net/
// @version 0.1
// @description 更改百度首页下文字的颜色
// @author 浅若红尘
// @match *://www.baidu.com/*
// @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant none
// ==/UserScript==
(function() {
'use strict';
var p=document.evaluate('/html/body/div[1]/div[1]/div[5]/div/div/div[3]/ul/li[1]/a/span[2]',document).iterateNext();
p.style.color='red';
// Your code here...
})();
效果
通过id或class获取元素
// ==UserScript==
// @name 百度背景颜色
// @namespace http://tampermonkey.net/
// @version 0.1
// @description 更改百度首页背景颜色
// @author 浅若红尘
// @match *://www.baidu.com/*
// @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant none
// ==/UserScript==
(function() {
'use strict';
var p=document.querySelector('.cos-pc');
p.style.backgroundColor='green';
// Your code here...
})();
效果