Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
107
rated 0 times [  111] [ 4]  / answers: 1 / hits: 27909  / 9 Years ago, thu, december 31, 2015, 12:00:00

I am trying to create a new extension. I was able to use the chrome.runtime.sendMessage function a while back but right now, I have tried everything and it still is not able to send the message to the background script. The console is getting populated with the log messages from the content-script.js but not from the background.js



content-script.js



console.log(Hello World!s);
$(document).ready(function() {
console.log(DOM READY!);
$(document.documentElement).keydown(function (e) {
console.log(Key Has Been Pressed!);
chrome.runtime.sendMessage({Message: getTextFile}, function (response) {
if (response.fileData) {
alert(Contents Of Text File = );
}
else {
console.log(No Response Received);
}
})

})
});


background.js



console.log(Atleast reached background.js)
chrome.runtime.onMessage.addListener (
function (request, sender, sendResponse) {
console.log(Reached Background.js);
if (request.Message == getTextFile) {
console.log(Entered IF Block);
$.get(http://localhost:8000/quicklyusercannedspeechbucket/helloWorld1, function(response) {
console.log(response);
sendResponse({fileData: response})
})
}
else {
console.log(Did not receive the response!!!)
}
}
);


manifest.json



{
manifest_version: 2,
name: My Cool Extension,
version: 0.1,
content_scripts: [ {
all_frames: true,
js: [ jquery-2.1.4.min.js, content-script.js ],
matches: [ http://*/*, https://*/*, file://*/* ]
} ],
permissions: [ http://*/*, https://*/*, storage ],
background: {
scripts: [
jquery-2.1.4.min.js,
background.js
]
}
}


Any help is appreciated :)



Thanks!


More From » jquery

 Answers
39

You need to change your code so that in the background.js you must change the behaviour:



console.log(Atleast reached background.js)
chrome.runtime.onMessage.addListener (
function (request, sender, sendResponse) {
console.log(Reached Background.js);
if (request.Message == getTextFile) {
console.log(Entered IF Block);
$.get(http://localhost:63342/Projects/StackOverflow/ChromeEXT/helloWorld1, function(response) {
console.log(response);

// to send back your response to the current tab
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {fileData: response}, function(response) {
;
});
});


})
}
else {
console.log(Did not receive the response!!!)
}
}
);




While for the contentscript you need to do:



console.log(Hello World!s);
$(document).ready(function() {
console.log(DOM READY!);
$(document.documentElement).keydown(function (e) {
console.log(Key Has Been Pressed!);
chrome.runtime.sendMessage({Message: getTextFile}, function (response) {
;
})

})
});


// accept messages from background
chrome.runtime.onMessage.addListener (function (request, sender, sendResponse) {
alert(Contents Of Text File = + request.fileData);
});




The sendResponse can be used as an immediate feedback not as a result of computation.


[#63879] Tuesday, December 29, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
susanajamiep

Total Points: 466
Total Questions: 113
Total Answers: 108

Location: Liberia
Member since Fri, Oct 22, 2021
3 Years ago
susanajamiep questions
Sun, Jun 12, 22, 00:00, 2 Years ago
Mon, Mar 7, 22, 00:00, 2 Years ago
Wed, Jun 10, 20, 00:00, 4 Years ago
Fri, Jan 24, 20, 00:00, 4 Years ago
;