0

I am writing a chrome extension using indexedDB and want to use sendMessage to pass data from the background page to the popup.
If the button is pressed, a message is sent from popup to background, and a response should be written to the 'results' div. the logging messages are all ok, but no response is sent from within the 'openRequest.onsuccess' block.

I am not sure why the response is not received from openRequest.onsuccess yet the console message just after it is written.

output from console.log;
popup;
processMessage() - 2

background;
processMessage() - 2
processMessage() - 1

content of 'results';
processMessage() - 2

popup.js

var popupInst;
var popupPage = function() {
    var mThis = this;
    this.msg;
    this.init = function() {
        document.querySelector('#btnAdd').addEventListener('click', this.add);
    };
    this.add = function() {
        chrome.extension.sendMessage({ add: { keyName: 'key', valueName: 'value' }}, function(response) {
            mThis.msg = (response.msg)
            console.log(mThis.msg);
            $('#results').empty().append(mThis.msg);
        });
    };
}
document.addEventListener('DOMContentLoaded', function(){popupInst = new popupPage(); popupInst.init();});

background.js

var bgInst;
var backgroundPage = function()
{
    var mThis = this;
    this.dbName = 'test-db';
    this.db;
    this.init = function() {
        var openRequest = window.indexedDB.open(mThis.dbName,1);
        openRequest.onsuccess = function(evt) {
            this.db = openRequest.result;
        };
        openRequest.onupgradeneeded = function(evt) {
            var objStore = evt.target.result.createObjectStore(this.dbName,{keyPath:"keyName"});
            objStore.createIndex("keyName","keyName",{unique:true});
            objStore.createIndex("valueName","valueName",{unique:false});
        };
        chrome.extension.onMessage.addListener(this.processMessage);
    };
    this.processMessage = function(msgRequest,msgSender,msgCallback) {
        if(msgRequest.add) {
            var openRequest = window.indexedDB.open(mThis.dbName);
            openRequest.onsuccess = function(evt) {
                var str1 = 'processMessage() - 1';
                msgCallback({ msg: str1 }); -> this message is never received in popup.js
                console.log(str1);          -> but this message is written to the log
            };
            var str2 = 'processMessage() - 2';
            msgCallback({ msg: str2 });
            console.log(str2);
        }
    };
}
document.addEventListener('DOMContentLoaded', function(){bgInst = new backgroundPage(); bgInst.init();});
cem82
  • 1
  • See this answer: http://stackoverflow.com/a/20077854/1507998 – rsanchez Nov 21 '13 at 15:53
  • possible duplicate of [Chrome Extension Message passing between extension(background) and content script](http://stackoverflow.com/questions/20077487/chrome-extension-message-passing-between-extensionbackground-and-content-scrip) – Deni Spasovski Dec 12 '13 at 14:36

0 Answers0