0

I'm trying to send a message to the active tab in Chrome

function sendMessageToCurrentTab(){
    var args = Array.prototype.slice.call(arguments); //Get arguments as an array
    if(typeof browser !== 'object' || typeof args[args.length - 1] === 'function') {
        //Either is Chrome, or we have a callback function
        chrome.tabs.query({active:true,currentWindow:true},function(tabs){
            args.unshift(tabs[0].id); //Add tab ID to be the new first argument.
            chrome.tabs.sendMessage.apply(this,args);
        });
        return;
    }//else

    return browser.tabs.query({active:true,currentWindow:true}).then(function(tabs){
        args.unshift(tabs[0].id); //Add tab ID to be the new first argument.
        return browser.tabs.sendMessage.apply(this,args);
    });
}

chrome.runtime.onMessage.addListener((command) => {
    chrome.tabs.executeScript({
        file:'core.js'
    }, function(){
        sendMessageToCurrentTab("test");
    });
});

but it doesn't seem to be working. Why? In Firefox it worked, but I changed the event listener to the chrome.runtime.onMessage.addListener here to make it working, so that's probably the problem, but with what should I change it?

  • Use the devtools debugger instead of guessing. Also, `slice` is [not safe](https://github.com/petkaantonov/bluebird/wiki/Optimization-killers#3-managing-arguments) on `arguments`, use ES6 rest and spread via `...`. And consider switching to the [mozilla's `browser` polyfill](https://github.com/mozilla/webextension-polyfill). – wOxxOm Mar 17 '17 at 18:12
  • 1
    Why are you testing for the existence of `browser`? Firefox supports `chrome.*` APIs directly, there is no reason to have code that separately uses `browser.*`. If you *really* want to use Promises, then follow wOxxOm's advice and use Mozilla's polyfill (note that it can have bugs, which may introduce yet another layer of possible issues). – Makyen Mar 17 '17 at 18:43
  • @Makyen well, [ask that yourself](http://stackoverflow.com/a/41205480/7728288) :'). I've searched on Google that for Chrome, I've found that link yesterday, and I've saved it on my bookmarks. Today, I've tested it for Chrome and it didn't work. – willKenneth37 Mar 17 '17 at 19:05
  • @willKenneth37, hmmm.. Well, clearly I need to revisit that question/answer. I don't remember off the top of my head why I was not just using the `chrome.*` versions (which is what I've generally done in answers before and after that). Give me a few minutes to determine why I was doing something strange for that answer. – Makyen Mar 17 '17 at 19:20
  • I took a meal break. While eating, I remembered why I wrote that function that way: It's intended to be usable in multiple environments where the overall program was using *either* Promises (`browser.*`) or callbacks (`chrome.*`). It returns a Promise when it thinks the calling code expects it. I should have explained that better in the answer. While "neat", it is better for me to provide two different versions (one based on promises, the other callbacks), rather than leave something around which is potentially confusing to people. I will update that answer. Thanks for pointing it out. – Makyen Mar 17 '17 at 20:36
  • BTW: If you copy content from questions or answers on Stack Exchange, you need to provide attribution. That is a requirement of the [CC BY-SA 3.0 license](https://creativecommons.org/licenses/by-sa/3.0/) under which all contributions to SE / SO are made. I'm not upset, or anything. I just want you to know. All you really need to do is [edit] the question to include a link. Copying it with attribution is perfectly fine, particularly when you have a question. Having it be without attribution could result in the question being deleted, if reported. – Makyen Mar 17 '17 at 20:42
  • Please provide a complete [mcve], including a *manifest.json* and at least enough of your *core.js* code to duplicate the problem. Please also explain *exactly* what you mean by "doesn't seem to be working" (e.g. You sent message, but it wasn't received, etc.). Is this code in a popup? Your background script? – Makyen Mar 17 '17 at 20:44
  • Related: [Pass a parameter to a content script injected using chrome.tabs.executeScript()](http://stackoverflow.com/a/40815514) – Makyen Mar 17 '17 at 20:45
  • Is your *core.js* the same as in the question you linked? – Makyen Mar 17 '17 at 21:09
  • @Makyen no. I've just copied/pasted it, but I've changed something. – willKenneth37 Mar 17 '17 at 21:11
  • I've updated that answer to have two code versions: one for callbacks (`chrome.*`), the other for Promises (`browser.*`). I tested the callback version in both Firefox and Chrome. The Promises version I only tested in Firefox. However, I re-tested the original code (what you copied) in both Chrome and Firefox and it worked just fine. Thus, there is something else going on. Please provide a [MCVE](http://stackoverflow.com/help/mcve), including all of *manifest.json*, an indication about how you load/use the code already in the question and *core.js*. We need enough to be able to duplicate. – Makyen Mar 17 '17 at 22:05
  • If you do update this with a [mcve] which duplicates the problem, please do ping me by leaving a comment with `@Makyen`. – Makyen Mar 17 '17 at 23:39

0 Answers0