0

How do I concatenate a call from my popup and my background script? My code is the following... I'm sending a message from the popup to verify whether I'm logged in or not and according to that I'm going to have a different popup appearance. After the message has been sent, I call a get on the chrome storage to check if the user is logged and its token is still valid.

If I send the message and call the callback function right away I'm fine. If I add a call to the storage and call the callback function within the body of this latter function, the callback function I defined for the message listener, does not happen to be called (I display "window" no matter what... but the message is sent and received by the background script).

Can anybody explain me why and perhaps how can I deal with my situation? Script from popup -> verify if auth token is stored -> callback function to generate HTML code to be displayed in popup.

What am I missing?

check.js:

var html_1 = '<div><input type="button" value="signup"></input><input type="button" value="login"></input></div>';
var html_2 = '<div>Username: <input type="text" name="username"></input>Password: <input type="text" name="password"></input></div>';
chrome.runtime.onMessage.addListener( function( request, sender, sendResponse) {
    if (request.type === 'authenticate') {        
        chrome.storage.sync.get(null, function(o) {
            var v = 0;
            if ( !isEmpty(o) ) {
                v = o.pf_logged;
                v++;
                chrome.storage.sync.set( {"pf_logged": v } );
            } else {
                chrome.storage.sync.set( {"pf_logged": 10} );
            }

            if (v % 2 == 0) {
                console.log('asda')
                sendResponse(html_1);
            } else
            ;
                sendResponse(html_2);
        });
    }    
});

open_dialog.js:

$(document).ready(function() {

    function setDOM(info) {

    }

    chrome.runtime.sendMessage({type:'authenticate'}, function(info) {

        $('#dialog').html(info);
        console.log('ad')
        console.log(info)
    });
});

manifest.json:

{
    "manifest_version": 2,
    "name": "WARC creator",
    "description": "This is the description",
    "version": "0.1",
    "icons": {
        "64" : "icons/icon64.png"

    },
    "browser_action": {
        "default_icon": "icons/icon64.png",
        "default_title": "Title",
        "default_popup": "html/a.html"
    },
    "background":{
        "scripts":["js/check.js"]
    },
    "permissions":["storage"],
    "homepage_url": "http://test.com"
}

a.html:

<!DOCTYPE html>
<html>
    <head>
        <title>Dialog test</title>
    </head>
    <body>
        <div id="dialog">window</div>
    </body>
    <script src="/js/jquery.min.js"></script>
    <script src="/js/open_dialog.js"></script>
</html>
Dieghito
  • 665
  • 1
  • 12
  • 24
  • Dup. But the other question has a very generic title so its hard to spot it for this case. – Zig Mandel Jun 19 '14 at 12:43
  • Thank you guys! Yeah that effectively answered my question but I didn't find it in my research, sorry for the duplicate. – Dieghito Jun 19 '14 at 16:24

0 Answers0