-1

I create a simple code to extract URL from a website but it's not working when I click on icon extensions

My js code

var el = document.getElementById('myPopup');
el && el.addEventListener('click', yourFunction, false);
function yourFunction() {
    let x = document.querySelectorAll("a");
            let myarray = [];
            for (let i = 0; i < x.length; i++) {
                    let nametext = x[i].textContent;
                    let cleantext = nametext.replace(/\s+/g, " ").trim();
                    let cleanlink = x[i].href;
                    myarray.push([cleantext, cleanlink]);
            }
            function make_table() {
                    let links_table = "<table><thead><th>Name</th><th>Links</th></thead><tbody>";
                    for (let i = 0; i < myarray.length; i++) {
                            links_table += "<tr><td>" + myarray[i][0] + "</td><td>" + myarray[i][1] + "</td></tr>";
                    }
                    let w = window.open("");
                    w.document.write(links_table);
                    console.log(w);
                    console.log(links_table);
                    
                         
            }
            make_table();
}
yourFunction();

My HTML code

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    
    <title>Document</title>
</head>
<body>
    <dev>
    <button id="myPopup">Clickme</button>
 
    </dev>
    <script src="popup.js"></script>
</body>
</html>

when i click on Clickme the url not show me just empty table can you tell me where is the Error?

video imgur.com/a/0OS60qG

  • Please put your HTML and JavaScript in a single snippet (in their respective boxes) and make sure they work together so your problem can be reproduced within your question. – Jesse Sep 16 '22 at 21:29
  • Also "it's not working" is not a clear or valid explanation of behavior. How is it not working? What's it doing? What's it supposed to do? Are there any error message? If so what are they? – Jesse Sep 16 '22 at 21:32
  • i create a video i think it's so clear here https://imgur.com/a/0OS60qG – Hama Swede Sep 16 '22 at 21:52
  • i add js code into the console.log it's working but Extensions do not work – Hama Swede Sep 16 '22 at 21:54

1 Answers1

1

The error happens since the popup script does not have access to the list of "a" tags. When you ran it on the browser console it works cause you are manipulating the page content, meanwhile, on the popup, it only interacts with the popup.html page.

Content scripts should be the only scripts able to manipulate the page content, so to fix that you probably should send a message to a content script when clicking on the button, so the content script can scrap the links and send them as a response to the popup or output as you want.

This other question may help you to see how to send messages between files: