Fast_Trade_In

优化回收流程

  1. // ==UserScript==
  2. // @name:zh-CN DIG批量回收
  3. // @name Fast_Trade_In
  4. // @namespace https://blog.chrxw.com
  5. // @supportURL https://blog.chrxw.com/scripts.html
  6. // @contributionURL https://afdian.com/@chr233
  7. // @version 1.2
  8. // @description 优化回收流程
  9. // @description:zh-CN 添加删除按钮
  10. // @author Chr_
  11. // @include https://www.dailyindiegame.com/account_*.html
  12. // @license AGPL-3.0
  13. // @icon https://blog.chrxw.com/favicon.ico
  14. // @grant GM_addStyle
  15. // ==/UserScript==
  16.  
  17. // 初始化
  18. (() => {
  19. "use strict";
  20.  
  21. initPanel();
  22.  
  23. function initPanel() {
  24. function genChk(name, title, checked = false) {
  25. const l = document.createElement("label");
  26. const i = document.createElement("input");
  27. const s = document.createElement("span");
  28. s.textContent = name;
  29. i.title = title;
  30. i.type = "checkbox";
  31. i.checked = checked;
  32. l.title = title;
  33. l.className = "fti_chk";
  34. l.appendChild(i);
  35. l.appendChild(s);
  36. return [l, i];
  37. }
  38. function genBtn(text, title, onclick) {
  39. let btn = document.createElement("button");
  40. btn.textContent = text;
  41. btn.title = title;
  42. btn.className = "fti_btn";
  43. btn.addEventListener("click", onclick);
  44. return btn;
  45. }
  46.  
  47. const [lblAlert, chkAlert] = genChk("直接回收", "回收前是否弹出提示框", false);
  48. const btnAllTradeIn = genBtn("全部回收", "回收所有选中的游戏", doTradeIn);
  49.  
  50. const tradeInLinks = document.querySelectorAll("a[href^='account_buyback']");
  51. for (let link of tradeInLinks) {
  52. const href = link.href;
  53. link.href = "#";
  54. link.data = href;
  55.  
  56. link.addEventListener("click", async (e) => {
  57. e.preventDefault();
  58.  
  59. if (!chkAlert.checked && !confirm("确定要回收吗?")) {
  60. return;
  61. }
  62.  
  63. const result = await makeTradeIn(href);
  64. if (result === 200) {
  65. link.style.textDecoration = "line-through";
  66. link.disabled = true;
  67. }
  68. });
  69. }
  70.  
  71. async function doTradeIn() {
  72. if (!chkAlert.checked && !confirm("确定要回收吗?")) {
  73. return;
  74. }
  75.  
  76. for (let link of tradeInLinks) {
  77. if (link.disabled) {
  78. continue;
  79. }
  80.  
  81. const result = await makeTradeIn(link.data);
  82. if (result === 200) {
  83. link.style.textDecoration = "line-through";
  84. link.disabled = true;
  85. }
  86. }
  87. }
  88.  
  89. if (tradeInLinks.length > 0) {
  90. document.body.appendChild(lblAlert);
  91. document.body.appendChild(btnAllTradeIn);
  92. }
  93. }
  94.  
  95. function makeTradeIn(url) {
  96. return new Promise((resolve, reject) => {
  97. fetch(url, {
  98. method: "POST",
  99. credentials: "include",
  100. data: "send=Trade KEY",
  101. })
  102. .then(async (response) => {
  103. if (response.ok) {
  104. const text = await response.text();
  105. resolve(200);
  106. } else {
  107. resolve(response.status);
  108. }
  109. })
  110. .catch((err) => {
  111. console.error(err);
  112. resolve(-1);
  113. });
  114. });
  115. }
  116. })();
  117.  
  118. GM_addStyle(`
  119. .fti_chk {
  120. color: orange;
  121. }
  122. `);