5

Based on the example from MDN, I've created my own extension that showing just a 'hi' message. I've included a file named "popup.js" via tag. But this file is not getting executed.
This is my manifest.json file:

{
  "manifest_version": 2,
  "name": "Ext",
  "version": "1.0",
  "description": "Shows a hi message.",
  "icons": {
    "48": "icons/ff.jpg"
  },
  "permissions": ["tabs"],
  "browser_action": {
    "default_icon": "icons/toggle-off.png",
    "default_title": "EXT",
    "default_popup": "popup/popup.html"
  }
}

This is my popup.html file:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h2>Hi</h2>
<script src="popup.js"></script>
</body>
</html>

There is only a single line in my popup.js file. That is,

console.warn("This is a warning");

But I can't see anything in the console. The pop-up is showing properly when I click on the extension icon.
I don't know what am missing here. Any help is appreciated!

Alex
  • 878
  • 1
  • 10
  • 25

1 Answers1

0

Finally, I find out what was the issue. To execute the code from the js file included via tag you need to specify that code in browser.tabs.executeScript and there is an error in my manifest.json file. I should specify the permission as activeTab instead of tabs.
So my modified manifest.json file is:

{
  "manifest_version": 2,
  "name": "Ext",
  "version": "1.0",
  "description": "Shows a hi message.",
  "icons": {
    "48": "icons/ff.jpg"
  },
  "permissions": ["activeTab"],
  "browser_action": {
    "default_icon": "icons/toggle-off.png",
    "default_title": "EXT",
    "default_popup": "popup/popup.html"
  }
}

and my popup.js file becomes:

browser.tabs.executeScript({code: `console.warn("This is a warning")`});

See the docs for more details.