w84Kel

Permet de se déclencher à l'arrivée d'un élément sur une page.

目前为 2020-01-06 提交的版本,查看 最新版本

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

  1. // ==UserScript==
  2. // @name w84Kel
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description Permet de se déclencher à l'arrivée d'un élément sur une page.
  6. // @author CoStiC
  7. // @include *://*/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. /*--- waitForKeyElements(): A utility function, for Greasemonkey scripts,
  15. that detects and handles AJAXed content. Forked for use without JQuery.
  16. Usage example:
  17. waitForKeyElements (
  18. "div.comments"
  19. , commentCallbackFunction
  20. );
  21. //--- Page-specific function to do what we want when the node is found.
  22. function commentCallbackFunction (element) {
  23. element.text ("This comment changed by waitForKeyElements().");
  24. }
  25. IMPORTANT: Without JQuery, this fork does not look into the content of
  26. iframes.
  27. */
  28. function waitForKeyElements(
  29. selectorTxt, /* Required: The selector string that specifies the desired element(s). */
  30. actionFunction, /* Required: The code to run when elements are found. It is passed a jNode to the matched element. */
  31. bWaitOnce /* Optional: If false, will continue to scan for new elements even after the first match is found. */
  32. ) {
  33. var targetNodes, btargetsFound;
  34. targetNodes = document.querySelectorAll(selectorTxt);
  35.  
  36. if (targetNodes && targetNodes.length > 0) {
  37. btargetsFound = true;
  38. /*--- Found target node(s). Go through each and act if they are new.*/
  39. targetNodes.forEach(function (element) {
  40. var alreadyFound = element.dataset.found == 'alreadyFound' ? 'alreadyFound' : false;
  41.  
  42. if (!alreadyFound) {
  43. //--- Call the payload function.
  44. var cancelFound = actionFunction(element);
  45. if (cancelFound) btargetsFound = false; else element.dataset.found = 'alreadyFound';
  46. }
  47. });
  48. } else {
  49. btargetsFound = false;
  50. }
  51.  
  52. //--- Get the timer-control variable for this selector.
  53. var controlObj = waitForKeyElements.controlObj || {};
  54. var controlKey = selectorTxt.replace(/[^\w]/g, "_");
  55. var timeControl = controlObj[controlKey];
  56.  
  57. //--- Now set or clear the timer as appropriate.
  58. if (btargetsFound && bWaitOnce && timeControl) {
  59. //--- The only condition where we need to clear the timer.
  60. clearInterval(timeControl);
  61. delete controlObj[controlKey];
  62. } else {
  63. //--- Set a timer, if needed.
  64. if (!timeControl) {
  65. timeControl = setInterval(function () {
  66. waitForKeyElements(selectorTxt,
  67. actionFunction,
  68. bWaitOnce
  69. );
  70. },
  71. 300
  72. );
  73. controlObj[controlKey] = timeControl;
  74. }
  75. }
  76. waitForKeyElements.controlObj = controlObj;
  77. }
  78.  
  79. })();