D
D
Dima2018-12-10 15:05:30
JavaScript
Dima, 2018-12-10 15:05:30

Opening page links from a button does not work in my Chrome extension, how can I fix it?

I made an extension, when you click, a pop-up with a button pops up and when you click on the button, all links with a certain class that are on this page open. The problem is that the extension does not work with the page, that is, it does not open links from it, but opens links only within itself (from the popup.html file).
Here is the popup.html markup

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Index</title>
  <script src="popup.js"></script>
</head>

<body>
  <button id="btnScreens">Open Screens</button>
</body>

</html>

Here is popup.js
document.addEventListener('DOMContentLoaded', function() {
  let openScreensBtn = document.getElementById('btnScreens');
  openScreensBtn.addEventListener('click', function() {
    let links = document.querySelectorAll('.link');
    let arrLinks = Array.from(links);
    let arrHref = [];
    for (let i = 0; i < arrLinks.length; i++) {
      arrHref.push(arrLinks[i].getAttribute('href'));
    };
    for (let i = 0; i < arrHref.length; i++) {
      window.open(arrHref[i], '_blank');
    };
  });
});

Here is manifest.json
{
    "name": "Ext",
    "description" : "",
    "version": "1.0",
    "manifest_version": 2,

    "browser_action": {
        "default_icon": "128.png",
        "default_popup": "popup.html"
    },

    "permissions": [
        "activeTab"
    ]
}

What am I doing wrong? The script itself outside the extension works

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
RAX7, 2018-12-10
@qpz

popup.js does not interact with the browser page, it only works with popup.html. Paste the code on the page as described here: https://gist.github.com/danharper/8364399

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question