Javascript 返回未定义字符串的Chrome扩展函数

Javascript 返回未定义字符串的Chrome扩展函数,javascript,string,function,google-chrome-extension,undefined,Javascript,String,Function,Google Chrome Extension,Undefined,我正在构建我的第一个chrome扩展,它通过从网页抓取URL来创建链接播放列表。然而,我的信息功能被挂断了。我在playlist对象中编写了一个名为“test”的测试函数,我只是尝试将内容脚本(通过getPageInfo函数)发回的消息分配给对象函数中的一个变量。然而,尽管我正在返回一条消息,但返回语句和测试函数之间的字符串丢失了。以下是我的背景脚本: //BACKGROUND.JS var Playlist = { index: 0, playlist: [], test: function

我正在构建我的第一个chrome扩展,它通过从网页抓取URL来创建链接播放列表。然而,我的信息功能被挂断了。我在playlist对象中编写了一个名为“test”的测试函数,我只是尝试将内容脚本(通过getPageInfo函数)发回的消息分配给对象函数中的一个变量。然而,尽管我正在返回一条消息,但返回语句和测试函数之间的字符串丢失了。以下是我的背景脚本:

//BACKGROUND.JS

var Playlist = {
index: 0,
playlist: [],
test: function(info) {
    var message = "Get Song";
    var song = getPageInfo(message);
    alert(song); //Shows undefined
    }
};

function getPageInfo(message) {
var s;
chrome.tabs.query({active:true, currentWindow:true}, function(tabs) {
        chrome.tabs.sendMessage(tabs[0].id, {greeting: message}, function(response){
            console.log(response.farewell);
            s = response.farewell;
            alert(s); //Shows "We did it" 
        });
    });
alert(s) //Shows "We did it"
return s;
}

chrome.contextMenus.create({
title: "Add to Playlist",
contexts: ["image", "link", "selection"],
onclick: Playlist.test
});
以下是我的内容脚本:

//CONTENT.JS

chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
    console.log(sender.tab ?
        "from a content script:" + sender.tab.url :
        "from the extension");
    if(request.greeting == "Get Song")
        sendResponse({farewell: "We did it"}); //My message
});
这是我的清单:

{
"manifest_version": 2,

"name": "RedditDJ",
"description": "This app hides jobs which the user has already clicked on.",
"version": "1.0",
"content_scripts": [
    {
        "matches": ["http://www.reddit.com/*","https://www.reddit.com/*"],
        "js": ["script/jquery-2.1.4.js", "script/content.js"]
    }
],
"permissions": [
    "http://www.reddit.com/",
    "https://www.reddit.com/",
    "http://www.youtube.com/",
    "https://www.youtube.com/",
    "http://www.soundcloud.com/",
    "https://www.soundcloud.com/",
    "http://*/", 
    "https://*/",
    "tabs",
    "contextMenus",
    "storage"
],
"browser_action": {
    "default_icon": {
        "19":"style/img/icon_19.png",
        "38":"style/img/icon_38.png",
        "128":"style/img/icon_128.png"
    },
    "default_title": "Reddit DJ",
    "default_popup": "popup.html"
},
"background": {
    "scripts": ["script/background.js"],
    "persistent": true
}
}

我已经翻了几天了,但我不知道我做错了什么。有什么想法吗?谢谢

您无法将异步函数转换为同步函数。您必须为
getPageInfo
添加
callback
参数,并使用它返回值

大概是这样的:

function getPageInfo(message, callback) {
    chrome.tabs.query({active:true, currentWindow:true}, function(tabs) {
        chrome.tabs.sendMessage(tabs[0].id, {greeting: message}, function(response) {
            console.log(response.farewell);
            s = response.farewell;
            callback(s);
        });
    });
}
或者更好的办法是使用承诺:

function getPageInfo(message) {
    return new Promise(function(resolve, reject) {
        chrome.tabs.query({active:true, currentWindow:true}, function(tabs) {
            chrome.tabs.sendMessage(tabs[0].id, {greeting: message}, function(response) {
                console.log(response.farewell);
                s = response.farewell;
                resolve(s);
            });
        });
    });
}


getPageInfo().then(function(value) {
    alert(value);
})
这不是“迷路”。所有chrome.*API都是异步的。您可能需要阅读一些关于js中异步代码的内容。可能重复: