Chrome monitors cookie changes and assigns values

Chrome monitors cookie changes and assigns values

The following code introduces Chrome's monitoring of cookie changes. The code is as follows:

/**
* Monitor cookie changes */
chrome.cookies.onChanged.addListener(function(changeInfo){
	// cookies.onChanged listens to all cookies, so we need to filter and process only our website's own cookies
	if(GhomepageDomain == changeInfo.cookie.domain){
		var cookieNameReg = /[AZ]/;
		var cookieInfo = changeInfo.cookie;
		if(!cookieNameReg.test(cookieInfo.name)){
			//Copy all lowercase cookie names to plugin
			if(changeInfo.removed){
				// Remove cookies
				chrome.cookies.remove({
					url : Gplugin,
					name : cookieInfo['name']
				},function(_cookie){
					// console.log('remove, re-acquire cookie',_cookie);
				 	// getUserInfo(1);
				});
			}else{
				// Set cookies
				chrome.cookies.set({
					url: Gplugin,
					name: cookieInfo['name'],
					path: '/',
					value: cookieInfo['value'],
					expirationDate: cookieInfo['expirationDate'],
					secure: true,
					sameSite: 'no_restriction', // Do not block cross-domain cookies
				},function(_cookie){
					// console.log('Set, re-obtain cookie',_cookie);
					// getUserInfo(1);
				});
			}
		}
	}
});

ps: Let's take a look at the cookie monitoring and assignment issues of CHROME extension notes.

Cookie monitoring and assignment operations require permissions to be declared in the manifest file.
The permissions are as follows:

{
	"permissions": [ "cookies", "*://*.Domain name to operate cookies.com/*" ],
}
/**
* Monitor cookie changes */
chrome.cookies.onChanged.addListener(function(changeInfo){
	// cookies.onChanged listens to all cookies, so we need to filter and process only our website's own cookies
	if(GhomepageDomain == changeInfo.cookie.domain){
		var cookieNameReg = /[AZ]/;
		var cookieInfo = changeInfo.cookie;
		if(!cookieNameReg.test(cookieInfo.name)){
			//Copy all lowercase cookie names to plugin
			if(changeInfo.removed){
				// Remove cookies
				chrome.cookies.remove({
					url : Gplugin,
					name : cookieInfo['name']
				},function(_cookie){
					// console.log('remove, re-acquire cookie',_cookie);
				 	// getUserInfo(1);
				});
			}else{
				// Set cookies
				chrome.cookies.set({
					url: Gplugin,
					name: cookieInfo['name'],
					path: '/',
					value: cookieInfo['value'],
					expirationDate: cookieInfo['expirationDate'],
					secure: true,
					sameSite: 'no_restriction', // Do not block cross-domain cookies. If secure and sameSite are not available, the iframe page will not be able to use cooke in chrome 80 and above.
				},function(_cookie){
					// console.log('Set, re-obtain cookie',_cookie);
					// getUserInfo(1);
				});
			}
		}
	}
});

Note: For those who don't understand secure and sameSite, please read Liao Xuefeng's blog on the SameSite attribute of cookies

This is the end of this article about Chrome's monitoring of cookie changes and assignment issues. For more relevant content about Chrome's monitoring of cookie changes, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • js uses cookies to refresh the unchanged tree menu

<<:  Analysis and solution of the reason why overflow-y: visible; does not work in CSS

>>:  Installation process of zabbix-agent on Kylin V10

Recommend

GDB debugging MySQL actual combat source code compilation and installation

Download source code git clone https://github.com...

Detailed explanation on how to modify the default port of nginx

First find out where the configuration file is wh...

Detailed usage of MYSQL row_number() and over() functions

Syntax format: row_number() over(partition by gro...

Docker installation tutorial in Linux environment

1. Installation environment Docker supports the f...

How to use Docker to build a tomcat cluster using nginx (with pictures and text)

First, create a tomcat folder. To facilitate the ...

Overview of the definition of HTC components after IE5.0

Before the release of Microsoft IE 5.0, the bigges...

Solution to inconsistent display of cursor size in input box

The cursor size in the input box is inconsistent T...

How to clear floating example code in css

Overview The framework diagram of this article is...

HTML elements (tags) and their usage

a : Indicates the starting or destination positio...

Use of Linux usermod command

1. Command Introduction The usermod (user modify)...

Detailed explanation of Jquery datagrid query

Table of contents Add code to the Tree item; 1. S...