Change the 'SameSite' cookie property

Discussion in 'JavaScript' started by Alex100, Jul 29, 2023.

  1. #1
    Can someone please tell me how I can change the SameSite cookie property (to None) for all the cookies written by a website?

    I injected the script below into the google.com website (using Tampermonkey), but it only changed one or two cookies, not all of them. Moreover, Google creates cookies under two addresses ('www.google.com' and '.google.com'), but the script did not change any cookie found under the '.google.com' domain name.

    Thank you!

    Alex
    // ==UserScript==
    // @name         Change the 'SameSite' cookie property
    // @namespace    http://tampermonkey.net/
    // @match        *://*/*
    // @grant        none
    // ==/UserScript==
    
    setTimeout(function() {
    
      // Get all existing cookies as an array of key-value pairs
      const cookiesArray = document.cookie.split(';');
    
      // Loop through each cookie and modify it to add SameSite=None and Secure
      const updatedCookiesArray = cookiesArray.map(cookie => {
        const cookieParts = cookie.trim().split('=');
        const cookieName = cookieParts[0];
        const cookieValue = cookieParts.slice(1).join('=');
        return `${cookieName}=${cookieValue} [CHANGED]; SameSite=None; Secure`;
      });
    
      // Join the modified cookies into a single string
      const updatedCookiesString = updatedCookiesArray.join('; ');
    
      // Update all cookies in the browser
      document.cookie = updatedCookiesString;
    
      alert ('Cookies changed!');
    
    }, 5000);
    Code (JavaScript):
     
    Alex100, Jul 29, 2023 IP