waitForKeyElements3

Find the specified element and execute subsequent code after finding it. This code is rewritten by BrockA's waitForKeyElements.js and uses the native js environment.

目前为 2025-01-07 提交的版本。查看 最新版本

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.cn-greasyfork.org/scripts/523085/1516446/waitForKeyElements3.js

  1. // Example usage:
  2. // waitForKeyElements("div.comments", {"textContent":"xxx"}, commentCallbackFunction);
  3. // function commentCallbackFunction(node) {
  4. // node.textContent = "This comment changed by waitForKeyElements().";
  5. // }
  6.  
  7. function waitForKeyElements(
  8. selectorTxt, // Required: The CSS selector string that specifies the desired element(s).
  9. attributeFilter,// Optional: Filter the element by target attribute(s).
  10. actionFunction, // Required: The code to run when elements are found. It is passed the matched element.
  11. bWaitOnce, // Optional: If false, will continue to scan for new elements even after the first match is found.
  12. iframeSelector // Optional: If set, identifies the iframe to search.
  13. ) {
  14. var targetNodes, btargetsFound = false;
  15.  
  16. function querySelectorAllInIframe(iframe, selector) {
  17. var doc = iframe.contentDocument || iframe.contentWindow.document;
  18. return doc.querySelectorAll(selector);
  19. }
  20.  
  21. if (typeof iframeSelector !== "undefined") {
  22. var iframe = document.querySelector(iframeSelector);
  23. if (iframe) {
  24. targetNodes = querySelectorAllInIframe(iframe, selectorTxt);
  25. }
  26. } else {
  27. targetNodes = document.querySelectorAll(selectorTxt);
  28. }
  29.  
  30. if (targetNodes && targetNodes.length > 0) {
  31. btargetsFound = true;
  32. // Found target node(s). Go through each and act if they are new.
  33. targetNodes.forEach(function (node) {
  34. if (attributeFilter){
  35. for (let attr of Object.keys(attributeFilter)){
  36. if(node[attr] != attributeFilter[attr]){
  37. return ;
  38. }
  39. }
  40. }
  41. var alreadyFound = node.dataset.alreadyFound || false;
  42. if (!alreadyFound) {
  43. // Call the payload function.
  44. var cancelFound = actionFunction(node);
  45. if (cancelFound) {
  46. btargetsFound = false;
  47. } else {
  48. node.dataset.alreadyFound = true;
  49. }
  50. }
  51. });
  52. }
  53.  
  54. // Get the timer-control variable for this selector.
  55. var controlObj = waitForKeyElements.controlObj || {};
  56. var controlKey = selectorTxt.replace(/[^\w]/g, "_");
  57. var timeControl = controlObj[controlKey];
  58.  
  59. // Now set or clear the timer as appropriate.
  60. if (btargetsFound && bWaitOnce && timeControl) {
  61. // The only condition where we need to clear the timer.
  62. clearInterval(timeControl);
  63. delete controlObj[controlKey];
  64. } else {
  65. // Set a timer, if needed.
  66. if (!timeControl) {
  67. timeControl = setInterval(function () {
  68. waitForKeyElements(selectorTxt, attributeFilter, actionFunction, bWaitOnce, iframeSelector);
  69. }, 300);
  70. controlObj[controlKey] = timeControl;
  71. }
  72. }
  73. waitForKeyElements.controlObj = controlObj;
  74. };