Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to modify the content of popup page in chrome extension?

I am trying to create a simple chrome extension that has a button in the popup window, and when the user clicks that button, some text in that popup changes. Here is what I have:

popup.html file:

<!DOCTYPE html>
<html>
  <head></head>
  <body>
    <button id="myButton">Button</button>
    <p id="myText">hello</p>
    <script src="popup.js"></script>
  </body>
</html>

popup.js file:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

let myButton = document.getElementById("myButton");

myButton.addEventListener("click", async () => {
  let [tab] = await chrome.tabs.query({ active: true, currentWindow: true });

  chrome.scripting.executeScript({
    target: {tabId: tab.id},  // This is probably wrong???
    function: doSomething,
  });
});

function doSomething() {
  console.log("button is clicked");
  document.getElementById("myText").innerHtml = "foo bar";
}

I am able to see "button is clicked" in the console, but do not see the content of popup change from "hello" to "foo bar". I suspect this is because the target I used in executeScript is wrong, but I am not sure what’s the right way to fix it. Any thoughts?

>Solution :

It should be:

document.getElementById("myText").innerHTML = "foo bar";

instead of:

document.getElementById("myText").innerHtml = "foo bar";
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading