![](/img/trans.png)
[英]Chrome-extension: Programmatic way to open popup.html from background.html?
[英]Returning pass data from Background.html to popup.html
我已經將數據從后台傳遞到彈出窗口了。 但是,如果我返回response.reply
,則會得到未定義的信息,但是如果我將其打印出來,則它的狀態將是不確定的。 就像我期望的那樣。 我該如何退貨?
popup.html
function getURL(action){
chrome.extension.sendRequest(
{
req: "geturl",
act: action
},
function(response)
{
return response.reply;
});
}
background.html
function getURL(action)
{
var url = cmshttp+cmshooksurl+"?action="+action;
return url;
}
您不能從異步函數返回值。 您需要將值傳遞給下一個函數(回調)。
您的代碼應如下所示:
function getURL(action, callback){
chrome.extension.sendRequest(
{
req: "geturl",
act: action
},
function(response)
{
callback(response.reply);
}
);
}
用法:
getURL("some_action", function(reply) {
console.log("reply is:", reply);
});
background.html:
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
if(request.req == "geturl") {
sendResponse({reply:"reply from background"});
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.