Unable to send Chrome Extension sender callback value

I am encoding a chrome extension that interacts with the gmail api (chrome 45 is my version) and I'm having problems sending a message from background.js to my content script. Asynchronous aspect is a problem. How can I send a message after a callback?

//---------in content script---------
chrome.runtime.sendMessage({ messageId: _id }, function (response) {    
    console.log('the respose.messagePayload is: ' + response.messagePayload); 
});

//---------in background script---------
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
    getMessage('me', request.messageId, function (payload) {
        //i want to send my response here
        //this executes and grabs the payload of data from the api, but isn't sending back the message to the content-script
        sendResponse({ messagePayload: payload }); 
    });
    //this synchronous call sends a message to the content-script
    //sendResponse({ messagePayload: "payload" });
    return true;
});

function getMessage(userId, messageId,callback) {
    var request = gapi.client.gmail.users.messages.get({
        'userId': userId,
        'id': messageId
    });
    request.execute(callback);
}

Chrome extension: messaging: no response sent

+4
source share
1 answer

You must send the function sendResponseto your callback.

getMessage('me', request.messageId, sendResponse);

Then do this when your call is getMessagecompleted.

function getMessage(userId, messageId, sendResponse) {
    var request = gapi.client.gmail.users.messages.get({
        'userId': userId,
        'id': messageId
    });

    request.execute(function(response) {
        sendResponse({messagePayload: response.payload});
    });
}

Another possible solution:

  • tab id sender.
  • getMessage.
  • getMessage script.
  • script onMessage, , , .

:

//---------in background script---------
chrome.runtime.onMessage.addListener(function (request, sender,sendResponse) {
    getMessage('me', request.messageId, sender.tab.id);
});

function getMessage(userId, messageId, tabId) {
    var request = gapi.client.gmail.users.messages.get({
        'userId': userId,
        'id': messageId
    });
    request.execute(function(response) {
        // Whatever response you want to sent
        // Below URL explains your response object
        // https://developers.google.com/gmail/api/v1/reference/users/messages#resource
        // Assuming you want to send payload
        chrome.tabs.sendMessage(tab.id, {messagePayload: response.payload});
    });
}

//---------in content script---------
chrome.runtime.sendMessage({ messageId: _id });

chrome.runtime.onMessage.addListener(function (request, sender,sendResponse) {
    // Just to verify that the request is from the getMessage callback
    // Because this will listen to all request from your extension
    if (request.messagePayload) {
        console.log('the respose.messagePayload is: ' + request.messagePayload);
    }
});

, .

-1

Source: https://habr.com/ru/post/1627541/


All Articles