函数错误地计算了元素数量

问题描述 投票:0回答:1

我使用waitForKeyElementswaitForKeyElements功能在Brock Adams页面上的链接后创建复选框。

由于某种原因,该功能无法正常工作。与其在每个链接之后放置单个复选框,不如在每个链接后面都带有很多复选框。

如何使其工作?

https://www.google.com/

函数本身(从要点复制粘贴):

// ==UserScript==
// @grant   none
// @match   https://*.google.*/
// @name    Google.com
// @require https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js
// @require https://gist.githubusercontent.com/BrockA/2625891/raw/9c97aa67ff9c5d56be34a55ad6c18a314e5eb548/waitForKeyElements.js
// ==/UserScript==

(function() {
    'use strict';

    function test() {
        var links = document.querySelectorAll('a');
        var i;
        for (i = 0; i < links.length; i++) {
            var input = document.createElement('input');
            input.type = 'checkbox';
            links[i].parentElement.appendChild(input);
        }
    }

    //waitForKeyElements('a', test); // works incorrectly. The third parameter
        // doesn't help.
    //setTimeout(test(), 3000); // works OK
})();

编辑:参见David的回答下的评论。

javascript jquery tampermonkey userscripts
1个回答
2
投票

您没有提供/*--- waitForKeyElements(): A utility function, for Greasemonkey scripts, that detects and handles AJAXed content. Usage example: waitForKeyElements ( "div.comments" , commentCallbackFunction ); //--- Page-specific function to do what we want when the node is found. function commentCallbackFunction (jNode) { jNode.text ("This comment changed by waitForKeyElements()."); } IMPORTANT: This function requires your script to have loaded jQuery. */ function waitForKeyElements ( selectorTxt, /* Required: The jQuery selector string that specifies the desired element(s). */ actionFunction, /* Required: The code to run when elements are found. It is passed a jNode to the matched element. */ bWaitOnce, /* Optional: If false, will continue to scan for new elements even after the first match is found. */ iframeSelector /* Optional: If set, identifies the iframe to search. */ ) { var targetNodes, btargetsFound; if (typeof iframeSelector == "undefined") targetNodes = $(selectorTxt); else targetNodes = $(iframeSelector).contents () .find (selectorTxt); if (targetNodes && targetNodes.length > 0) { btargetsFound = true; /*--- Found target node(s). Go through each and act if they are new. */ targetNodes.each ( function () { var jThis = $(this); var alreadyFound = jThis.data ('alreadyFound') || false; if (!alreadyFound) { //--- Call the payload function. var cancelFound = actionFunction (jThis); if (cancelFound) btargetsFound = false; else jThis.data ('alreadyFound', true); } } ); } else { btargetsFound = false; } //--- Get the timer-control variable for this selector. var controlObj = waitForKeyElements.controlObj || {}; var controlKey = selectorTxt.replace (/[^\w]/g, "_"); var timeControl = controlObj [controlKey]; //--- Now set or clear the timer as appropriate. if (btargetsFound && bWaitOnce && timeControl) { //--- The only condition where we need to clear the timer. clearInterval (timeControl); delete controlObj [controlKey] } else { //--- Set a timer, if needed. if ( ! timeControl) { timeControl = setInterval ( function () { waitForKeyElements ( selectorTxt, actionFunction, bWaitOnce, iframeSelector ); }, 300 ); controlObj [controlKey] = timeControl; } } waitForKeyElements.controlObj = controlObj; } 的用例,但是如果我理解正确,您可以将waitForKeyElements函数传递给它。如果真是这样-难怪它将X复选框附加到页面上的每个链接。仔细观察:-您的函数在被调用后会搜索页面中的所有链接,并在其旁边添加一个复选框-test根据提供的选择器调用函数对于找到的每个元素。因此,基本上,如果页面有50个链接,则将在每个链接之后附加50个复选框。

解决方案是不要在函数中循环,而仅在提供的参数中添加一个复选框:

waitForKeyElements
© www.soinside.com 2019 - 2024. All rights reserved.