函数没有正确计算元素的数量
Function doesn't count the amount of elements correctly
我用waitForKeyElements
function by Brock Adams to create the checkboxes after the links on the https://www.google.com/.
由于某种原因,该功能无法正常工作。它不是在每个 link 之后放置单个复选框,而是对所有 link 进行计数,然后在每个 link 之后添加相应数量的复选框。
可能是什么错误?
// ==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
})();
函数本身:
/*--- 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
的用例,但如果我理解正确,您将 test
函数传递给它。如果是这样的话——难怪它会在页面上的每个 link 上附加 X 复选框。仔细看:
- 您的函数在被调用时搜索页面中的所有 link 并在其旁边添加一个复选框
- waitForKeyElements
根据提供的选择器为它找到的每个元素 调用该函数 。
所以,基本上,如果页面有 50 links,if 将在每 link 50 次后附加一个复选框。
解决方案是不要在您的函数中循环,而是只向提供的参数添加一次复选框:
(function() {
'use strict';
function test(element) {
var input = document.createElement('input');
input.type = 'checkbox';
element.parent().append(input);
}
})();
我用waitForKeyElements
function by Brock Adams to create the checkboxes after the links on the https://www.google.com/.
由于某种原因,该功能无法正常工作。它不是在每个 link 之后放置单个复选框,而是对所有 link 进行计数,然后在每个 link 之后添加相应数量的复选框。
可能是什么错误?
// ==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
})();
函数本身:
/*--- 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
的用例,但如果我理解正确,您将 test
函数传递给它。如果是这样的话——难怪它会在页面上的每个 link 上附加 X 复选框。仔细看:
- 您的函数在被调用时搜索页面中的所有 link 并在其旁边添加一个复选框
- waitForKeyElements
根据提供的选择器为它找到的每个元素 调用该函数 。
所以,基本上,如果页面有 50 links,if 将在每 link 50 次后附加一个复选框。
解决方案是不要在您的函数中循环,而是只向提供的参数添加一次复选框:
(function() {
'use strict';
function test(element) {
var input = document.createElement('input');
input.type = 'checkbox';
element.parent().append(input);
}
})();