0

I was just trying some hands on with Chrome Extension development.

The code i'm writing is simple, It accesses Fixer.io API from Background Event Page to get Forex rates and then returns those rates to Content Script.

I tried following the Question: [How to return the response from an asynchronous call? ][1]. But now I am confused and ended up with this code.

Can anybody help me understand how to achieve this ?

contentScript.js

document.addEventListener('mouseup',function(event)
                          {
    
                            //console.log("Event triggered");
                            
                            var selectedText=document.getSelection().toString(); //variable to store selected text
                            
                            if(selectedText.length)
                            {
                                console.log("Selected Text is "+selectedText);
                                
                                                         
                                var amount=1;
                                var base="USD";
                                
                                //Get the Exchange Rate from eventPage.js
                                chrome.runtime.sendMessage({amt: amount, bs: base},function(response){
                                                    console.log("Converted Amount: "+response.convert);});
                            }
});

Background Event.js

function convertAmount(callback){
        
            var xhr=new XMLHttpRequest(); //creating a XMLHttpRequest Object

            xhr.open("GET","http://api.fixer.io/latest?base=INR",true); //Open async connection to the API
            
            xhr.onload=function returnResponse(){
                callback(xhr.responseText);
            }

            xhr.send(null);
    
}

function getRate(rate){
                   
            var json=JSON.parse(rate);

            convertedAmount=json.rates.USD;

            console.log("In Callback: "+convertedAmount);
            
            chrome.runtime.onMessage.addListener( function(request,sender,sendResponse){
    
            console.log("Converted Amount: "+convertAmount(getRate));
            
            sendResponse({convert: 60 });
            
            });
        }
Lokesh Sah
  • 2,283
  • 5
  • 23
  • 33
  • 1
    I've closed your question as a duplicate since `convertFunction` does not currently have a return value (and the inner `onreadystatechange` callback does, though the value is never used) so it seems to be a perfect fit for that duplicate. If you have problems not addressed by that duplicate, either open a new question, or edit this question to clarify why the duplicate doesn't solve your problem. – apsillers Feb 20 '15 at 16:07
  • @apsillers Hi, Can you please help. – Lokesh Sah Mar 06 '15 at 04:40
  • You should take a look at http://stackoverflow.com/questions/20077487/chrome-extension-message-passing-response-not-sent – Xan Mar 06 '15 at 16:49

0 Answers0