Greasy Fork is available in English.

校友邦实习一键评价 XiaoYouBang Auto Review

一键评价,自动填写评价内容

  1. // ==UserScript==
  2. // @name 校友邦实习一键评价 XiaoYouBang Auto Review
  3. // @namespace https://github.com/lcandy2/XiaoYouBang-Auto-Review
  4. // @version 1.4
  5. // @author lcandy2
  6. // @description 一键评价,自动填写评价内容
  7. // @license MIT
  8. // @icon https://www.xybsyw.com/favicon.ico
  9. // @match *://*.xybsyw.com/personal/*
  10. // @require https://registry.npmmirror.com/jquery/3.7.1/files/dist/jquery.min.js
  11. // @run-at document-start
  12. // ==/UserScript==
  13.  
  14. (function ($) {
  15. 'use strict';
  16.  
  17. const config = {
  18. autoSubmit: false,
  19. // 是否自动提交
  20. day: 1,
  21. // 天数类评价
  22. rate: 5,
  23. // 评分类评价 1-5
  24. review: "好",
  25. // 评价内容
  26. suggestions: "无",
  27. // 建议内容
  28. addCourse: "无",
  29. // 增加课程
  30. radio: 0,
  31. // 单选选择索引,从 0 开始
  32. reviewHref: "myEvaluationDetail"
  33. // 评价页面的 href,无特殊情况不需要修改
  34. };
  35. const fillInputs = (inputElement, data) => {
  36. inputElement.focus();
  37. inputElement.value = "";
  38. data.split("").forEach((char) => {
  39. let keydownEvent = new KeyboardEvent("keydown", { key: char, bubbles: true });
  40. let keyupEvent = new KeyboardEvent("keyup", { key: char, bubbles: true });
  41. let inputEvent = new Event("input", { bubbles: true });
  42. inputElement.dispatchEvent(keydownEvent);
  43. inputElement.value += char;
  44. inputElement.dispatchEvent(inputEvent);
  45. inputElement.dispatchEvent(keyupEvent);
  46. });
  47. inputElement.blur();
  48. };
  49. const Review = () => {
  50. const $allInput = $("input.el-input__inner");
  51. const $dayInput = $allInput.filter((index, element) => {
  52. return $(element).attr("max") !== void 0 && $(element).attr("min") !== void 0;
  53. });
  54. const $otherInput = $allInput.not($dayInput);
  55. const $courseInput = $otherInput.filter((index, element) => {
  56. return $(element).attr("placeholder").includes("课程");
  57. });
  58. const $allRate = $("div.el-rate");
  59. const $allTextarea = $("textarea.el-textarea__inner");
  60. const $suggestTextarea = $allTextarea.filter((index, element) => {
  61. return $(element).attr("placeholder").includes("建议");
  62. });
  63. const $otherTextarea = $allTextarea.filter((index, element) => {
  64. return !$(element).attr("placeholder").includes("建议");
  65. });
  66. const $allRadio = $("div.radioItem");
  67. $dayInput.each((index, element) => {
  68. fillInputs(element, config.day.toString());
  69. });
  70. $courseInput.each((index, element) => {
  71. fillInputs(element, config.addCourse);
  72. });
  73. $allRate.each((index, element) => {
  74. $(element).children().eq(config.rate - 1).trigger("click");
  75. });
  76. $otherTextarea.each((index, element) => {
  77. fillInputs(element, config.review);
  78. });
  79. $suggestTextarea.each((index, element) => {
  80. fillInputs(element, config.suggestions);
  81. });
  82. $allRadio.each((index, element) => {
  83. $(element).children().eq(config.radio).trigger("click");
  84. });
  85. console.log("Review success!");
  86. };
  87. const addReviewButton = (listener) => {
  88. if ($("button.--lcandy2-xyb-auto-review").length)
  89. return;
  90. const $topContent = $("div.contentItem div.topContent");
  91. const $topContent_item = $topContent.children().eq(1);
  92. const $reviewButton = $(`<button type="button" class="el-button submitBtn el-button--default --lcandy2-xyb-auto-review"><i class="el-icon-edit" /><span> 一键评价 </span></button>`);
  93. $reviewButton.on("click", () => {
  94. config.autoSubmit = false;
  95. listener();
  96. });
  97. const $reviewAndSubmitButton = $(`<button type="button" class="el-button submitBtn el-button--primary --lcandy2-xyb-auto-review"><i class="el-icon-check" /><span> 评价并<b>提交</b> </span></button>`);
  98. $reviewAndSubmitButton.on("click", () => {
  99. config.autoSubmit = true;
  100. listener();
  101. });
  102. $topContent_item.append($reviewButton);
  103. $topContent_item.append($reviewAndSubmitButton);
  104. console.log("Add Review Button", $reviewButton);
  105. };
  106. const watchUrlChange = (onChange) => {
  107. const originalPushState = history.pushState;
  108. history.pushState = function(state, title, url) {
  109. originalPushState.apply(this, arguments);
  110. onChange(url);
  111. };
  112. const originalReplaceState = history.replaceState;
  113. history.replaceState = function(state, title, url) {
  114. originalReplaceState.apply(this, arguments);
  115. onChange(url);
  116. };
  117. window.addEventListener("popstate", () => {
  118. onChange(document.location.href);
  119. });
  120. };
  121. const executeReview = async () => {
  122. Review();
  123. const $submitButton = $("div.bottomDiv button.submitBtn");
  124. $submitButton.val("评价完成,点击提交评价");
  125. if (config.autoSubmit) {
  126. $submitButton.trigger("click");
  127. alert("评价完成,已自动提交。");
  128. }
  129. };
  130. const main = () => {
  131. const href = window.location.href;
  132. if (!href.includes(config.reviewHref))
  133. return;
  134. addReviewButton(executeReview);
  135. };
  136. const observer = (func) => {
  137. const observer2 = new MutationObserver((mutations) => {
  138. for (let mutation of mutations) {
  139. if (mutation.addedNodes.length) {
  140. const $topContent = $("div.contentItem div.topContent");
  141. if ($topContent.length) {
  142. observer2.disconnect();
  143. func();
  144. break;
  145. }
  146. }
  147. }
  148. });
  149. observer2.observe(document.body, {
  150. childList: true,
  151. subtree: true
  152. });
  153. };
  154. $(async () => {
  155. observer(main);
  156. watchUrlChange((newUrl) => {
  157. console.log("URL 变化了:", newUrl);
  158. observer(main);
  159. });
  160. });
  161.  
  162. })(jQuery);