Reputation: 9305
I have a chrome-extension with lots lines of code. More and more people demand that I offer that extension on other browsers (like firefox), too.
Because it is a chrome extension, a lot of chrome-specific functions are included. As I start I want to put all chrome-specific methods in a javascript-file "chrome.js" and encapsulate the chrome-functions with my own so I could easily create other browser-specific methods.
This is quite easy for simple methods:
function geti18nMessage(messageId) {
return chrome.i18n.getMessage(messageId)
}
But how can I encapsulate (asynchronous) methods that return a function?
Example:
chrome.runtime.sendMessage(
{
Action: "Load"
}, function (response)
{
console.log("response is "+response);
});
This isn't really chrome-specific, but the chrome-issue is a real-world-example for my problem.
Upvotes: 0
Views: 40
Reputation: 707826
You can just pass the function through like any other argument:
function sendMessage(options, fn) {
return chrome.runtime.sendMessage(options, fn);
}
This assumes that you are committed to the same Chrome callback scenario on all platforms. If you want to customize the callback to be something of your own design, then you can replace it like this:
function sendMessage(options, fn) {
return chrome.runtime.sendMessage(options, function() {
// do any processing of the chrome-specific arguments here
// then call the standard callback with the standard arguments you want to
// support on all platforms
fn(...);
});
}
Upvotes: 2