Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript chrome扩展,将数据从内容传递到后台_Javascript_Json_Google Chrome Extension - Fatal编程技术网

Javascript chrome扩展,将数据从内容传递到后台

Javascript chrome扩展,将数据从内容传递到后台,javascript,json,google-chrome-extension,Javascript,Json,Google Chrome Extension,我创建了一个chrome扩展,它由manifest.json、content.js和background.js组成。在content.js中,我提取当前选项卡的URL,在background.js中,我打开一个新选项卡。我想做的是,从内容中传递URL并将其附加到我在后台调用的URL,但这不起作用 content.js: chrome.extension.onMessage.addListener(function(request, sender, sendResponse) { if(r

我创建了一个chrome扩展,它由manifest.json、content.js和background.js组成。在content.js中,我提取当前选项卡的URL,在background.js中,我打开一个新选项卡。我想做的是,从内容中传递URL并将其附加到我在后台调用的URL,但这不起作用

content.js:

chrome.extension.onMessage.addListener(function(request, sender, sendResponse)
{
    if(request.greeting=="gimmieyodatas")
    {
    var output ="URL=";
    //check for the character '?' for any parameters in the URL
    var paramIndex = document.URL.indexOf("?");
    //if found, eliminate the parameters in the URL
    if (paramIndex > -1)
    {
        output += document.URL.substring(0, paramIndex);
    };
        sendResponse({data: output});
    }
    else{
        sendResponse({}); 
    }
});
background.js:

var output2;
chrome.tabs.getSelected(null, function(tab) {
  chrome.tabs.sendMessage(tab.id, {greeting:"gimmieyodatas"}, function(response) {
    output2 = response.data;
  });
});
chrome.browserAction.onClicked.addListener(function() {
    chrome.tabs.create({url: "http://www.google.com?" + output2}, function(tab) {
        chrome.tabs.executeScript(tab.id, {file: "content.js"}, function() {
            sendMessage();
        });
    });
});

当我从一个打开的选项卡运行扩展时,它会在一个新选项卡上打开google,但它不会在google URL中附加当前选项卡的URL,这意味着“输出”数据不会传递到background.js。我做错了什么?

问题是在打开新选项卡时,您没有告诉后台页面发送消息。对chrome.tabs.getSelected的调用仅在第一次运行扩展时发生一次,而不是每次打开新选项卡时都发生

通过将背景页用作两个内容页之间的中介,您的做法是正确的,但我建议采用不同的方法:

每次打开新选项卡时,通过清单文件加载内容脚本:

"content_scripts": [
    {
        "matches" : [
            "<all_urls>"
        ],
        "js" : [
            "content.js"
        ]
    }
],
当后台页面收到消息时,它会将URL保存到全局变量:

(background.js)

单击“操作”按钮后,可以加载该URL:

(background.js)


问题在于,当打开新选项卡时,您没有告诉后台页面发送消息。对chrome.tabs.getSelected的调用仅在第一次运行扩展时发生一次,而不是每次打开新选项卡时都发生

通过将背景页用作两个内容页之间的中介,您的做法是正确的,但我建议采用不同的方法:

每次打开新选项卡时,通过清单文件加载内容脚本:

"content_scripts": [
    {
        "matches" : [
            "<all_urls>"
        ],
        "js" : [
            "content.js"
        ]
    }
],
当后台页面收到消息时,它会将URL保存到全局变量:

(background.js)

单击“操作”按钮后,可以加载该URL:

(background.js)


谢谢你,裘德。我做了你建议的事,但有一点不同。为了确保“chrome.extension.onMessage.addListener”正在后台处理,我添加了“output2+=”this=test“,这样它至少会返回“”。但我得到了“”,这意味着onMessage.addListener无法通过。为什么?好吧,我的错!我刚刚意识到我忘记从content.js中删除“chrome.extension.onMessage.addListener”,这就是问题所在。非常感谢裘德解决了我的问题…没问题。如果我的答案解决了你的问题,你介意接受/同意吗?干杯,谢谢你,裘德。我做了你建议的事,但有一点不同。为了确保“chrome.extension.onMessage.addListener”正在后台处理,我添加了“output2+=”this=test“,这样它至少会返回“”。但我得到了“”,这意味着onMessage.addListener无法通过。为什么?好吧,我的错!我刚刚意识到我忘记从content.js中删除“chrome.extension.onMessage.addListener”,这就是问题所在。非常感谢裘德解决了我的问题…没问题。如果我的答案解决了你的问题,你介意接受/同意吗?干杯
var output2;
chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
    output2 = request.output2;
});
chrome.browserAction.onClicked.addListener(function() {
    chrome.tabs.create({url: "http://www.google.com?" + output2});
});