代码改变世界

JS 获取跨域的cookie.

  chris-shao  阅读(2809)  评论(2编辑  收藏  举报

JS 获取跨域cookie? 简直是疯了。这怎么可能,不过在条件允许的情况下(两个域内文件都可以自定义),如下有一个方案。

一共需要3个文件,第一个文件为需要获取cookie的页面,在这个页面内嵌入存在网站B的获取cookie的代码,

第二个文件存在网站B,读取cookie,然后将自身URL修改为网站A域下,并将cookie拼接到URL中,

第三个文件在网站A,用于调用父页面处理得到的cookie.

 

代码如下:

 

文件一:假设为网站A下.1.html

<html>
<head>
</head>
<body>
<iframe src='http://B.com/2.html' width='100' height='100'>
</iframe>
<textarea id="sogou_cookie">
</textarea>

</body>
</html>

 

文件二:存在网站B下,名为2.html

<html>
<head>
</head>
<body>
<script>
window.location="http://A.com/3.html?"+document.cookie;
</script>
</body>
</html>

 

文件三:存在网站A下:

<html>
<head>
</head>
<body>
<script>
window.parent.document.getElementById("sogou_cookie").value=window.location.toString().substring(window.location.toString().indexOf("?"));
</script>
</body>
</html>

这样,当打开1.html时,文本框中内容就为B站的所有cookie了。

编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
点击右上角即可分享
微信分享提示