2

I am trying to open a new tab in my chrome extension from the popup (which is working) and then also pass a message to the content script that will be injected into the newly opened tab. This code is being called in my popup.js after a button is clicked within the popup.

The tab is opening as expected, but the content script is not executing as expected. I am trying to tap into the callback function of the create method. The code inside the of the callback has been tested separately and works as expected when standing alone, I just cannot figure out how to bring these to things together properly. I thought maybe this was happening b/c the content script wasn't loaded yet, so I tried somethings to wait until the tab loaded with no luck. Below is the code I have been working on.

$("#submit_button").click(function() {
  chrome.tabs.create({url: "http://myurl.com", index: 0}, function(tab) {
    chrome.tabs.sendMessage(tab.id, {"value": "amazon_paste", "object": chrome.extension.getBackgroundPage().jsonOrderObj});
  });
});

Here is working code after commenting out the create tab and manually going to the url:

//chrome.tabs.create({url: "http://myurl.com", index: 0}, function(tab) {

        chrome.tabs.getSelected(null, function(tab) {
            chrome.tabs.sendMessage(tab.id, {"value": "amazon_paste", "object": chrome.extension.getBackgroundPage().jsonOrderObj});
        });
//});

Also here is my JSON manifest:

{
  "name": "Amazon Order Extension",
  "version": "2.0",
  "manifest_version": 2,
  "content_scripts": [
    {
      "matches": ["https://sellercentral.amazon.com/gp/orders-v2/confirm-shipment/*"],
      "js": ["jquery.js", "amazon_content_script.js"]
    },
    {
        "matches": ["http://myurl.com"],
        "js": ["jquery.js", "inventory_content_script.js"]
    }
  ],
  "permissions": ["background", "tabs"],
  "background": {
     "page": "background.html"
  },
  "description": "",
  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
  }
}
sowbug
  • 4,644
  • 22
  • 29
boldSOCKS
  • 21
  • 3
  • Can you confirm that the content script is actually working via an `alert()` or a `console.log`? And could you post your _manifest.json_? –  Jul 10 '13 at 02:26
  • Seems my situation is the same. At http://stackoverflow.com/q/17564324/1566008 I see callback from create behaves different then expected:( – sohel Jul 10 '13 at 07:49
  • Yes I have confirmed the content script is working via an alert. I commented out the open tab, manually went to the url in a new tab and ran the code and it worked as expected. @JeremiahMegel I added the working code example above. – boldSOCKS Jul 10 '13 at 15:14
  • @JeremiahMegel sorry I forgot to post my manifest earlier. It is posted now. – boldSOCKS Jul 10 '13 at 18:58
  • @boldSOCKS I'm having same issue. Any luck with this? Seems that creating a tab and sending a message to its content script is not a supported messaging pattern. – Luke W Jan 01 '15 at 16:26

1 Answers1

0

For "external" sites, it looks like you should have an "externally_connectable" key in your manifest.json:

"externally_connectable": {
  "matches": [
    "*://*.example.com/*"
  ]
}

Does adding this in work for you?

Source: http://developer.chrome.com/extensions/messaging#external-webpage

Josh
  • 1,997
  • 13
  • 21