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

Analysis of a MySQL deadlock scenario example

Preface Recently I encountered a deadlock problem...

Use tomcat to set shared lib to share the same jar

As more and more projects are deployed, more and ...

Mysql aggregate function nested use operation

Purpose: Nested use of MySQL aggregate functions ...

How to build Nginx image server with Docker

Preface In general development, images are upload...

Browser compatibility summary of common CSS properties (recommended)

Why do we need to summarize the browser compatibi...

One minute to experience the smoothness of html+vue+element-ui

Technology Fan html web page, you must know vue f...

A brief discussion on event-driven development in JS and Nodejs

Table of contents Event-driven and publish-subscr...

Implementation of k8s deployment of docker container

Environment: (docker, k8s cluster), continue with...

MySQL 8.0 New Features - Introduction to Check Constraints

Table of contents Preface Check Constraints Creat...

The difference between mysql outer join and inner join query

The syntax for an outer join is as follows: SELEC...

Write a publish-subscribe model with JS

Table of contents 1. Scene introduction 2 Code Op...

Let's talk about the Vue life cycle in detail

Table of contents Preface 1. Life cycle in Vue2 I...

How to implement nginx smooth restart

1. Background During the server development proce...