2016-05-06 33 views
0

您好,我正在嘗試展開事件頁面如何適用於Chrome擴展。這個想法是要求事件頁面獲取書籤樹並將其作爲響應發送給彈出窗口。Chrome擴展從事件頁面到異常響應的異步響應

問題:響應是undefined。 getTree()函數的外部響應起作用,另一個則不起作用。我這樣做是錯誤的嗎?

background.js

chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) { 
    if (request.action == "INIT"){ 
    chrome.bookmarks.getTree(function(treeStructure){ 
     sendResponse(treeStructure); 
    }); 
    } 
    //sendResponse(true); 
}); 

popup.js

chrome.runtime.sendMessage({action: "INIT"}, function(response) { 
    console.log(response); 
}); 

的manifest.xml

{ 
    "manifest_version": 2, 

    "name": "Test", 
    "description": "Test", 
    "version": "1.0", 

    "browser_action": { 
     "default_icon": "/img/icon.png", 
     "default_popup": "front.html" 
    }, 

    "icons": { "16": "/img/icon.png", 
      "48": "/img/icon.png", 
      "128": "/img/icon.png" }, 

    "permissions": [ 
     "bookmarks", 
     "storage", 
     "https://*/" 
    ], 

    "background": { 
     "scripts": ["js/back.js"], 
     "persistent": false 
    } 
} 
+0

後一些谷歌上搜索周圍,我發現類似的東西[鏈接] https://stackoverflow.com/questions/20077487/chrome-extension-message-passing-response-not-sent。通過在IF內部或外部添加返回true來解決問題。 – enr00ted

回答

0

你需要到m請確保您在清單文件中擁有以下權限。我不知道你是否已經擁有這些東西,所以我將它放在那裏以防萬一。

"permissions": [ 
    "bookmarks" 
] 
+0

我更新了我的問題,因爲我已經擁有該權限,因爲我能夠在後臺獲取書籤,事情是消息不會傳遞給前臺/彈出窗口。 – enr00ted