0

I'm using openpgp.js in my chrome extension, and sendResponse that sends the response to the content script from decryptMessage is NOT working:

openpgp.decryptMessage(key, message).then(function(plaintext) {
    var res = sendResponse({result: "test"});
})

But it works inside the encryptMessage function:

openpgp.encryptMessage(publicKey.keys, base64Message).then(function(pgpMessage) {
    sendResponse({result: pgpMessage});
})

In the background console there are no errors, I have no clues why this is happening.

The entire onMessage handler:

function handleMessageEvent(request, sender, sendResponse) {
    console.log('just-encrypt');
    if(request.data.type == 'decrypt')
    {
        var key = openpgp.key.readArmored(request.data.privateKey);
        console.log(key);
        if(key.keys.length > 0)
        {
            key = key.keys[0];
            key.decrypt(request.data.password);
            var message = openpgp.message.readArmored(request.data.message);
                console.log('decrypt message');
                openpgp.decryptMessage(key, message).then(function(plaintext) {
                    console.log('decrypted');
                    sendResponse({result: 'test'});
                }).catch(function(error) {
                    console.log(error);
                });
        }
        else
        {
            chrome.extension.getBackgroundPage().console.log("no accessible keys found");
        }
      return true;
}
wOxxOm
  • 65,848
  • 11
  • 132
  • 136
user3112115
  • 695
  • 3
  • 12
  • 23
  • Hopefully you didn't redefine the *built-in* `sendResponse` function :-) See also [Chrome Extension Message passing: response not sent](http://stackoverflow.com/a/20077854) – wOxxOm Nov 06 '15 at 09:15
  • @wOxxOm heey man, now I've added return true;and yes it gave some clue(error message) from your link, now it tells me that port object is disconnected, why that is happening? – user3112115 Nov 06 '15 at 10:24
  • @wOxxOm I've updated the code, it is simple, I'am returning true now it is the function handler for onMessage, the decrypt function works asynchrously, and the port is getting null, I can't figure out how to solve this, maybe you have some ideas, I've noticed that outside the decryptMessage function the port object is available, just inside decryptMessage is null – user3112115 Nov 06 '15 at 10:34
  • @wOxxOm I'am in the options tab, from there I send a request from the content script to the extension – user3112115 Nov 06 '15 at 10:39
  • @wOxxOm hey maaan it worked, thank you very much!!!!!! it just had to add the callback function, not just sending the request :DDDD you saved my day – user3112115 Nov 06 '15 at 10:46
  • Not sure I did help :-) Can you post the solution as an answer? – wOxxOm Nov 06 '15 at 11:55

0 Answers0