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

Detailed tutorial on using Docker to build Gitlab based on CentOS8 system

Table of contents 1. Install Docker 2. Install Gi...

Example code for implementing a QR code scanning box with CSS

We usually have a scanning box when we open the c...

VMware vSphere 6.7 (ESXI 6.7) graphic installation steps

Environment: VMware VCSA 6.7 (VMware-VCSA-all-6.7...

A complete list of commonly used HTML tags and their characteristics

First of all, you need to know some characteristi...

CSS3 gradient background compatibility issues

When we make a gradient background color, we will...

Implementation of mysql backup strategy (full backup + incremental backup)

Table of contents Design scenario Technical Point...

IIS7 IIS8 http automatically jumps to HTTPS (port 80 jumps to port 443)

IIS7 needs to confirm whether the "URL REWRI...

Details on macrotasks and microtasks in JavaScript

Table of contents 1. What are microtasks? 2. What...

Detailed explanation of the process of zabbix monitoring sqlserver

Let's take a look at zabbix monitoring sqlser...

Discussion on the Issues of Image Button Submission and Form Repeated Submission

In many cases, in order to beautify the form, the ...

MySQL incremental backup and breakpoint recovery script example

Introduction Incremental backup means that after ...

Why Seconds_Behind_Master is still 0 when MySQL synchronization delay occurs

Table of contents Problem Description Principle A...