I would like to add a bookmarklet to Edge bookmark bar.
By exploring Edge user data, it seems that I could follow this approach:
$bk=Get-Content "$env:APPDATA\..\Local\Microsoft\Edge\User Data\Default\Bookmarks" | ConvertFrom-Json
$bk.roots.bookmark_bar.children += $newbk
$bk | ConvertTo-Json | Add-Content "$env:APPDATA\..\Local\Microsoft\Edge\User Data\Default\Bookmarks"
For an existing bookmark, values are like follow:
$bk.roots.bookmark_bar.children[0]
date_added : 13301408662223632
date_last_used : 0
guid : d9ab901f-c7b2-4daa-8f22-31147db571ac
id : 7
name : Microsoft – Cloud, Computers, Apps & Gaming
show_icon : False
source : user_copy
type : url
url : https://www.microsoft.com/en-us
I do not know, how I should exactly format the newbk
object.
Also should I recompute $bk.checksum
?
This is where I got so far, thanks to users JosefZ and zett42.
## Bookmark file
$bmpath="$env:APPDATA\..\Local\Microsoft\Edge\User Data\Default\Bookmarks"
## New bookmark
$newbk = [pscustomobject][ordered]@{guid=New-Guid; name="Test Bookmark"; show_icon=$false;
source="user_copy"; type="url";
url="https://www.microsoft.com/en-us"}
## New bookmarklet
$url = @'
javascript: (() => {
alert('Hello, World!');
})();
'@
$newbkjs = [pscustomobject][ordered]@{guid=New-Guid; name="Bookmarklet";
show_icon=$false; source="user_copy"; type="url"; url=$url}
## Read
$bk=Get-Content $bmpath | ConvertFrom-Json
## Add
$bk.roots.bookmark_bar.children += $newbk
$bk.roots.bookmark_bar.children += $newbkjs
$bk.psobject.Properties.Remove('checksum')
## Save
$bk | ConvertTo-Json -Depth 4 | Set-Content $bmpath
Limitations
I do not know how to make changes effective immediately.
If Edge is running, you need to close and reopen it.
If Edge is not running, you need to start, close, and start it again.
Additions
I haven't tried this, but for more sophisticated bookmarklets, it may be necessary URL encoding.
$url = [System.Web.HttpUtility]::UrlEncode($url)