teddy wang

teddy's lib

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

  1. // https://github.com/Tampermonkey/tampermonkey/issues/853#issuecomment-892417299
  2. // Original poster might consider closing this issue. As mentioned, "require" is already available to reuse libs. The error message was just pointing out that "mylibrary" wasn't a working URI. GreasyFork or GitHub would work fine for publishing the lib itself.
  3.  
  4.  
  5. function inputElemChinese(elem, chtext) {
  6.  
  7. // Set focus on the input element
  8. elem.click();
  9. elem.focus();
  10.  
  11. elem.value = chtext;
  12.  
  13. for (var i = 0; i < chtext.length; i++) {
  14. var char = chtext.charAt(i);
  15. var event3 = new CompositionEvent('compositionstart', { bubbles: true });
  16. elem.dispatchEvent(event3);
  17.  
  18. var eventInput = new InputEvent('input', { bubbles: true });
  19. elem.dispatchEvent(eventInput);
  20.  
  21. var event4 = new CompositionEvent('compositionend', { bubbles: true });
  22. elem.dispatchEvent(event4);
  23. }
  24.  
  25. var keyUpEventDone = {
  26. value: false
  27. };
  28. setTimeout(() => {
  29. // Simulate pressing the Enter key
  30. var enterEvent = new KeyboardEvent('keydown', {
  31. key: 'Enter',
  32. code: 'Enter',
  33. keyCode: 13,
  34. which: 13,
  35. bubbles: true
  36. });
  37. elem.dispatchEvent(enterEvent);
  38.  
  39. setTimeout(() => {
  40. var enterEvent2 = new KeyboardEvent('keyup', {
  41. key: 'Enter',
  42. code: 'Enter',
  43. keyCode: 13,
  44. which: 13,
  45. bubbles: true
  46. });
  47. elem.dispatchEvent(enterEvent2);
  48. keyUpEventDone.value = true;
  49. }, 99);
  50. }, 1200);
  51.  
  52.  
  53. return keyUpEventDone;
  54. }
  55. // queryElemByClassAndText('label.el-form-item__label', '产权凭证号')
  56. function queryElemByClassAndText(selector, textToFind) {
  57.  
  58. // Get all span elements
  59. var elems = document.querySelectorAll(selector);
  60.  
  61. // Loop through the elements
  62. for (var i = 0; i < elems.length; i++) {
  63. var elem = elems[i];
  64.  
  65. if (elem.textContent.trim().includes(textToFind)) {
  66. console.log('elem:', elem);
  67. return elem;
  68. }
  69. }
  70. return null;
  71. }
  72. function clickSpanByTextOOOOK(textToFind) { // 2024-08-20 Claude 3 Sonnet (self-moderated)
  73.  
  74. // Get all span elements
  75. var spanElements = document.querySelectorAll('span.el-checkbox__label');
  76.  
  77. // Loop through the span elements
  78. for (var i = 0; i < spanElements.length; i++) {
  79. var spanElement = spanElements[i];
  80.  
  81. // Check if the span element's text content matches the desired text
  82. if (spanElement.textContent.trim().includes(textToFind)) {
  83. console.log('spanElement', spanElement);
  84. spanElement.click();
  85. return true;
  86. }
  87. }
  88.  
  89. // If no span element with the desired text was found, return false
  90. return false;
  91. }
  92. function clickSpanByTextBad2(textToFind) {
  93. /* 2024-08-20 Claude 3 Sonnet (self-moderated)
  94. The issue with your code is that the document.evaluate method used for XPath evaluation is not supported in all browsers.
  95. It works in Firefox, but not in Chrome or other Chromium-based browsers like Edge or Opera.
  96. Additionally, the XPathResult object is not a standard web API and is only supported in Firefox.
  97. Here's an alternative approach that uses querySelectorAll to find the span elements and checks their text content:
  98. */
  99.  
  100. // Use XPath to find the span with the specified text
  101. var xpath = "//span[contains(text(), '" + textToFind + "')]";
  102. var result = document.evaluate(xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
  103. var spanElement = result.singleNodeValue;
  104.  
  105. console.log('spanElement', spanElement);
  106.  
  107. // Check if the element was found
  108. if (spanElement) {
  109. spanElement.click()
  110. return true;
  111. }
  112. return false;
  113. }
  114.  
  115. function clickSpanByTitle(title1) {
  116. // var elem = document.querySelector('p[title="我的待办(OA)"]');
  117. var elem = document.querySelector('span[title="' + title1 + '"]');
  118.  
  119. console.log('elem', elem);
  120.  
  121. // Check if the element was found
  122. if (elem) {
  123. elem.click()
  124. /*
  125. elem.dispatchEvent(new MouseEvent('click', {
  126. bubbles: true,
  127. cancelable: true,
  128. view: window
  129. }));
  130. */
  131. return true;
  132. }
  133. return false;
  134. }