WaitForKeyElements3

Find the specified element and execute subsequent code after finding it.

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

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

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