0

(The question that this was closed for being a duplicate of does not answer my question; I need more help) I am trying to access the DOM for the website a user is using from a chrome extension. What I am trying to do is get the innerText from one element in the DOM using it's class name and then display it in the chrome extension window. Thanks for your help in advance! My code is listed below:

script.js

chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
    var variableOne = document.querySelector('.className').innerText;
    document.querySelector('#test').innerText = variableOne;
})

manifest.json

{
    "name": "Name",
    "description": "Description",
    "version": "1.0",
    "manifest_version": 3,
    "permissions": ["storage", "activeTab", "scripting", "tabs"],
    "host_permissions": [
      "*://*.example.com/*"
    ],
    "content_scripts": [
      {
        "matches": ["*://*.example.com/*"],
        "js": ["content_script.js"]
      }
    ],
    "action": {
      "default_popup": "popup.html"
    }
  }

popup.html

<!DOCTYPE html>
<html>
<head>
    <title>Chrome Extension</title>
    <meta charset="utf-8">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <div class="container mt-3" style="width: 450px;">
        <h2 class="text-center">Chrome Extension</h2>
        <table class="table table-bordered">
            <thead>
            <tr>
                <th>One</th>
                <th>Two</th>
                <th>Three</th>
                <th>Four</th>
            </tr>
            </thead>
            <tbody>
            <tr>
                <td id="test"></td>
                <td id="one"></td>
                <td id="two"></td>
                <td id="three"></td>
            </tr>
            </tbody>
        </table>
    </div>
</body>
</html>

<script src="script.js"></script>
  • Most answers of the linked post seem quite clear to me, the answer seems to be using `chrome.scripting.executeScript()` to execute scripts in the active tab. Your question isn't showing any of that. – robertklep Sep 17 '22 at 16:54
  • Hello, I am still confused on how to implement executeScript. I have tried it but it always throws an error. – user16355394 Sep 18 '22 at 15:00
  • Please update your question to show what your current code is doing, and what error you are getting. – robertklep Sep 18 '22 at 15:33

0 Answers0