javascript - 从 chrome 扩展调用 API 时出现问题

标签 javascript google-chrome-extension

我正在尝试构建一个 chrome 扩展来制作内容脚本,我正在尝试进行 API 调用,但我无法得到响应。 我收到类型为 CORS 且状态为 200 的响应。 我已经将外部 API 主机附加到了 manifest.json 的权限部分

我的代码是:

fetch('http://www.virustotal.com/vtapi/v2/ip-address/report?ip=8.8.8.8&apikey=My API KEY')
    .then(res => {
        console.log(res);
// Response{type: cors, url:http://www.virustotal.com/....... status: 200}
        return res.json(); // Here is the problem
    })
    .then(data => {
        console.log(data);
    })

manifest.json:

{
    "name": "IP information on hover",
    "description": "IP information on hover",
    "version": "1.0",
    "manifest_version": 2,
    "browser_action": {
        "default_icon": "hello_extensions.png"
    },
    "permissions": [
        "https://www.virustotal.com/",
        "http://www.virustotal.com/"
    ],
    "content_scripts": [
        {
            "matches": [
                "http://*/*",
                "https://*/*"
            ],
            "js": [
                "jquery-3.4.0.js",
                "getIpReport.js"
            ],
            "run_at": "document_end"
        }
    ]
}

错误: 未捕获( promise 中)SyntaxError:JSON 输入意外结束 在 getIpReport.js:23

最佳答案

内容脚本无法再发出跨源请求,请在后台脚本中执行 谢谢wOxxOm

关于javascript - 从 chrome 扩展调用 API 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55904548/

相关文章:

javascript - 选项 beginAtZero 在 angular-chart.js 中不起作用

javascript - 当我在 HTML 中对 JS 函数使用 onkeyup 时,为什么控制台显示我的函数未定义?

google-chrome - chrome.extension.sendMessage() 中的 Chrome 扩展程序内存泄漏?

html - 获取网址并保存 | Chrome 扩展程序

javascript - Chrome Extension 无法让 ContextMenu 仅加载特定页面

javascript - jQuery DataTables - 按隐藏列排序日期

javascript - 如何避免来自对象下方的 div 的事件?

javascript - 无法在 Angular 中将 json 文件调用到工厂中

javascript - 在 Chrome 扩展中实现 Pusher

firefox - 我可以手动检查浏览器的readyState值吗?