使用Cookie从Chrome扩展程序发送XHR请求 [英] Send XHR request from Chrome extension with cookies

查看:540
本文介绍了使用Cookie从Chrome扩展程序发送XHR请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试将谷歌浏览器扩展程序中的XHR请求发送到其他网域。这将工作正常,但我需要发送该域的cookie与请求。任何方式来做到这一点? manifest.json中正确设置跨网站域请求权限 设置正确 您的Chrome扩展程序的。正确完成后,已经为目标域设置的cookie将与您对该域进行的请求一起发送。 manifest.json文档



您必须特别在玩 localhost:port_number 时要小心。您需要在 manifest.json 中完整指定该域以使其工作。当我的localhost域名未被完整指定时,我最终遇到了一些尴尬的行为。



这就是您想要如何在清单中指定localhost域.json 您的扩展名(如果有意义的话):

  ... 
权限:[
http:// localhost:3000 /
],
...

如果您要发送到目标域的cookie尚未设置,您可以使用 chrome.cookies.set set 方法的对象 domain 属性指定所需的域名。文档位于: chrome.cookies.set


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?

解决方案

Make sure the manifest.json permissions are setup properly.

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

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.

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/"
  ],
...

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆