首页 > 解决方案 > Tampermonkey:试图点击按钮

问题描述

我试图点击的按钮:

--

<button data-adblock-el="continueWithAdblockerButton" aria-label="Continue with ad blocker" 
class="outline-focus">
    <svg aria-labelledby="title-9b2bd6fd-8b00-4d7e-8535-cfc1811b1b07" class="stroke-current" 
    width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" 
    role="img">
         <title id="title-9b2bd6fd-8b00-4d7e-8535-cfc1811b1b07">Icon: schließen</title>
         <g id="l-close-9b2bd6fd-8b00-4d7e-8535-cfc1811b1b07">
             <path id="vector-9b2bd6fd-8b00-4d7e-8535-cfc1811b1b07" d="M18 18L6 6m12 0L6 18" stroke- 
             width="1.3" stroke-linecap="round"></path>
         </g>
     </svg>
 </button>

在 concole 这有效:

document.querySelector('button[data-adblock-el^="continueWithAdblockerButton"]').click();

但是我在 Tampermonkey 中所做的任何事情似乎都没有效果。我的脚本:

// ==UserScript==
// @name         some script
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       jhm
// @match        https://www.spiegel.de/*
// @require      http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @require      https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    waitForKeyElements ('button[data-adblock-el^="continueWithAdblockerButton"]', clickNode);

    document.querySelector('button[data-adblock-el^="continueWithAdblockerButton"]').click();

    $(document).ready(function() {
    document.querySelector('button[data-adblock-el^="continueWithAdblockerButton"]').click();
    }

    function clickNode (jNode) {
        jNode.click ();
    }


    // Your code here...
})();

标签: jquerytampermonkey

解决方案


推荐阅读