使用 cookie 从 Chrome 扩展发送 XHR 请求 [英] Send XHR request from Chrome extension with cookies
问题描述
我正在尝试从 Google Chrome 扩展程序向另一个域发送 XHR 请求.这可以正常工作,但我需要随请求发送该域 cookie.有什么办法可以做到这一点?
I'm trying to send an XHR request from a Google Chrome extension to another domain. This would work fine, but I need to send that domains cookies with the request. Any way to do this?
推荐答案
确保 manifest.json
permissions 正确设置.
您必须在 chrome 扩展的 manifest.json
中正确设置跨站域请求权限.正确完成后,已经为目标域设置的 cookie 将与您向该域发出的请求一起发送.manifest.json 文档
You have to properly set the cross site domain request permission in the manifest.json
of your chrome extension. When done properly, the cookies who are already set for the targeted domain will be sent along with the request you are making to that domain. manifest.json documentation
在玩 localhost:port_number
时必须特别小心.您需要在 manifest.json
中完整指定该域才能使其正常工作.当我的 localhost 域没有完整指定时,我的行为很尴尬.
You have to be especially careful when playing with localhost:port_number
. You will need to specify that domain in full in the manifest.json
for it to work. I ended up with awkward behaviors when my localhost domain was NOT specify in full.
这就是您希望在扩展的 manifest.json
中指定本地主机域的方式(如果有意义的话):
This is how you want to specify your localhost domain in the manifest.json
of your extension (if that makes sense):
...
"permissions": [
"http://localhost:3000/"
],
...
如果您要发送到目标域的cookie尚未设置,您可以使用chrome.cookies.set
方法并通过对象指定您想要的域名<您传递给 set
方法的 code>domain 属性.文档在这里: chrome.cookies.set.
If the cookies you want to send to the targeted domain are not set yet, you can do so my using the chrome.cookies.set
method and specify the domain name you want through the object domain
attribute you pass to the set
method. The documentation is here: chrome.cookies.set.
这篇关于使用 cookie 从 Chrome 扩展发送 XHR 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!