What is callback parameter function?

1,922 views
Skip to first unread message

Evgheny

unread,
Jul 10, 2010, 7:58:12 PM7/10/10
to Chromium-extensions
Hello everyone. I think it is easy question for most of people in this
group, but I need it's answer.

Almost every method from APIs has the last parameter - callback
function.
I can't understand, what does it do? Can you show me example with
callback? for example with method " chrome.tabs.executeScript()"

For example I need to execute some little JS code on the content page,
and I need to get return value. Can I do it with callback parameter,
or callback is for other purposes.

Thank you, Evgheny.

Ben

unread,
Jul 12, 2010, 2:49:51 PM7/12/10
to Evgheny, Chromium-extensions

> callback ( optional function )
> Called after all the JavaScript has been executed.

Basically means what it says. I believe it's called in the same context
as chrome.tabs.executeScript, but I'm not sure if that's always the
case. you can create an anonymous function (e.g.: "function() {}"), or
create one before-hand and include it in the executeScript call:

details = {code: "alert('This alert appears on the page.');
chrome.extension.sendRequest() /* port = chrome.extension.connect();
*/"}

function doSomething() {
//code to execute after alert, port creation, etc..
}

chrome.tabs.executeScript(tabId, details, doSomething);
// You should have gotten tabId previously
// I didn't show that simply for brevity.

You might be able to include a chrome.extension.connect after the code
you execute on the page (I'm not sure if pages are allowed to do that,
though) with the return value. Otherwise, if you know the tabId, you can
use chrome.tabs.connect (from the extension) or chrome.extension.connect
(from the page; you need only your extension id for this, so it may be
ideal) to communicate between the page and your extension.

Pauan

unread,
Jul 12, 2010, 11:39:46 PM7/12/10
to Chromium-extensions
The callback parameter is a function that is run when the code is
finished. As an example:

chrome.tabs.create({}, function () {
alert("Done!");
});

The above code will create a new tab, and then run the function after
the tab has been created.

If you want to communicate between the background page and a content
script, you will need to use the messaging APIs:
http://code.google.com/chrome/extensions/dev/messaging.html

I prefer to use chrome.extension.sendRequest, but
chrome.extension.connect will work too. If you want to just send a
single message and a reply, here's an example:


// content script
chrome.extension.sendRequest({ message: "Hello!" }, function (json) {
alert(json.message);
});

// background page
chrome.extension.onRequest.addListener(function (json, port, reply) {
if (json.message === "Hello!") {
reply({ message: "Goodbye" });
} else {
reply({});
}
});


What the above code does is the content script sends the message
"Hello!" to the background page. When the background page receives the
message, it will reply with the message "Goodbye", which the content
script will then alert. If you want to send multiple messages back and
forth, it's probably better to use chrome.extension.connect.
Reply all
Reply to author
Forward
0 new messages