I am trying to communicate with a google chrome extension using JavaScript. I have succeeded in calling the extension from my code. But can't read the response back to my application.
I have written the calling function like this.
function extensionCall(){
var event = document.createEvent('Event');
event.initEvent('EXTENSION_READ_EVENT');
document.dispatchEvent(event);
}
And the code inside the extension is
document.addEventListener("EXTENSION_READ_EVENT", function (data) {
chrome.runtime.sendMessage("test", function (response) {
});
});
chrome.extension.onMessage.addListener(function (msg, sender, sendResponse) {
if (msg.action == 'EXTENSION_DATA') {
try {
readExtension($.parseJSON(msg.response));
}
catch (e) {
var error = "error" + e;
}
}
});
And I am expecting the response here..
function readExtension(val){
console.log(val);
}
But unfortunately, I am not getting any response from the extension.
How can I access the data to my application?