Aoxx
Aoxx

Reputation: 75

Using Powershell to add URL to Chrome cookies

I am trying to find out if there is a way to add a URL to Chrome cookies tab under the "Sites that can always use cookies" in settings. I have tried to pull the Json preferences file to see if it could be added there but not seeing it. Wondering if anyone can point me in the right direction.

Here is what I have so far:

$LocalAppData = [Environment]::GetFolderPath( [Environment+SpecialFolder]::LocalApplicationData )
$ChromeDefaults = Join-Path $LocalAppData "Google\Chrome\User Data\default"
$ChromePrefFile = Join-Path $ChromeDefaults "Preferences"
$Settings = Get-Content $ChromePrefFile | ConvertFrom-Json >> C:\Temp\Test1.txt

But this is not giving me what I thought it would, and searching around it seems no one has asked about adding a URL to this section/list before using powershell.

Upvotes: 0

Views: 1211

Answers (1)

Bennanaananneb
Bennanaananneb

Reputation: 96

TL;DR at the bottom.

After beautifying the preferences file I've found this is the correct path:

$Preferences = Get-Content -Path "C:\Users\xxx\AppData\Local\Google\Chrome\User Data\default\Preferences" | ConvertFrom-Json

$CookieExceptions = $Preferences.profile.content_settings.exceptions.cookies

$CookieExceptions returns a PSCustomObject:

stackexchange.com,*
-------------------
@{expiration=0; last_modified=13287790992647427; model=0; setting=1}

With in it a member that also has PSCustomObject type.

$CookieExceptions.'stackexchange.com,*' returns:

expiration last_modified     model setting
---------- -------------     ----- -------
0          13287790939152770     0       1

With the Get-Member cmdlet you can see what members the PSCustomObject has:

$CookieExceptions.'stackoverflow.com,*' | Get-Member -MemberType NoteProperty returns:

Name          MemberType   Definition                            
----          ----------   ----------                            
expiration    NoteProperty string expiration=0                   
last_modified NoteProperty string last_modified=13287790939152770
model         NoteProperty int model=0                           
setting       NoteProperty int setting=1

So to add a new site, you can create a similar object:

$SiteProperties = [PSCustomObject]@{
    expiration = 0
    last_modified = 13287790939152770
    model = 0
    setting = 1
}

And then add it to $CookiePreference with the correct sitename:

$CookieExceptions | Add-Member -MemberType NoteProperty -Name "stackoverflow.com,*" -Value $SiteProperties

Now you have two sites!

stackexchange.com,*                                                  stackoverflow.com,*                                                 
-------------------                                                  -------------------                                                 
@{expiration=0; last_modified=13287790939152770; model=0; setting=1} @{expiration=0; last_modified=13287790939152770; model=0; setting=1}

Don't forget to save it to the JSON preferences file.

Set-Content -Path "C:\Users\xxx\AppData\Local\Google\Chrome\User Data\default\Preferences" -Value ($Preferences | ConvertTo-Json -Depth 32)

TL;DR I have no idea what all the site properties do and how Chrome reacts to changing the preferences file externally. I have tested this briefly and it seemed to work. Below is the whole script. Good luck!

$Preferences = Get-Content -Path $PathToPreferencesFile | ConvertFrom-Json

$CookieExceptions = $Preferences.profile.content_settings.exceptions.cookies

$SiteProperties = [PSCustomObject]@{
    expiration = 0
    last_modified = 13287793933254853 #WebKit timestamp?
    model = 0
    setting = 1
}

$CookieExceptions | Add-Member -MemberType NoteProperty -Name "stackoverflow.com,*" -Value $SiteProperties

Set-Content -Path $PathToPreferencesFile -Value ($Preferences | ConvertTo-Json -Depth 32)

Upvotes: 3

Related Questions