简体   繁体   中英

How can I get JSON data from a browser extension (Chrome) from another server?

I am writing a browser extension that should periodically receive the exchange rate in the JSON format and place it in local storage. But when I try to do this in the code of a page of any site, I get error:

Access to XMLHttpRequest at 'https://my.server.domain/currency.txt' from origin 'https://developer.mozilla.org' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Sample request code

$updateUrl = "hhttps://my.server.domain/currency.txt";
$.get($updateUrl).done(function($updData) {

});

The only place where I can run a script like this is in popup.html , but I need updates to be triggered automatically. I tried to do it through Background but in manifest v3 there is no such possibility anymore.

The only option is to periodically open the update.html extension page and perform updates in it, but this happens by opening a new tab, and I would like to do this in the background.

Any tips?

In general, I wrote this in background.js and it works

fetch("https://my.server.domain/currency.txt")
.then((response) => {
    return response.json();
})
.then((data) => {
    console.log(data);
});

It remains now to figure out how to make the script periodically update the data, otherwise it works only once. But that is another story.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM