2

Probably I'm having a misunderstanding about how to use the browser_action, but basically I need to trigger an action on the actual tab opened on the browser via the popup.html opened when I click my extension icon and inside of this html I have a button for this trigger.

So i clicked on the button inside the html on my extension, and i want to trigger a console.log on the opened tab at my browser.

This is my code below, what I'm doing wrong?

manifest.json

{
  "name": "Test",
  "description": "Test",
  "version": "1.0.0",
  "manifest_version": 2,
  "minimum_chrome_version": "46",
  
  "permissions": ["tabs", "<all_urls>"],
 
  "background":{
    "scripts":["script.js"]
  },
  "browser_action": {
    "default_title": "Test",
    "default_popup": "popup.html",  
    "default_icon": {
      "16": "/images/icon-16.png",
      "48": "/images/icon-48.png",
      "128": "/images/icon-128.png"
    }
  },
  
}

script.js

document.getElementById("myButton").addEventListener("click", test);
function test() {
  alert("Message")
}

popup.html

<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-    scale=1.0">
    <title>Document</title>
  </head>
<body>
  <button id="myButton"> TEST </button>

  <script type="text/javascript" src="./script.js" ></script>
</body>
</html>

What i want: enter image description here

  • You display a message "CLIQUE I" but I do not see that in your code, is that a different event? – Eduardo Jun 15 '22 at 14:45
  • I have never built a chrome extension, but I did a quick search for browser_actions, and it says that is used to add icons to the toolbar, maybe that means that you should not be adding that "Test" button but look for your button on the toolbar? – Eduardo Jun 15 '22 at 14:48
  • Here is the browserAction reference: https://developer.chrome.com/docs/extensions/reference/browserAction/ – Eduardo Jun 15 '22 at 14:49
  • @Eduardo sorry i changed the text inside the button, now its right – Guilherme Gonzalez Jun 15 '22 at 15:10
  • The problem is, I not understand how to trigger my console.log on the tab console instead of the console at the extension html. – Guilherme Gonzalez Jun 15 '22 at 15:12
  • Use a [content script](https://developer.chrome.com/docs/extensions/mv3/content_scripts/). – wOxxOm Jun 15 '22 at 16:20
  • Check this maybe give you the answer: https://stackoverflow.com/questions/45813175/how-can-i-console-log-to-parent-window – Eduardo Jun 15 '22 at 16:20
  • @Eduardo i tried and didnt work, i wil try to study the "content script" and see if i can do what i want, but for now the question keeps unanswered – Guilherme Gonzalez Jun 15 '22 at 21:04

0 Answers0