将注入的js中的消息传递给chrome扩展中的background.js

时间:2015-04-02 15:44:40

标签: google-chrome-extension

我将数据从注入的js传递给background.js

注入js name" getDOM.js"并在background.js

中注入此代码
chrome.contextMenus.onClicked.addListener(function(info, tab){
  chrome.tabs.executeScript(tab.id, {file: "getDOM.js"})
});

我想将数据从getDOM.js传递给background.js,所以在background.js中有一个函数将在单击上下文菜单时运行

这是background.js中的函数:

function SetLinks(info, tab) {

    chrome.extension.sendMessage({greeting: "GetURL"},
            function (response) {
                tabURL = response.navURL;
                alert(tabURL);
            });
}

你可以看到我试图发送按摩并希望得到tabURL(这只是为了测试)

在getDOM.js文件中

我写了这段代码:

chrome.extension.onMessage.addListener(function(request,sender,sendResponse)
{
    if( request.greeting === "GetURL" )
    {
            sendResponse( {navURL:'test'} );       
    }
});

所以必须返回navURL。但它不起作用

它反向运行,我的意思是如果我正在尝试发送请求"来自" getDOM.js它会工作。它接缝请求函数应该在getDOM.js中,否则它将无法正常工作。请帮帮我

我也是这样试过的:

background.js

function SetLinks(info, tab) {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
            chrome.tabs.sendMessage(tabs[0].id, {greeting: "GetURL"}, function(response) {
            alert(response.navURL);
      });
      });  
}

chrome.contextMenus.onClicked.addListener(SetLinks);

// Set up context menu tree at install time.
chrome.runtime.onInstalled.addListener(function() {
  // Create one test item for each context type.
  var contexts = ["page","selection","link","editable","image","video",
                  "audio"];
    var context = contexts[1];
    var title = "test";
    var id = chrome.contextMenus.create({"title": title, "contexts":[context],
                                         "id": "context" + context});

});

chrome.contextMenus.onClicked.addListener(function(info, tab){
  chrome.tabs.executeScript(tab.id, {file: "getDOM.js"})
});

getDOM.js

chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {

    if (request.greeting === "GetURL")
      sendResponse({navURL:'test'});
  });

但仍无法正常工作

2 个答案:

答案 0 :(得分:1)

从扩展程序/ background.js向内容脚本发送请求,您需要指定要将其发送到哪个选项卡。 https://developer.chrome.com/extensions/messaging

我创建了示例代码并对其进行了测试,如下所示:

<强> Background.js

chrome.contextMenus.create({"title": "sample text", "contexts":["page"],
                                         "id": "ViewAlert"
        });
  function contextClicked(info, tab) {
  if (info.menuItemId == "ViewAlert" ) {
        chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
            chrome.tabs.sendMessage(tabs[0].id, {greeting: "GetURL"}, function(response) {
            alert(response.navURL);
      });
      });  
    } 
};
chrome.contextMenus.onClicked.addListener(contextClicked);

<强> Content.js

chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {

    if (request.greeting === "GetURL")
      sendResponse({navURL:'test'});
  });

另请注意在清单中添加"permissions": ["contextMenus"...]

答案 1 :(得分:0)

我找到了解决方案

<强> background.js

chrome.contextMenus.onClicked.addListener(function(info, tab){
  chrome.tabs.executeScript(tab.id, {file: "getDOM.js"})
});


chrome.contextMenus.onClicked.addListener(function(info, tab){
  chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
            chrome.tabs.sendMessage(tabs[0].id, {greeting: "GetURL"}, function(response) {
            alert(response.navURL);
      });
      });
});

<强> getDOM.js

chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {

    if (request.greeting === "GetURL")
      sendResponse({navURL:'test'});
  });