2. Grease Monkey: consigue elementos

Vídeo didáctico https://space.bilibili.com/519965290?spm_id_from=..0.0

Obtener elementos a través de 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...
})(); 

Efecto 

Obtener elemento por id o clase

// ==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...
})(); 

Efecto

Supongo que te gusta

Origin blog.csdn.net/m0_62247560/article/details/131347449
Recomendado
Clasificación