首页 > 解决方案 > fetch() 和 XMLHttpRequest GET 不适用于 Chrome 扩展

问题描述

我开发了一个用户脚本,它工作得非常好。但是,我最近将其转换为 Chrome 扩展程序 - 部分有效。对于 Chrome 扩展程序,唯一不起作用的是fetch()XMLHttpRequest GET.

控制台中没有错误,我确保在清单中包含我试图请求的 URL。

这是我的清单:

{
  "manifest_version": 2,
  "name": "WallpaperUI",
  "version": "0.23",
  "description": "Customize your Steam Profile more effectively.",
  "icons": { "128": "UILogo.png" },
  "content_scripts": [
   {
      "matches": ["https://steamcommunity.com/id/*","https://steamcommunity.com/profiles/*"],
      "js": ["jquery-3.2.1.min.js", "WallpaperUI.user.js"]
    }
   ],
  "permissions": [
    "https://steamcommunity.com/*",
    "activeTab"
   ]
}

还有我的 JS:

fetch('https://steamcommunity.com/sharedfiles/filedetails/?id=XXXXXXXXXX')
    .then(function(response) {
    return response.text();
})
    .then(function(text) {
    console.log('Request successful');
    parseHTML(text);
})
    .catch(function(error) {
    console.log('Request failed', error);
});

标签: javascriptjsongoogle-chrome-extension

解决方案


推荐阅读