Why I cannot intercept the message?

I have a background script where I send a message after I pressed hotkey but the content script does not intercept it. Why? I added breakpoint to the line with if (package.command) … and some other lines and nothing paused. The message is send successfully.

background script:

// Get current tab id
chrome.tabs.query(
    {
    active: true, 
    currentWindow: true // send message to the active window
    }, 
    function(tabs) {
      chrome.tabs.sendMessage( tabs[0].id, package  )    
    }

content script:

chrome.runtime.onMessage.addListener(
      function(package){    
       if (package.command) ...
       }
    )

This is because yesterday or maybe today’s morning it worked. I mean I could inject css styles to web-page when I pressed the hotkey. No I cannot.

In general I’d suggest that you try to get your extension to run in Chrome first, and then “port” it to Firefox. Chromes Dev-Tools support for WebExtensions is simply better.

In chrome you can:

  • Check whether your content script is actually running in the tab you want it to.
  • Set a break point in both the line with sendMessage in the background script to check the tabId, and in the first line of the message listener of the contnent script to see exactly what happens.

You can do the same in Firefox too, but in my experience it’s much easier in Chrome, especially when you reload the extension frequently.

I found the problem. I am trying to run the hotkey/command on file:/// not http://
so it seems it is impossible to inject the C.S. to file:/// html document.

I use this pattern:
“content_scripts”: [
{
“matches”: [":///*"],

The * as a protocol in a match pattern is specified to match http: and https: not file: or any other protocol

Should this work for both?

"matches": ["<all_urls>" ],

the Toolbox is so disgusting slow, that I cannot test it. I just wait till the file will load… and still not finished.

Edit:
Finally confirmed.

Solved.

Thanks

Regarding the question you edited away above:

the Toolbox is so disgusting slow, that I cannot test it

Yupp, that’s one reason why I’d use Chrome for most of the deugging

I have different Chrome. I have different icon for Menu, This is 3 horizontal lines. I see the hotkey for devtools is the same as in Firefox. And I had to click on The mode for developers checkbox, which is not visible on your screenshot because you expanded the menu.

Edit:
The chrome cries:

Too many shortcuts specified for ‘commands’: The maximum is 4.

Ugh…

Why they have 4 commands limit? I planned to use about 5 or 6 tools.

Well, Chrome has quite some weird limits …

Seems like you can’t test them all at once. But then again, just change them and press F5.

This is one thing about WebExtensions I’m very much afraid of.

  1. Mozilla promotes WebExtensions as the only way to develop add-ons.
  2. Add-on developers start to experiment on WebExtensions, of course.
  3. The developers feel G. Chrome as the norm and concentrate more and more of their development efforts on it.
  4. User experiences on G. Chrome improves (but not on Firefox) and more and more users move to it.
  5. Firefox needs to copy even more of G. Chrome and it loses most things that differentiate itself.

Obviously, we don’t want that. We certainly need somethings to avoid this possibility.

I share your concern. I think to counter this, the Firefox devs need to:

  1. Fix all the little bugs (or undocumented ‘features’) that make developing an extension for Firefox considerably more annoying than necessary.
  2. Improve the performance of Firefox in general and of WebExtensions. Which mostly means to press on with e10s. This is literally the only way that I see any future for Firefox at all. I have developed an extension that works very fast in chrome (page load times so low that you wouldn’t believe that the page was loaded and not just shown. In Firefox the same thing is so slow that it is almost unusable.
  3. Make the things that make Firefox great available to modern extensions. WebExtensions as they are now won’t be sufficient. If I have the choice between a fast browser with WebExtensions and a slow browser with WebExtensions, why would I choose Firefox? If I dislike Google I can still use Opera, or, now that that is being sold to Chinese, Vivaldi.

We will see if Mozilla is able to do so while Firefox still has some users …

2 Likes

It seems like they are not aware about that bugs, even that I have posted at least 10 bugs during last weeks. One of the project co-workers asked me to send the addon I use, but I cannot help when the addon is not finished.

My last bug reports:

Hi there,
any news if the development team removed the bugs?

You can go to the bug reports and see their status there. Since you filed them, you should be getting updates on their progress or resolution. Some bugs can take a long time to be resolved, unfortunately.