"chrome extensions: which will be better, ajax or chrome.extension.sendrequest?" Code Answer

4

you should use the message passing api whenever you want to communicate between content scripts and your other pages. i don't see how you want to use ajax here.

if you're communicating between let's say your background page and the browseraction popup you could also use chrome.extension.getbackgroundpage which simply returns you the window object from the backgroundpage. due to the restrictions imposed on content scripts you can't use this function in content scripts. this means you will always have to use message passing api for content scripts.

don't be fooled by the asynchronous nature of these message passing functions. it's just a design consideration from the chrome team to use asynchronous functions (with a callback) everywhere. it doesn't mean they take a lot of time to execute. although i haven't bench-marked them, they seem to execute almost instantaneously.

edit

i misinterpreted your question. i thought you were asking about how to communicate between the different pages in your extension/app. what you are really asking is how to communicate with a web server.

the message passing api (sendrequest) only works for communication between different parts of your app. the reason why this api is in place is because different parts of your app run in different environments, these are called isolated worlds. these environments are completely separated from each other to protect you from malicious code. the only pinhole through these different environments is this message passing api.

if you want to communicate with a web server ('pages' as you call them) you will need to use ajax. with ajax you will also notice the tight security model of chrome. under normal conditions a webpage is only permitted to make requests to the same site it originates from. this is called 'same origin policy'. because your extension/app is not hosted (it is packaged) it has no rights to access a web server by default. you, as a developer, will have to request this right to the user. you can do this by filling in the permissions property in the extension manifest. whenever the user installs your app he has to accept that you have the right to access a certain web server.

as i read your question i am assuming you are still not very familiar with chrome extensions, apps, ajax and policy in a browser. i encourage you to read further on these topics while you are developing your app so you can ensure the security of your users.

edit 2

ok, so you're communicating between two different parts of your app, the index.html and the background.html. for this you can use getbackgroundpage and directly call functions defined in your background page. what you do is the following: in your backgroundpage:

window.getsuggestions = function(query) {
  // search database for query

  return ['suggestion1', 'suggestion2'];
};

then you can call the following function on your main page (index.html):

var backgroundpage = chrome.extension.getbackgroundpage();
var mysuggestions = backgroundpage.getsuggestions('suggestion');
console.log(mysuggestions);

it's that simple. you don't need any ajax or sendrequest for this. no asynchronous code at all. you can just call a function in a synchronous fashion and this will be returned:

['suggestion1', 'suggestion2']

i didn't benchmark this and i haven't seen any data about it yet but i'm pretty sure this code is a lot faster than the message passing api or ajax. i didn't know you could use xmlhttprequest within your extension but if it's possible i expect it to be the slowest way since you're actually making a network call in stead of calling to in-memory objects. if you want to be sure about the timings i suggest you to benchmark them yourself. you can easily do this with chrome by using the following code:

console.time('someid');
// perform heavy operation(s) here.
console.timeend('someid');

the time to perform this operation will be printed to the console.

edit 3

now that i think about it, i'm not really sure how you would do ajax between two pages inside an extension. don't you need a web server for this task? can you give an example in your question of how you achieved this in your app?

By King J on March 5 2022

Answers related to “chrome extensions: which will be better, ajax or chrome.extension.sendrequest?”

Only authorized users can answer the Search term. Please sign in first, or register a free account.