簡體   English   中英

如何從另一台服務器的瀏覽器擴展 (Chrome) 獲取 JSON 數據?

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

我正在編寫一個瀏覽器擴展程序,它應該定期接收 JSON 格式的匯率並將其放在本地存儲中。 但是當我嘗試在任何網站的頁面代碼中執行此操作時,我會收到錯誤消息:

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.

示例請求代碼

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

});

我可以運行這樣的腳本的唯一地方是popup.html ,但我需要自動觸發更新。 我試圖通過背景做到這一點,但在清單 v3中不再有這種可能性。

唯一的選擇是定期打開 update.html 擴展頁面並在其中執行更新,但這是通過打開一個新選項卡來實現的,我想在后台執行此操作。

有小費嗎?

一般來說,我在background.js中寫了這個並且它有效

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

現在仍然要弄清楚如何使腳本定期更新數據,否則它只能工作一次。 不過那是另一回事了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM