S
S
Soul12021-12-14 17:10:22
JavaScript
Soul1, 2021-12-14 17:10:22

How to correctly handle the launch of a google chrome extension?

I'm trying to write extensions for google chrome, re-read the official documentation and guides in Russian, I can't find what the problem is.
There is a background.js file

spoiler
// Вызывается, когда пользователь нажимает на действие браузера.
chrome.browserAction.onClicked.addListener(function(tab) {
    // Отправить сообщение на активную вкладку
    chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
      var activeTab = tabs[0];
      chrome.tabs.sendMessage(activeTab.id, {"message": "clicked_browser_action"});
    });
});

script file
spoiler
chrome.runtime.onMessage.addListener(
    function(message, callback) {
      if (message == "licked_browser_action"){
        chrome.tabs.executeScript(alert('hello'));
      }
        return true
});

manifest.json file
spoiler
{
    "manifest_version": 2,
    "name": "test",
    "version": "1.0.0",
    "content_security_policy": "script-src 'self'; object-src 'self'",
    "background": {
      "scripts": ["background.js"]
    },
    "permissions": [
      "activeTab"
    ],
    "content_scripts": [
      {
        "matches": [
          "<all_urls>"
        ],
        "js": ["search_in_file.js", "test.js"]
      }
    ],
    "browser_action": {
      "default_icon": "icon.png",
      "default_popup": "popup.html"
    },
    "web_accessible_resources": [
      "search_in_file.js", "test.js"
    ]
}

There was a function in the script file, but for the test I simplified it to alert('hello'). And it was triggered by a condition, when it received the desired value "clicked_browser_action" in the message, I also removed this condition so that it always starts.

And at the moment, when you open the page for the first time and click on the extension icon, the alert we set is displayed, but when you click it again, it no longer works. When the page is reloaded, the alert is displayed immediately, without a click. And it doesn't work on click either.
How can I change the code so that the script executes every time the icon is clicked?

UPD: as it turned out, for the first time, the adjacent js file is executed, where there is only one line - alert('hello'). And in our script nothing works at all.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dollar, 2021-12-14
@dollar

To begin
"licked_browser_action"with, it does not coincide with "clicked_browser_action"
Well, check it step by step. The first thing is that the handler generally fires with every click, that is, we need to find out whether the click handler itself or something after it misfires. To do this, insert something like this: Well, and so on down the chain. According to the logs, it will be further visible where the plug is.
console.log("onClicked");

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question