【发布时间】: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 标头。
-
请检查更新后的代码。
-
我也尝试与其他用户一起登录,但没有成功。它有什么问题?