Skip to content Skip to sidebar Skip to footer

Chrome Call Function In Content Scripts From Background.js

I've read the documentation but I still haven't been able to get this working. Here is my manifest: { 'name':'app', 'version':'0.1', 'manifest_version':2, 'descript

Solution 1:

To call a function in the content script from the background page, you first need to know the tab id. contextMenus.onClicked event has a tab parameter containing that id. Then use message passing to do it.

For example, in your background page:

chrome.contextMenus.onClicked.addListener(function(info, tab) {
  if (tab)
    chrome.tabs.sendMessage(tab.id, {args: ...}, function(response) {
      // ...
    });
});

In your content script:

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
    sendResponse(myFunc(request.args));
});

Post a Comment for "Chrome Call Function In Content Scripts From Background.js"