Test_auto reply

Test_自动回复_防止原作者删库

  1. // ==UserScript==
  2. // @name Test_auto reply
  3. // @namespace https://github.com/ewigl/hus
  4. // @version 1.0
  5. // @description Test_自动回复_防止原作者删库
  6. // @author Jack Ou
  7. // @license MIT
  8. // @homepage https://github.com/ewigl/hus
  9. // @match http*://www.hifini.com/thread-*.htm
  10. // @match http*://*.lanzn.com/*
  11. // @icon https://www.hifini.com/favicon.ico
  12. // @grant GM_addStyle
  13. // ==/UserScript==
  14.  
  15. ;(function () {
  16. 'use strict'
  17.  
  18. // 常量
  19. const constants = {
  20. ASIDE_CLASS: 'aside',
  21.  
  22. QUICK_REPLY_BUTTON_ID: 'hus_quick_reply_button',
  23. QUICK_REPLY_FORM_ID: 'quick_reply_form',
  24. QUICK_REPLY_INPUT_ID: 'message',
  25. QUICK_REPLY_SUBMIT_ID: 'submit',
  26.  
  27. NON_REPLY_CLASS: 'alert-warning',
  28. REPLIED_CLASS: 'alert-success',
  29.  
  30. DOWNLOAD_LINKS_PANEL_ID: 'hus_download_links_panel',
  31.  
  32. BAIDU_HOST: 'pan.baidu.com',
  33. LANZOU_HOST: 'lanzn.com',
  34.  
  35. URL_PARAMS_PWD: 'pwd',
  36. LANZOU_PWD_INPUT_ID: 'pwd',
  37.  
  38. USER_LOGIN_URL: '/user-login.htm',
  39. }
  40.  
  41. // 自定义样式
  42. const styleCSS = `
  43. #${constants.QUICK_REPLY_BUTTON_ID} {
  44. position: sticky;
  45. top: 16px;
  46. }
  47.  
  48. #${constants.DOWNLOAD_LINKS_PANEL_ID} {
  49. position: sticky;
  50. top: 60px;
  51. }
  52. `
  53.  
  54. // 应用自定义样式
  55. GM_addStyle(styleCSS)
  56.  
  57. // 默认配置
  58. const config = {
  59. // 回复内容
  60. replies: ['666', 'Good', 'Nice', 'Thanks', '给力', '谢谢', '谢谢分享', '谢谢大佬', '感谢', '感谢分享', '感谢大佬'],
  61. }
  62.  
  63. // 工具
  64. const utils = {
  65. // 获取随机回复(replies)
  66. getRandomReply() {
  67. return config.replies[Math.floor(Math.random() * config.replies.length)]
  68. },
  69. // 判断当前帖是否已回复
  70. isReplied() {
  71. return $(`.${constants.REPLIED_CLASS}`).length > 0
  72. },
  73. isBaiduOrLanzou(url) {
  74. if (url.includes(constants.BAIDU_HOST)) {
  75. return '百度'
  76. } else if (url.includes(constants.LANZOU_HOST)) {
  77. return '蓝奏'
  78. }
  79. return '未知'
  80. },
  81. isInLanzouSite() {
  82. return location.host.includes(constants.LANZOU_HOST)
  83. },
  84. // “解密”提取码
  85. getHiddenPwd(element) {
  86. // 若无子元素,则无“加密”
  87. if ($(element).children().length === 0) {
  88. return $(element).text().trim().replace('提取码', '').replace(':', '').replace(':', '')
  89. }
  90.  
  91. // 若有子元素,则有“加密”
  92. let pwd = ''
  93.  
  94. $(element)
  95. .find('span')
  96. .each((_index, innerElement) => {
  97. if (!($(innerElement).css('display') === 'none')) {
  98. pwd += $(innerElement).text()
  99. }
  100. })
  101.  
  102. return pwd
  103. },
  104. getLinkItems() {
  105. let netDiskLinks = utils.getAllNetDiskLinks()
  106. let pwds = utils.getAllPwds()
  107.  
  108. // 若链接与密码数量不等,则抛错(暂定)
  109. if (netDiskLinks.length !== pwds.length) {
  110. throw new Error('HIFINI User Script: netDiskLinks.length !== pwds.length')
  111. }
  112.  
  113. return netDiskLinks.map((link, index) => {
  114. return {
  115. // split 以兼容不规范 url
  116. link: link.split('?')[0] + '?pwd=' + pwds[index],
  117. pwd: pwds[index],
  118. type: utils.isBaiduOrLanzou(link),
  119. }
  120. })
  121. },
  122. // 获取页面内所有(a 标签)网盘链接(百度、蓝奏)
  123. getAllNetDiskLinks() {
  124. return $(`a[href*="${constants.BAIDU_HOST}"], a[href*="${constants.LANZOU_HOST}"]`)
  125. .toArray()
  126. .map((element) => {
  127. return element.href
  128. })
  129. },
  130. // 获取页面内所有提取码(alert-success)
  131. getAllPwds() {
  132. let pwdElements = $(`.${constants.REPLIED_CLASS}`)
  133.  
  134. let pwdArray = []
  135.  
  136. pwdElements.each((_index, element) => {
  137. utils.getHiddenPwd(element) && pwdArray.push(utils.getHiddenPwd(element))
  138. })
  139.  
  140. return pwdArray
  141. },
  142. }
  143.  
  144. const operation = {
  145. // 快速回复当前帖,模拟点击操作方式。
  146. quickReply() {
  147. const replyInputDom = $(`#${constants.QUICK_REPLY_INPUT_ID}`)
  148. const submitButtonDom = $(`#${constants.QUICK_REPLY_SUBMIT_ID}`)
  149.  
  150. if (replyInputDom.length) {
  151. replyInputDom.focus()
  152. replyInputDom.val(utils.getRandomReply())
  153.  
  154. submitButtonDom.click()
  155.  
  156. // or
  157. // $("#quick_reply_form").submit();
  158. } else {
  159. console.log('Need to Login.')
  160. window.location.href = constants.USER_LOGIN_URL
  161. }
  162.  
  163. // 可选, Ajax 方式
  164. // To do, or not to do, that is the question.
  165. },
  166. }
  167.  
  168. const initAction = {
  169. addQuickReplyButton() {
  170. const quickReplyButtonDom = `<a id="${constants.QUICK_REPLY_BUTTON_ID}" class="btn btn-light btn-block mb-3"> 自动回复 </a>`
  171. $(`.${constants.ASIDE_CLASS}`).append(quickReplyButtonDom)
  172.  
  173. $(document).on('click', `#${constants.QUICK_REPLY_BUTTON_ID}`, operation.quickReply)
  174. },
  175. addNetDiskLinksPanel() {
  176. let linkItems = utils.getLinkItems()
  177.  
  178. let linksDom = ''
  179.  
  180. linkItems.forEach((item) => {
  181. linksDom += `
  182. <a class="btn btn-light btn-block" href="${item.link}" target="_blank"> ${item.type} / ${item.pwd} </a>`
  183. })
  184.  
  185. const downloadPanelDom = `
  186. <div id="${constants.DOWNLOAD_LINKS_PANEL_ID}" class="card">
  187. <div class="m-3 text-center">
  188. ${linksDom}
  189. </div>
  190. </div>
  191. `
  192.  
  193. $(`.${constants.ASIDE_CLASS}`).append(downloadPanelDom)
  194. },
  195. autoFillLanzouPwd() {
  196. const urlParams = new URLSearchParams(window.location.search)
  197.  
  198. if (urlParams.has(constants.URL_PARAMS_PWD)) {
  199. let pwd = urlParams.get(constants.URL_PARAMS_PWD)
  200.  
  201. $(`#${constants.LANZOU_PWD_INPUT_ID}`).val(pwd)
  202. }
  203. },
  204. }
  205.  
  206. // Main
  207. const main = {
  208. init() {
  209. if (utils.isInLanzouSite()) {
  210. // 自动填充蓝奏网盘提取码
  211. initAction.autoFillLanzouPwd()
  212. } else {
  213. initAction.addQuickReplyButton()
  214.  
  215. utils.isReplied() && initAction.addNetDiskLinksPanel()
  216. }
  217.  
  218. console.log('HIFINI User Script is ready.')
  219. },
  220. }
  221.  
  222. main.init()
  223. })()