0

I have a problem to pass text in input field of current opened page once button on extension popup is clicked.

When I click element with id "button" on popup nothing happens.

Here is mu manifest.js:

{
  "manifest_version":2,
  "name": "myextension",
  "version": "1.0.0",
  "browser_action": {
    "default_popup": "popup.html"
  },
  "background": {
    "service_worker": "background.js"
  },
  "permissions": [
    "storage",
    "activeTab",
    "<all_urls>",
    "tabs"
], "content_scripts": [
  {
    "matches": ["<all_urls>"],
    "js": ["content.js"]
  }
]

}

content.js:

chrome.runtime.onMessage.addListener((msg, sender, response) => {
    if(msg.command == "button"){
        document.querySelector('input#search').value = 'miki';
        console.log('sdsds');
    }
    return true;
});

popup.js:

  document.getElementById("button").addEventListener("click", function() {
    chrome.tabs.query({currentWindow: true, active: true}, function (tabs) {
        var activeTab = tabs[0];
        chrome.tabs.sendMessage(activeTab.id, {command: "button"});
    })
  });
johnnycode
  • 23
  • 4
  • You probably reloaded the extension on chrome://extensions page. This doesn't re-run content scripts so your sendMessage won't be received. Either [reinject content scripts explicitly](https://stackoverflow.com/questions/10994324/) or switch to executeScript instead of declaring in manifest.json. – wOxxOm Jul 16 '21 at 19:45

0 Answers0