Chrome扩展程序-未在请求中发送Cookie

时间:2019-05-29 21:34:03

标签: google-chrome-extension

我已经阅读了很多有关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文件。但仍然无法正常工作。

1 个答案:

答案 0 :(得分:0)

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

相关问题