Results 1 to 4 of 4

This is a discussion on Console.log returns value, alert of same response is blank??? within the Plugins Development section, part of the Chrome Plugins category: I'm trying to reference information from my background.html page (that is accessing localStorage) by sending a request from my content ...


  1. #1
    bdubwendt is offline Junior Member
    Join Date
    Mar 2010
    Location
    Bonner Springs, KS
    Posts
    2

    Question Console.log returns value, alert of same response is blank???

    I'm trying to reference information from my background.html page (that is accessing localStorage) by sending a request from my content script using the messaging API. I am able to successfully send and receive a response, but what I am able to do with the response is throwing me for a loop.

    This works:
    Code:
    function getStoredPrefs()
    {
    	chrome.extension.sendRequest({name: "getPreferences"}, function(response) { console.log(response.prefEnableKey) ;} );
    }
    This doesn't work:
    Code:
    function getStoredPrefs()
    {
    	var responseVal = "";
    	chrome.extension.sendRequest({name: "getPreferences"}, function(response) { responseVal = response.prefEnableKey ;} );
    	alert(responseVal);
    }

    Side note: When I call the alert in the portion that is not working, it seems to fire twice (two separate alert boxes received) even though I am only calling it once. Any ideas?

    Thanks in advance for any assistance.

  2. #2
    PAEz's Avatar
    PAEz is offline Moderator
    Join Date
    Aug 2009
    Location
    Australia
    Posts
    655

    Default

    First up, why did you feel the need to make a new account to post the same question?....
    Console.log returns value, alert of same response is blank???
    ...if its because no one answered your question then Im sorry coz Ive been busy the last few days and havent touched the computer other than to answer emails and play Torchlight.

    The reason your alert is not getting the variable is because its being exectuted before the variable arrives.....
    Code:
    chrome.extension.sendRequest({name: "getPreferences"}, function(response) { responseVal = response.prefEnableKey ;} );
    ...in your sendRequest your stating that your "callback" is the anonymous function....
    Code:
    function(response) { responseVal = response.prefEnableKey ;}
    this gets called after the request has been got, when this is can be anytime so the alert will prolly happen before the response is got.

    Another way to write your code that may help you see it is....
    Code:
    function prefsResponse(response)
    {
        responseVal = response.prefEnableKey ;
    }
    
    function getStoredPrefs()
    {
    	var responseVal = "";
    	chrome.extension.sendRequest({name: "getPreferences"}, prefsResponse()  );
    	alert(responseVal);
    }
    (hope i got that right, i didnt test it and Im prone to silly mistakes...can never remember details)
    If you where to put your alert in prefsResponse you would see that it gets the value.
    Callbacks are annoying but something you've got to work with alot in chrome extensions.

    Also, you dont need to message to get stuff from localstorage (I guess thats what your doing), Im not sure whats its scope is exactly but anyways you can get it from the background page doing something like this....
    Code:
    var backgroundPage = chrome.extension.getBackgroundPage();
    value=backgoundPage.localstorage.prefEnableKey;
    Asfar as the double alert I saw something on google groups about this recently and apparently it was a bug that got fixed in one of the latest dev updates (cant remember the thread).

    Hope this helps, Im not allways the best at explaining stuff.
    Last edited by PAEz; 03-18-2010 at 07:10 AM.

  3. #3
    Waha's Avatar
    Waha is offline Senior Member
    Join Date
    Apr 2009
    Location
    Oregon
    Posts
    788

    Default

    PAEz, it would be:
    Code:
    chrome.extension.sendRequest({name: "getPreferences"}, prefsResponse );
    Because the intention is to pass the function by reference, and not to execute the function and pass the return value.
    Also it's localStorage, with a capital S.

    Also callbacks aren't that tough, you just have to work with them. Really, JavaScript makes them a lot easier than other languages:
    Code:
    function getStoredPrefs(){
    	var responseVal = "";
    	chrome.extension.sendRequest({name: "getPreferences"}, function(response) {
    		responseVal = response.prefEnableKey ;
    		alert(responseVal);
    	});
    }
    This (putting the alert in the callback) is what you should be doing, in my personal formatting style.

    Remember, JavaScript isn't going to hang to wait for the callback to be run, it doesn't even know you passed a callback. So sequentially all you're doing is chrome.extension.sendRequest() then alert() - no anonymous function till the Background Page gets around to it.
    ~ Projects ~
    Specialized: Carapass Auction Watcher, Kongregate Chat
    Libraries: bliplib
    Tools: manifest syntax highlighting & snippits
    ~ Happy to make extensions for pay too ;D ~
    Portfolio: Search and Share

  4. #4
    bdubwendt is offline Junior Member
    Join Date
    Mar 2010
    Location
    Bonner Springs, KS
    Posts
    2

    Question

    All, thanks for your assistance!

    So I updated to the following and the alert is producing the correct results:

    Code:
    function getStoredPrefs(){
    	var responseVal = "";
    	chrome.extension.sendRequest({name: "getPreferences"}, function(response) {
    		responseVal = response.prefEnableKey ;
    		alert(responseVal);
    	});
    }
    My next question: How do I reference the value of responseVal outside of this function within the rest of my code?

    Code:
    getStoredPrefs();
    if(responseVal == "no")
    {
    	alert("Not toggling");
    }
    else
    {
    	alert("Toggling");
    }
    Console log shows that responseVal is undefined. I know I'm running into issues with the private declaration of responseVal inside my getStoredPrefs function.

    Thanks again for the assistance (and sorry about the double post of the same question, I have originally used a bugmenot account but wasn't sure I'd get a response on that account, so I created my own since I'll be frequenting this forum).

Similar Threads

  1. Superman Returns [CRX]
    By Jedi Knight in forum Chrome Themes
    Replies: 10
    Last Post: 02-07-2012, 05:13 PM
  2. Blank page. Not in firefox or ie
    By Twins.Seven in forum Bugs and Vulnerabilities
    Replies: 5
    Last Post: 08-02-2011, 06:48 AM
  3. Console.log returns value, alert of same response is blank???
    By tubgirl in forum Plugins Development
    Replies: 0
    Last Post: 03-16-2010, 11:48 PM
  4. Search From Address Bar Returns I-Google
    By LindyGirl in forum Bugs and Vulnerabilities
    Replies: 3
    Last Post: 03-14-2009, 04:17 AM
  5. Replies: 0
    Last Post: 03-09-2009, 07:54 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •