简体   繁体   English

"<i>How can I fetch data in my content script?<\/i>如何在我的内容脚本中获取数据?<\/b> <i>(chrome extension)<\/i> (镀铬扩展)<\/b>"

[英]How can I fetch data in my content script? (chrome extension)

I'm trying to fetch a simple .json file from my we accessible ressources into my content script.我正在尝试从我们可访问的资源中获取一个简单的 .json 文件到我的内容脚本中。 Here is what I tried so far:这是我到目前为止所尝试的:

manifest.json清单.json

 { "manifest_version": 3, "name": "MyExtension", "version": "0.0", "content_scripts": [{ "matches": ["*:\/\/*\/*"], "js": ["content.js"] }], "icons":{ "128": "images\/logo128x128.png" }, "web_accessible_resources": [{ "resources": ["data\/data.json"], "matches": ["https:\/\/web-accessible-resources-1.glitch.me\/*"] }] }<\/code><\/pre>

content.js内容.js

 const jsondict_url = chrome.runtime.getURL("data.json") console.log(jsondict_url) var jsondict = fetch(jsondict_url) console.log(jsondict)<\/code><\/pre>

This is the error message I get:这是我收到的错误消息:

 chrome-extension:\/\/hbjmkaohgphjcegliejkcehbmjeggnnb\/data.json physique-generale-mecanique-PHYS-101-A#:1 Denying load of chrome-extension:\/\/hbjmkaohgphjcegliejkcehbmjeggnnb\/data.json. Resources must be listed in the web_accessible_resources manifest key in order to be loaded by pages outside the extension. content.js:6 Promise {<pending>}[[Prototype]]: Promise [[PromiseState]]: "rejected" [[PromiseResult]]: TypeError: Failed to fetch at chrome-extension:\/\/hbjmkaohgphjcegliejkcehbmjeggnnb\/content.js:4:16 content.js:4 GET chrome-extension:\/\/invalid\/ net::ERR_FAILED (anonymous) @ content.js:4 content.js:4 Uncaught (in promise) TypeError: Failed to fetch at content.js:4:16 (anonymous) @ content.js:4<\/code><\/pre>

Edit: my files are organized this way编辑:我的文件是这样组织的

images\/ logo128x128.png图片\/ logo128x128.png

data\/data.json数据\/data.json

content.js内容.js

manifest.js manifest.js

"

You're trying getURL("data.json")<\/code> but your manifest states "resources": ["data\/data.json"]<\/code> so the path is incorrect in atleast one place?您正在尝试getURL("data.json")<\/code>但您的清单声明"resources": ["data\/data.json"]<\/code>所以路径至少在一个地方不正确? It should be getURL("data\/data.json")<\/code> .它应该是getURL("data\/data.json")<\/code> 。

You can also try adding content.js<\/code> to "web_accessible_resources"<\/code> .您也可以尝试将content.js<\/code>添加到"web_accessible_resources"<\/code> 。

"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Chrome扩展程序内容脚本中快速查看解析错误? - How can I quickly see parsing errors in my chrome extension content script? 如何在后台获取chrome扩展程序的内容脚本? - How can I get chrome extension's content script to in the background? 如何确定Chrome扩展程序是否位于内容脚本的弹出窗口中? - How can I determine if a Chrome extension is in a popup from the content script? 如何在 chrome 扩展内容脚本中使用 google api? - How can I use google api in chrome extension content script? 如何通过内容脚本将模板中的 HTML 插入 Chrome 扩展程序中的 div - How do I insert the HTML from a template into my div in my Chrome extension via a content script Chrome 扩展程序:如何获取 web 页面以访问我的内容脚本定义的功能? - Chrome Extension: How do I get a web page to access functions defined by my content script? Chrome扩展程序如何将我的内容脚本注入所有Facebook页面 - Chrome extension how to inject my content script into all Facebook pages 如何在Chrome扩展程序内容脚本中获取D3节点的数据 - How to get the data of a D3 node in Chrome Extension content script 当页面上已有旧版本时,如何在chrome扩展内容脚本中使用jquery - How can I use jquery in a chrome extension content script when an older version is already on the page 如何在Chrome扩展程序的内容脚本中将事件监听器添加到Google表格链接? - How can I add event Listener to google sheets link in chrome extension's content script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM