【问题标题】:Chrome Extension - not sending cookies in the requestChrome 扩展程序 - 不在请求中发送 cookie
【发布时间】:2019-10-15 13:17:11
【问题描述】:

我已经阅读了大量关于 SO 的问题,尝试了多种方法,但都没有成功。我的问题会很相似,但我发帖是因为我已经尝试了所有方法但仍然没有成功。

我想检查我的服务器上是否维护了一个用户会话。我有一个 web 服务,它根据收到的 cookie 返回一个布尔值。

浏览器有责任在请求服务器时发送 cookie(如果有)。因此,当我通过 chrome-extension 请求时,理论上浏览器也应该发送 cookie。但是,它没有。

这是我的代码:-

content.js

fetch('https://zoffers.in/apis/shops/get_aff_url/', {
    credentials: 'include',
    mode: 'cors',
    async: false,
    method: 'POST',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify({
        name: 'Hubot',
        login: 'hubot',
    })
})
.then(function(data) {

})
.catch(function(error) {

})

Manifest.json

{
    "manifest_version": 2,
    "version": "0.1",
    "content_scripts": [{
        "matches": [
            "https://*/*/"
        ],
        "js": ["js/content.js"],
        "run_at": "document_start"
    }],
    "browser_action": {
        // "default_icon": "icon.png",
        "default_popup": "popup.html",
        "default_title": "Click here!"
    },
    "permissions": [
        "identity",
        "identity.email",
        "tabs",
        "notifications",
        "cookies",
        "https://zoffers.in/"
    ]
}

我做错了什么?谁能帮我解决一下。

更新

content.js

message = {
    'event_type': 'Event1',
}
chrome.runtime.sendMessage(message);

background.js

chrome.runtime.onMessage.addListener(
    function(request, sender, sendResponse){
        if(request.event_type === 'Event1'){
            chrome.tabs.query({active: true, currentWindow: true}, function(tabs){
                fetch('https://zoffers.in/apis/shops/get_aff_url/', {
                    credentials: 'include',
                    mode: 'cors',
                    async: false,
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        name: 'Hubot',
                        login: 'hubot',
                    })
                })
                .then(function(data) {

                })
                .catch(function(error) {

                })
            }
        }
    }
);

已将background.js 添加到 manifest.json 文件中。但还是不行。

【问题讨论】:

  • Chrome 已弃用内容脚本中的跨域请求。在后台脚本中执行,如official CORB explainer所示。
  • 仍然没有帮助。即使将获取代码转移到 background.js 之后,请求仍然没有 cookie 标头。
  • 请检查更新后的代码。
  • 我也尝试与其他用户一起登录,但没有成功。它有什么问题?

标签: google-chrome-extension


【解决方案1】:

您可以使用标准 Cookies api 并访问任何可用 URL 的 cookie。

【讨论】:

  • 我如何将 cookie 设置到 Web 请求中?
  • @PythonEnthusiast 这是另一个问题。顺便说一句,您是否尝试过 'credentials: "include"' ? github.com/github/fetch/issues/452#issuecomment-397155739
  • 是的。检查问题。已经尝试使用credentials:include。我也研究过发送cookie,我们必须使用webRequest API。感谢您的帮助。
  • 但是,我相信问题出在其他地方。它与跨域请求有关。我在 DomainB 网站上向 DomainA 发出请求。这会阻止 cookie 自动发送。我正在尝试是否可以在不明确传递 cookie 的情况下自动发送它。我找到了xhr.withCredentials = true;,但它仍然不起作用。任何导致此问题的线索都会有所帮助。谢谢
猜你喜欢
  • 2012-06-20
  • 2014-09-26
  • 1970-01-01
  • 2019-01-14
  • 1970-01-01
  • 2014-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多