I am injecting a script into a tab using chrome.tabs.executeScript. Its last parameters is a callback function that will be called with The result of the script in every injected frame.
. How do I make my script return a result? I tried adding a simple return statement at the end of my js, but nothing got returned.
Asked
Active
Viewed 3,861 times
4

ThePiachu
- 8,695
- 17
- 65
- 94
1 Answers
5
As RobW corrected me, a function is not necessary, just make sure that the desired data is the last expression and of course that is JSON-serializable.
chrome.tabs.executeScript(tabId,
{code:"document.charset"},
function(results){
// results[0] will now contain the charset for the page in question
});

BeardFist
- 8,031
- 3
- 35
- 40
-
Wrapping in a function is not needed. In order to see some meaningful value at the `executeScript` callback, three requirements need to be satisfied: 1. The correct permissions must be requested via the manifest file 2. The desired result must be the last expression at the end of the whole JavaScript block (function/variable declarations do not count - `1;var x=2;` results in `1`, for instance). 3. The value must be JSON-serializable. – Rob W May 24 '13 at 13:13
-
@RobW Thanks, I had figured a function wasn't needed, but I was too focused on thinking I had to *return* it and didn't try it this way. – BeardFist May 24 '13 at 19:11