磁力搜自动采集

添加一个采集按钮到页面右上角,执行自动采集解析规则的操作

目前为 2019-11-13 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name 磁力搜自动采集
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description 添加一个采集按钮到页面右上角,执行自动采集解析规则的操作
  6. // @homeurl https://github.com/dengyuhan/LardMonkeyScripts
  7. // @author denghaha
  8. // @match *://*/*
  9. // @grant none
  10. // ==/UserScript==
  11. (function () {
  12. 'use strict'
  13.  
  14. let button = document.createElement('button')
  15. button.innerText = "采集"
  16. button.style.position = 'absolute'
  17. button.style.top = '0'
  18. button.style.right = '0'
  19. button.style.zIndex = '99999'
  20. button.style.borderRadius = '4px'
  21. button.style.color = "#606266"
  22. button.style.border = "1px solid #dcdfe6"
  23. button.style.backgroundColor = "#fff"
  24. button.style.opacity = "0.6"
  25. button.onclick = exec
  26. document.body.appendChild(button)
  27.  
  28. function exec () {
  29. // 分析出关键词
  30. let inputNodes = document.getElementsByTagName('input')
  31. let keyword
  32. for (let i = 0; i < inputNodes.length; i++) {
  33. const value = inputNodes[i].value
  34. if (value && inputNodes[i].style.display !== "none" && inputNodes[i].style.visibility !== "hidden") {
  35. keyword = value
  36. break
  37. }
  38. }
  39. if (!keyword) {
  40. return
  41. }
  42.  
  43. // 遍历所有超链接 找出第一个包含关键词的节点
  44. let aNode
  45. let aNodes = document.getElementsByTagName('a')
  46. for (let i = 0; i < aNodes.length; i++) {
  47. if (aNodes[i].innerText.indexOf(keyword) !== -1) {
  48. aNode = aNodes[i]
  49. break
  50. }
  51. }
  52. if (!aNode) {
  53. return
  54. }
  55.  
  56. const dateRegx = '(\\d{4})-(\\d{1,2})-(\\d{1,2})|ago|前'
  57. const sizeRegx = '\\d.*(字节|bytes|KB|MB|GB|TB|PB|EB|ZB|YB)'
  58. const magnetRegx = '(magnet:\\?xt).*'
  59. const hotRegx = '^\\d+$'
  60.  
  61. // 递归向上找出group
  62. function findItemGroupNode (node) {
  63. const parentNode = node.parentNode
  64. const regx = new RegExp(`(?=[\\s\\S]*${keyword})(?=[\\s\\S]*(${dateRegx}))(?=[\\s\\S]*(${sizeRegx}))^[\\s\\S]*$`, 'gi')
  65. if (regx.test(parentNode.innerHTML)) {
  66. return parentNode
  67. } else {
  68. return findItemGroupNode(parentNode.parentNode)
  69. }
  70. }
  71.  
  72. const childNodes = []
  73.  
  74. function findAllChildNode (node) {
  75. let children = node.children
  76. for (let i = 0; i < children.length; i++) {
  77. // 递归遍历
  78. childNodes.push(children[i])
  79. if (children[i].children.length > 0) {
  80. findAllChildNode(children[i])
  81. }
  82. }
  83. }
  84.  
  85. function findItemValueNode (regx) {
  86. for (let i = childNodes.length - 1; i >= 0; i--) {
  87. // 先检查文字
  88. if (regx.test(childNodes[i].innerText)) {
  89. return {
  90. node: childNodes[i],
  91. attrPath: ''
  92. }
  93. }
  94.  
  95. // 再检查属性
  96. let attributeNames = childNodes[i].getAttributeNames()
  97. for (let j = 0; j < attributeNames.length; j++) {
  98. if (regx.test(childNodes[i].getAttribute(attributeNames[j]))) {
  99. return {
  100. node: childNodes[i],
  101. attrPath: `/@${attributeNames[j]}`
  102. }
  103. }
  104. }
  105. }
  106. }
  107.  
  108. function getRootPathTo (element) {
  109. let tagName = element.tagName.toLowerCase()
  110. if (element.id) {
  111. return `${tagName}[@id='${element.id}']`
  112. }
  113. if (element.className) {
  114. return `${tagName}[@class='${element.className}']`
  115. }
  116. let parentNode = element.parentNode
  117. if (parentNode.className) {
  118. return `${parentNode.tagName.toLowerCase()}[@class='${parentNode.className}']/${tagName}`
  119. }
  120. if (element === document.body) {
  121. return tagName
  122. }
  123. return getNumberPathTo(element)
  124. }
  125.  
  126. function getNumberPathTo (element) {
  127. let tagName = element.tagName.toLowerCase()
  128. if (element === document.body) {
  129. return tagName
  130. }
  131. let ix = 0
  132. let siblings = element.parentNode.childNodes
  133. for (let i = 0; i < siblings.length; i++) {
  134. let sibling = siblings[i]
  135. if (sibling === element) {
  136. return `${getNumberPathTo(element.parentNode)}/${tagName}[${ix + 1}]`
  137. }
  138. if (sibling.nodeType === 1 && sibling.tagName.toLowerCase() === tagName) {
  139. ix++
  140. }
  141. }
  142. }
  143.  
  144. function findSortPaths (rootUrl) {
  145. function formatPath (path) {
  146. return decodeURIComponent(path)
  147. .replace(new RegExp(`${keyword}`, 'gi'), '{k}')
  148. .replace(/\d+/, '{p}')
  149. }
  150.  
  151. let preset = formatPath(window.location.href.replace(rootUrl, ''))
  152. const paths = {preset}
  153.  
  154. const sortRegx = {
  155. time: /.*时间.*|.*time.*/gi,
  156. size: /.*大小.*|.*size.*/gi,
  157. hot: /..*点击.*|.*人气.*|.*次数.*|.*hot.*|.*count.*|.*click.*/gi
  158. }
  159. let aNodes = document.getElementsByTagName('a')
  160. for (let i = 0; i < aNodes.length; i++) {
  161. let linkText = aNodes[i].innerText
  162. let href = aNodes[i].getAttribute('href')
  163. for (let key in sortRegx) {
  164. if (href && sortRegx[key].test(linkText)) {
  165. const keyPath = formatPath(href)
  166. if (keyPath !== preset) {
  167. paths[key] = keyPath
  168. }
  169. }
  170. }
  171. }
  172. return paths
  173. }
  174.  
  175. const groupNode = findItemGroupNode(aNode)
  176. if (!groupNode) {
  177. return
  178. }
  179. findAllChildNode(groupNode)
  180. const dateWrapper = findItemValueNode(new RegExp(dateRegx, 'gi'))
  181. const sizeWrapper = findItemValueNode(new RegExp(sizeRegx, 'gi'))
  182. const magnetWrapper = findItemValueNode(new RegExp(magnetRegx, 'gi'))
  183. const hotWrapper = findItemValueNode(new RegExp(hotRegx, 'gi'))
  184.  
  185. let hostnameArray = window.location.hostname.split('.')
  186. const id = hostnameArray[Math.floor(hostnameArray.length / 2)]
  187. const url = `${window.location.protocol}//${window.location.host}`
  188. const title = document.title
  189. const paths = findSortPaths(url)
  190.  
  191. // xpath
  192. const groupNumberPath = getNumberPathTo(groupNode)
  193. const group = `//${getRootPathTo(groupNode)}`
  194. const magnet = `.${getNumberPathTo(magnetWrapper.node).replace(groupNumberPath, '')}${magnetWrapper.attrPath}`
  195. const name = `.${getNumberPathTo(aNode).replace(groupNumberPath, '')}`
  196. const size = `.${getNumberPathTo(sizeWrapper.node).replace(groupNumberPath, '')}`
  197. const date = `.${getNumberPathTo(dateWrapper.node).replace(groupNumberPath, '')}`
  198. const hot = `.${getNumberPathTo(hotWrapper.node).replace(groupNumberPath, '')}`
  199. const xpath = {
  200. group, magnet, name, size, date, hot
  201. }
  202. const item = {
  203. id, name: title, url, paths, xpath
  204. }
  205.  
  206. console.info('关键词:%s', keyword)
  207. console.info(groupNode)
  208. console.info(aNode)
  209. console.info(magnetWrapper.node)
  210. console.info(dateWrapper.node)
  211. console.info(sizeWrapper.node)
  212. console.info(hotWrapper.node)
  213.  
  214. const lowVer = {
  215. site: item.name,
  216. group: item.xpath.group,
  217. magnet: item.xpath.magnet,
  218. name: item.xpath.name,
  219. size: item.xpath.size,
  220. date: item.xpath.date,
  221. hot: item.xpath.hot,
  222. url: item.url,
  223. paths: item.paths
  224. }
  225.  
  226. const xVerUrl = item.url + item.paths[Object.keys(item.paths)[0]]
  227. const xVer = {
  228. site: item.name,
  229. waiting: "0",
  230. group: item.xpath.group,
  231. magnet: item.xpath.magnet,
  232. name: item.xpath.name,
  233. size: item.xpath.size,
  234. count: item.xpath.date,
  235. source: xVerUrl.replace(/{k}/g, 'XXX').replace(/{p}/g, 'PPP'),
  236. }
  237. console.info('\nmagnetX 规则如下')
  238. console.info(JSON.stringify(xVer, '\t', 2))
  239. console.info('\nmagnetW 2.x 规则如下')
  240. console.info(JSON.stringify(lowVer, ' ', 2))
  241. console.info('\nmagnetW 3.x 规则如下')
  242. console.info(JSON.stringify(item, '\t', 2))
  243. }
  244. })()

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址