I’m trying to pass data that is saved in sessionStorage from background.html to popup.html
background.html:
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
data = sessionStorage.getItem(request.tabId);
alert(data);
sendResponse({ data: data });
});
and in popup.html:
chrome.tabs.getSelected(null, function(tab) {
chrome.extension.sendRequest({ tabId: tab.id }, function(response) {
alert(response.data);
});
});
The popup is opened by a pageAction button, when I click the button I get an alert box with “null” on the popup and then an alert box with the data that I stored in sessionStorage on the background!
Any ideas how to fix this?
Well, I’ve done something dumb.
I inspected the background page by opening
chrome-extension://[extension-id]/background.htmlin a tab instead of clicking on “inspect active views:background.html” in the extensions management page. This caused the tab to catch the request and callsendResponse, but the popup expected the REAL background page to callsendResponse(and if I understand Google’s documentation regarding message passing, the fact that sendResponse was called twice is root of the problem, because the first call clears therequestobject)