user3206440
user3206440

Reputation: 5059

Calling backend API from chrome extension

I need to make GET http calls to ‘http://localhost:3000/posts’ from a dev chrome page action extension. With the GET action I get a list of posts which need to be displayed in popup.html. In ‘content.js’ I fetch a list of topics from current tab to pass as a query parameter in GET request.

  1. How to make a GET request from a chrome extension ? - a tutorial link on http requests from chrome extension if available would be helpful.
  2. From where should be the get request made - content.js, popup.js or background.js ?

Upvotes: 1

Views: 7946

Answers (1)

Pavel Agarkov
Pavel Agarkov

Reputation: 3783

Since chrome extension is just a html/css/js - to make web requests you can use the same techniques as you do on normal websites. E.g. fetch api or XHR.

From where to call depends only from you. If you want to cache the results background page might be better, but then you would need to use messaging to transfer data from background page to popup page. Otherwise you can call your api right from the popup script.

Anyway you might have problems with CORS when working with api hosted on localhost (it is a known chrome limitation). With normal page you can use one of many extensions that automatically add CORS headers to all requests, but I'm not sure they would work with requests from extensions.

Upvotes: 5

Related Questions