获取网页中的全部链接

获取网页中的全部链接。鼠标右键 -> tampermonkey -> "Get All Links"。

目前为 2022-09-26 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Get All Links
  3. // @name:zh-CN 获取网页中的全部链接
  4. // @namespace xcl
  5. // @version 1.0
  6. // @description Get all links from a website. right-click -> tampermonkey -> "Get All Links".
  7. // @description:zh-CN 获取网页中的全部链接。鼠标右键 -> tampermonkey -> "Get All Links"。
  8. // @author xcl
  9. // @match *://*/*
  10. // @grant none
  11. // @run-at context-menu
  12. // ==/UserScript==
  13.  
  14. const filter_results = false;
  15. const filter_regex = new RegExp(/png|jpg/g);
  16.  
  17. function make_table(results) {
  18. let table = "<table><thead><th>Names</th><th>Links</th></thead><tbody>";
  19. results.forEach(result => {
  20. table += `<tr><td> ${result.name} </td><td> ${result.url} </td></tr>`;
  21. });
  22. table += "</table>";
  23. window.open("").document.write(table);
  24. }
  25.  
  26. function make_list(results) {
  27. let list = "";
  28. results.forEach(result => {
  29. list += `<div>${result.url}</div>`;
  30. });
  31.  
  32. window.open("").document.write(list);
  33. }
  34.  
  35. function filter_link(link) {
  36. if (!!link.match(filter_regex)) {
  37. return true;
  38. }
  39. return false;
  40. }
  41.  
  42. function get_links() {
  43. let urls = document.querySelectorAll("a");
  44. let results = [];
  45. urls.forEach(url => {
  46. let link_name = url.textContent.replace(/\t|\s+/g, "").trim();
  47. let link = url.href;
  48. if (!filter_results) {
  49. results.push({
  50. name: link_name,
  51. url: link
  52. });
  53. } else if (filter_link(link)) {
  54. results.push({
  55. name: link_name,
  56. url: link
  57. });
  58. }
  59. });
  60. // make_list(results);
  61. make_table(results);
  62. }
  63.  
  64. (function () {
  65. "use strict";
  66. get_links();
  67. })();