我正在尝试创建一个 chrome 扩展,但我遇到了 popup.js 的问题

问题描述 投票:0回答:1

未捕获(承诺)TypeError:无法获取 语境 弹出窗口.html 堆栈跟踪 popup.js:10(匿名函数) "const response = await fetch('http://localhost:5000/process', {"

document.getElementById('keyword-form').addEventListener('submit', async (event) => { 事件.preventDefault(); const 关键字 = document.getElementById('关键字').value;

// Define your subreddit names, start_date, and end_date here
const subredditNames = ['AskReddit', 'worldnews', 'gaming'];
const startDate = '2023-04-01';
const endDate = '2023-04-30';

const response = await fetch('http://localhost:5000/process', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    keyword,
    subreddit_names: subredditNames,
    start_date: startDate,
    end_date: endDate
  })
});


const result = await response.json();

// Update the result element with the response from the backend server
document.getElementById('result').innerHTML = JSON.stringify(result);

});

我熟悉python。这是我第一次尝试使用 chrome 扩展

在我最初的尝试中,我尝试使用 gstatic 创建图表和 chart.js,但这两个都遇到了 content_security_policy 的问题。

json google-chrome-extension popup localhost popup.js
1个回答
0
投票

您遇到的错误是由于对“http://localhost:5000/process”的“获取”请求无效造成的。修复它:

确保您的服务器在“localhost:5000”上运行,并设置了“/process”端点以接收 POST 请求。

这似乎更像是问题与服务器连接有关,而不是代码本身,因此请确保您的 json 文件配置为允许所需的权限和安全策略。

{
  "manifest_version": 2,
  "name": "Your Extension Name",
  "version": "1.0",
  "description": "Your Extension Description",
  "permissions": [
    "http://localhost:5000/"
  ],
  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
  },
  "content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'"
}
© www.soinside.com 2019 - 2024. All rights reserved.