Link Extractor

Extract and display links from web sites pages based on specific patterns

安装此脚本?
作者推荐脚本

您可能也喜欢Search Engine Replacement

安装此脚本
  1. // ==UserScript==
  2. // @name Link Extractor
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.6
  5. // @description Extract and display links from web sites pages based on specific patterns
  6. // @author SijosxStudio
  7. // @match *://*/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. console.log('Link Extractor started');
  15.  
  16. // Define URL patterns to match specific links
  17. const patterns = [
  18. 'https://*/.*',
  19. ];
  20.  
  21. // Function to check if a URL matches any of the defined patterns
  22. function matchesPattern(url) {
  23. return patterns.some(pattern => {
  24. const regex = new RegExp(pattern);
  25. const matches = regex.test(url);
  26. console.log(`Checking ${url} against pattern ${pattern}: ${matches}`);
  27. return matches;
  28. });
  29. }
  30.  
  31. // Regex to find URL-like strings within page text
  32. const urlRegex = /https?:\/\/[^ "]+/g;
  33.  
  34. // Create an overlay to display matching links
  35. const overlay = document.createElement('div');
  36. overlay.style = 'position: fixed; top: 10px; right: 10px; min-width: 300px; max-height: calc(100vh - 20px); background: rgba(255, 255, 255, 0.9); color: #333; padding: 15px; border: 1px solid #ccc; border-radius: 8px; box-shadow: 0 4px 8px rgba(0,0,0,0.1); z-index: 9999; white-space: nowrap; overflow-y: auto; font-size: 14px; font-family: Arial, sans-serif;';
  37. document.body.appendChild(overlay); // Append overlay to body
  38.  
  39. // Function to extract URLs from text and non-text elements
  40. function extractURLs() {
  41. const urls = [];
  42.  
  43. // Extract URLs from text nodes
  44. const walker = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT);
  45. let node;
  46. while (node = walker.nextNode()) {
  47. const matches = node.textContent.match(urlRegex);
  48. if (matches) {
  49. urls.push(...matches);
  50. }
  51. }
  52.  
  53. // Extract URLs from non-text elements (e.g., <a>, <iframe>, <p>)
  54. const elements = document.querySelectorAll('a[href], iframe[src]');
  55. elements.forEach(element => {
  56. const url = element.getAttribute('href') || element.getAttribute('src');
  57. if (url) {
  58. urls.push(url);
  59. }
  60. });
  61.  
  62. return urls;
  63. }
  64.  
  65. let isUpdating = false; // Flag to track if the update function is currently executing
  66.  
  67. // Function to update the overlay with matching links
  68. function updateOverlay() {
  69. if (isUpdating) return; // Skip if the function is already executing
  70.  
  71. isUpdating = true; // Set the flag to true to prevent recursive calls
  72.  
  73. // Temporarily disconnect the observer to avoid triggering it during overlay update
  74. observer.disconnect();
  75.  
  76. console.log('Updating overlay...');
  77. const potentialUrls = extractURLs(); // Extract URLs from text and non-text elements
  78. console.log('Potential URLs:', potentialUrls);
  79. const links = potentialUrls.filter(url => matchesPattern(url)); // Filter URLs by pattern
  80. console.log('Matching URLs:', links);
  81.  
  82. // Clear previous content
  83. overlay.textContent = '';
  84.  
  85. if (links.length > 0) {
  86. const ul = document.createElement('ul');
  87. links.forEach(link => {
  88. const li = document.createElement('li');
  89. const a = document.createElement('a');
  90. a.href = link;
  91. a.textContent = link;
  92. a.style.color = 'lightblue';
  93. li.appendChild(a);
  94. ul.appendChild(li);
  95. });
  96. overlay.appendChild(ul);
  97. } else {
  98. overlay.textContent = 'No matching links found.';
  99. }
  100.  
  101. // Adjust width to fit content or screen width
  102. overlay.style.width = `${Math.min(overlay.offsetWidth, window.innerWidth)}px`;
  103.  
  104. isUpdating = false; // Reset the flag after the update is complete
  105.  
  106. // Reconnect the observer after updating the overlay
  107. observer.observe(document.body, { childList: true, subtree: true });
  108. }
  109.  
  110. // Use a mutation observer to detect changes to the DOM
  111. const observer = new MutationObserver(() => {
  112. clearTimeout(updateTimeout); // Clear the previous timeout
  113. updateTimeout = setTimeout(updateOverlay, 500); // Set a new timeout
  114. });
  115.  
  116. // Initial update
  117. updateOverlay();
  118. })();