使用 javascript 从 cookie 中获取会话 ID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6214332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
obtain session Id from cookies using javascript
提问by user781022
I am trying to learn about cookies and what all they store. Since I am an active user of Google Chrome, I was thinking of accessing the session Id of a webpage from the cookie using only Javascript. I have obtained the list of all cookies stored in the browser using chrome.cookies.getAll()
.
我正在尝试了解 cookie 以及它们存储的内容。由于我是 Google Chrome 的活跃用户,因此我想仅使用 Javascript 从 cookie 访问网页的会话 ID。我已经使用chrome.cookies.getAll()
.
However I can't understand how to access the session Id from the cookies since there is no keyword for it ?
但是我无法理解如何从 cookie 访问会话 ID,因为它没有关键字?
How can I obtain the session ID from the cookies?
如何从 cookie 中获取会话 ID?
The code from obtaining the cookies is below :
获取cookies的代码如下:
<html><head>
<script>
chrome.browserAction.onClicked.addListener(getCookies);
var cacheMap = {};
var cookie_nameArr = [];
var cookie_valArr = [];
function getCookies(){
chrome.cookies.getAll({}, function(cookies){
for(var b in cookies){
var cookieVal = cookies[b].value;
var cookieName = cookies[b].domain;
cookie_nameArr.push(cookieName);
cookie_valArr.push(cookieVal);
if(!cacheMap[cookieName])
{
cacheMap[cookieName] = 1;
}
else
cacheMap[cookieName]++;
}//alert(cookie_nameArr.length + "," + cookie_valArr.length);
for(var i=0;i<3;i++)
{
alert(cookie_nameArr[i] + ", " + cookie_valArr[i]);
}
});
}
</script></head>
</body></html>
回答by serg
First you need to figure out what you are looking for. It's hard to look for a black cat in a dark room, especially if it is not there.
首先,您需要弄清楚您在寻找什么。在黑暗的房间里很难找到一只黑猫,特别是如果它不在那里。
Open Chrome dev tools on the page you are interested in and check Resources tab. Do you see your cookie under "Cookies"? If yes, then you will be able to read it with your method. If not, then check your page url - maybe it is passed as url parameter? If not, then maybe session is used only for ajax requests? Maybe it is not used at all?
在您感兴趣的页面上打开 Chrome 开发工具并检查资源选项卡。您在“Cookies”下看到您的 cookie 了吗?如果是,那么您将能够使用您的方法阅读它。如果没有,请检查您的页面 url - 也许它是作为 url 参数传递的?如果没有,那么会话可能仅用于 ajax 请求?也许它根本没有使用?
You need to find it manually first, then do it programmatically. Sessions come in all shapes and sizes, each site has its own way of implementing it. You can't build an extension that reads session for all sites.
您需要先手动查找它,然后以编程方式进行查找。会话有各种形状和大小,每个站点都有自己的实现方式。您无法构建读取所有站点会话的扩展程序。
Your approach is pretty much the way to go, just there is an easy way of inspecting objects using console instead of alert:
您的方法几乎是可行的方法,只是有一种使用控制台而不是警报来检查对象的简单方法:
chrome.cookies.getAll({domain: "stackoverflow.com"}, function(cookies){
console.log(cookies);
});
Click on background.html link on your extensions tab and check the console.
单击扩展选项卡上的 background.html 链接并检查控制台。