Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the change and assignment of cookie in chrome snooping

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

本篇内容介绍了"如何理解chrome监听cookie变化与赋值问题"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

代码如下所示:

/*** 监听cookie变化*/chrome.cookies.onChanged.addListener(function(changeInfo){ // cookies.onChanged监听的是所有的cookie,所以需要过滤只处理我们网站自己的cookie if(GhomepageDomain == changeInfo.cookie.domain){ var cookieNameReg = /[A-Z]/; var cookieInfo = changeInfo.cookie; if(!cookieNameReg.test(cookieInfo.name)){ // cookie小写名的全部复制到plugin if(changeInfo.removed){ // 移除cookie chrome.cookies.remove({ url : Gplugin, name : cookieInfo['name'] },function(_cookie){ // console.log('移除,重新获取cookie',_cookie); // getUserInfo(1); }); }else{ // 设置cookie chrome.cookies.set({ url: Gplugin, name: cookieInfo['name'], path: '/', value: cookieInfo['value'], expirationDate: cookieInfo['expirationDate'], secure: true, sameSite: 'no_restriction', // 不阻止跨域cookie },function(_cookie){ // console.log('设置,重新获取cookie',_cookie); // getUserInfo(1); }); } } }});

ps:下面看下CHROME扩展笔记之cookie监听与赋值问题。

cookie监听与赋值操作需要manifest文件里声明权限,cookie

权限如下:

{ "permissions": [ "cookies", "*://*.要操作cookie的域名.com/*" ],}/*** 监听cookie变化*/chrome.cookies.onChanged.addListener(function(changeInfo){ // cookies.onChanged监听的是所有的cookie,所以需要过滤只处理我们网站自己的cookie if(GhomepageDomain == changeInfo.cookie.domain){ var cookieNameReg = /[A-Z]/; var cookieInfo = changeInfo.cookie; if(!cookieNameReg.test(cookieInfo.name)){ // cookie小写名的全部复制到plugin if(changeInfo.removed){ // 移除cookie chrome.cookies.remove({ url : Gplugin, name : cookieInfo['name'] },function(_cookie){ // console.log('移除,重新获取cookie',_cookie); // getUserInfo(1); }); }else{ // 设置cookie chrome.cookies.set({ url: Gplugin, name: cookieInfo['name'], path: '/', value: cookieInfo['value'], expirationDate: cookieInfo['expirationDate'], secure: true, sameSite: 'no_restriction', // 不阻止跨域cookie,如果没有secure和sameSite这两个那么在chrome80以上的版本会出现iframe页面无法使用cooke },function(_cookie){ // console.log('设置,重新获取cookie',_cookie); // getUserInfo(1); }); } } }});

注意:关于secure和sameSite不懂的可看廖雪峰的博客cookie的SameSite属性一篇

"如何理解chrome监听cookie变化与赋值问题"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report