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>
6
  • The links are real anchors or only text inside the button element? Commented Dec 14, 2021 at 17:59
  • @LarsFlieger, they are the inner text of button (not a element). i will post the inspect element Commented Dec 14, 2021 at 18:14
  • Alright. Why do you want to storage them to clipboard? Commented Dec 14, 2021 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. Commented Dec 14, 2021 at 18:20
  • I added an answer. You could simply copy all links from the console. Is that what you want? Commented Dec 14, 2021 at 18:22

1 Answer 1

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>

Sign up to request clarification or add additional context in comments.

3 Comments

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 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)
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.