batch magnet selection

batch select magnet in a webpage (dmhy, nyaa), and then copy to clipboard (ctrl+c), new page(ctrl+b) for download

  1. // ==UserScript==
  2. // @name batch magnet selection
  3. // @name:zh 磁力链接批量选择
  4. // @namespace http://tampermonkey.net/
  5. // @version v0.1.1
  6. // @description batch select magnet in a webpage (dmhy, nyaa), and then copy to clipboard (ctrl+c), new page(ctrl+b) for download
  7. // @description:zh 批量选择bt站上当前页面上(动漫花园, nyaa)的批量链接, 并自动复制到剪贴板(ctrl+c)、新打开页面(ctrl+b) 用于下载
  8. // @author devseed
  9. // @match *://*.dmhy.org/*
  10. // @match *://*.nyaa.si/*
  11. // @icon https://www.dmhy.org/favicon.ico
  12. // @grant none
  13. // ==/UserScript==
  14.  
  15. (function() {
  16. 'use strict';
  17.  
  18. function add_checkbox() {
  19. var doms = document.querySelectorAll('a[href^=magnet]');
  20. for (let i=0; i< doms.length; i++) {
  21. let dom = doms[i];
  22. let html = `<input type="checkbox" checked="true" href="${dom.href}"/>`;
  23. dom.insertAdjacentHTML("afterend", html);
  24. }
  25. };
  26.  
  27. function add_button() {
  28. document.body.insertAdjacentHTML("afterbegin", `<input type="button" value="SendClipboard" onclick="send_magnets_clipboard()" style="position fixed; margin:0px 10px 0px 0px">`);
  29. document.body.insertAdjacentHTML("afterbegin", `<input type="button" value="SendNewpage" onclick="send_magnets_newpage()" style="position fixed; margin:0px 10px 0px 0px">`);
  30. document.body.insertAdjacentHTML("afterbegin", `<input type="button" value="SelectAll" onclick="check_magnets_all(true)" style="position fixed; margin:0px 10px 0px 0px">`);
  31. document.body.insertAdjacentHTML("afterbegin", `<input type="button" value="SelectNone" onclick="check_magnets_all(false)" style="position fixed; margin:0px 10px 0px 0px">`);
  32. };
  33.  
  34. function get_magnets(only_checked=true) {
  35. var magnets = [];
  36. var doms = document.querySelectorAll('input[href^=magnet]');
  37. for (let i=0; i< doms.length; i++) {
  38. let dom = doms[i];
  39. if (dom.checked || !only_checked) {
  40. magnets.push(dom.getAttribute("href"));
  41. }
  42. }
  43. return magnets;
  44. }
  45.  
  46. async function send_magnets_clipboard() {
  47. var magnets = get_magnets();
  48. if(!magnets || magnets.length == 0) {
  49. alert("no selected magnent!");
  50. return;
  51. }
  52. const text = magnets.join("\n");
  53. try {
  54. await navigator.clipboard.writeText(text);
  55. } catch (error) {
  56. console.error(error.message);
  57. }
  58. }
  59.  
  60. async function send_magnets_newpage() {
  61. var magnets = get_magnets();
  62. if(!magnets || magnets.length == 0) {
  63. alert("no selected magnent!");
  64. return;
  65. }
  66.  
  67. const html = magnets.join("<br>")
  68. const blob = new Blob([html], { type: "text/html" });
  69. const bloburl = URL.createObjectURL(blob);
  70. window.open(bloburl, "_blank");
  71. }
  72.  
  73. async function check_magnets_all(checked) {
  74. var doms = document.querySelectorAll('input[href^=magnet]');
  75. for (let i=0; i< doms.length; i++) {
  76. let dom = doms[i];
  77. dom.checked = checked;
  78. }
  79. }
  80.  
  81. document.onkeydown = async (e) => {
  82. if (e.ctrlKey && e.key=='c') {
  83. send_magnets_clipboard();
  84. }
  85. else if (e.ctrlKey && e.key=='b') {
  86. send_magnets_newpage();
  87. }
  88. }
  89.  
  90. window.onload = async (e) => {
  91. add_checkbox();
  92. add_button();
  93. };
  94. window.send_magnets_clipboard = send_magnets_clipboard;
  95. window.send_magnets_newpage = send_magnets_newpage;
  96. window.check_magnets_all = check_magnets_all;
  97.  
  98. })();