1

i asked this question where i tried to make a JS script that automatically captures links in buttons in downlaod page of edge and save them to clipboard but another problem popped up, the string copied contains only first 5 or 6 links (There are hundreds of links in the page with scrolling aility 'on')

code:

    function findButton() {
  var buttons = document.querySelectorAll('button');
             var arr = [];
 for (var i = 0; i < 100 ;i++) {
     var elem = buttons[i++];
var text = elem.textContent || elem.innerText;
     arr.push(text);
 }
      return arr
}

var x = findButton().toString();
setTimeout(function(){navigator.clipboard.writeText(x);},5000);
alert(x);

inspect element:

<button class="c01209" id="open_link774" role="link" aria-label="https://link.springer.com/content/pdf/10.1007%2F978-94-007-2464-8.pdf">https://link.springer.com/content/pdf/10.1007%2F978-94-007-2464-8.pdf</button>
mohamed
  • 231
  • 1
  • 13
  • The links are real anchors or only text inside the button element? – Lars Flieger Dec 14 '21 at 17:59
  • @LarsFlieger, they are the inner text of button (not `a` element). i will post the inspect element – mohamed Dec 14 '21 at 18:14
  • Alright. Why do you want to storage them to clipboard? – Lars Flieger Dec 14 '21 at 18:17
  • @LarsFlieger, i want to paste the result to a text document containing all links that ihave downloaded because i want to make a script that compares these links to the link of any page i open and if it matches any one of them the script will give a warning. just see my first question and you will understand the erason behind this. – mohamed Dec 14 '21 at 18:20
  • I added an answer. You could simply copy all links from the console. Is that what you want? – Lars Flieger Dec 14 '21 at 18:22
  • @LarsFlieger, i replied to it – mohamed Dec 14 '21 at 18:40

1 Answers1

1

Try this:

const urls = document.querySelectorAll('button')
const result = Array.from(urls).map(url => url.textContent)

console.log(result)
<button role="link" aria-label="https://example-1.com">https://example-1.com</button>
<button role="link" aria-label="https://example-2.com">https://example-2.com</button>
<button role="link" aria-label="https://example-3.com">https://example-3.com</button>
<button role="link" aria-label="https://example-4.com">https://example-4.com</button>
<button role="link" aria-label="https://example-5.com">https://example-5.com</button>
<button role="link" aria-label="https://example-6.com">https://example-6.com</button>
Lars Flieger
  • 2,421
  • 1
  • 12
  • 34
  • your code worked in the same way mine's too (same result 5 to 6 links). I noticed something, the script captures first 5 to 6 links in the viewed portion of the page (download page has a scroll down bar in the right). +1 for shortening the code. – mohamed Dec 14 '21 at 18:31
  • @mohamed Can you share the page where you want to grab the links? Probably it's related to a visual render (the page only renders the elements in the viewport) – Lars Flieger Dec 14 '21 at 20:05
  • it is the downloads page of edge there is no link to it because its just edge://downloads/all (you can open it in the browser) . the whole page is rendered though – mohamed Dec 14 '21 at 20:20