Skip to content

Commit

Permalink
Merge pull request #793 from Splaxi/impl-new-bookmark
Browse files Browse the repository at this point in the history
Refactor: Make favoritebookmark work with edge and chrome
  • Loading branch information
Splaxi authored Dec 13, 2023
2 parents 6e17539 + a940b79 commit 1c80b96
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 22 deletions.
2 changes: 1 addition & 1 deletion d365fo.tools/bin/d365fo.tools-index.json
Original file line number Diff line number Diff line change
Expand Up @@ -10526,7 +10526,7 @@
},
{
"CommandName": "Set-D365FavoriteBookmark",
"Description": "Enable the favorite bar in internet explorer and put in the URL as a favorite",
"Description": "Enable the favorite bar in Edge \u0026 Chrome and put in the URL as a favorite/bookmark",
"Params": [
[
"URL",
Expand Down
35 changes: 15 additions & 20 deletions d365fo.tools/functions/set-d365favoritebookmark.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Enable the favorite bar and add an URL
.DESCRIPTION
Enable the favorite bar in internet explorer and put in the URL as a favorite
Enable the favorite bar in Edge & Chrome and put in the URL as a favorite/bookmark
.PARAMETER URL
The URL of the shortcut you want to add to the favorite bar
Expand Down Expand Up @@ -45,7 +45,7 @@
#>
function Set-D365FavoriteBookmark {
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "")]
[CmdletBinding(DefaultParameterSetName="D365FO")]
[CmdletBinding(DefaultParameterSetName = "D365FO")]
param (
[Parameter(ValueFromPipelineByPropertyName = $true)]
[string] $URL,
Expand All @@ -62,30 +62,25 @@ function Set-D365FavoriteBookmark {

process {
if ($PSCmdlet.ParameterSetName -eq "D365FO") {
$fileName = "D365FO.url"
$name = "D365FO"
}
else{
$fileName = "AzureDevOps.url"
else {
$name = "AzureDevOps"
}

$filePath = Join-Path (Join-Path $Home "Favorites\Links") $fileName

$pathShowBar = 'HKCU:\Software\Microsoft\Internet Explorer\MINIE\'
$propShowBar = 'LinksBandEnabled'
# Is edge installed?
$pathEdgeBase = "$($env:LOCALAPPDATA)\Microsoft\Edge\User Data\Default"

$pathLockBar = 'HKCU:\Software\Microsoft\Internet Explorer\Toolbar\'
$propLockBar = 'Locked'

$value = "00000001"

Write-PSFMessage -Level Verbose -Message "Setting the show bar and lock bar registry values."
Set-ItemProperty -Path $pathShowBar -Name $propShowBar -Value $value -Type "DWord"
Set-ItemProperty -Path $pathLockBar -Name $propLockBar -Value $value -Type "DWord"
if (Test-PathExists -Path $pathEdgeBase -Type Container) {
Set-BrowserBookmark -PathBrowser $pathEdgeBase -Uri $URL -Name $name
}

$null = New-Item -Path $filePath -Force -ErrorAction SilentlyContinue
# Is chrome installed?
$pathChromeBase = "$($env:LOCALAPPDATA)\Google\Chrome\User Data\Default"

$LinkContent = (Get-Content "$script:ModuleRoot\internal\misc\$fileName") -Join [Environment]::NewLine
$LinkContent.Replace("##URL##", $URL) | Out-File $filePath -Force
if (Test-PathExists -Path $pathChromeBase -Type Container) {
Set-BrowserBookmark -PathBrowser $pathChromeBase -Uri $URL -Name $name
}
}

end {
Expand Down
85 changes: 85 additions & 0 deletions d365fo.tools/internal/functions/set-browserbookmark.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@

<#
.SYNOPSIS
Set a new bookmark in the browser
.DESCRIPTION
Add a new bookmark to the favorite bar in the browser
Edge and Chrome behaves the same
.PARAMETER PathBrowser
Path to the root folder of the profile in the browser
Only default is tested / handled
.PARAMETER Uri
Uri of the system that you want to add as a bookmark
.PARAMETER Name
Name of the bookmark entry
.EXAMPLE
PS C:\> Set-BrowserBookmark -PathBrowser 'C:\Users\Admin....\AppData\Local\Microsoft\Edge\User Data\Default' -Uri 'https://devdevaos.axcloud.dynamics.com/?cmp=DAT&mi=DefaultDashboard' -Name "D365FO"
This will work against the Edge browser.
The bookmark will be for the 'https://devdevaos.axcloud.dynamics.com/?cmp=DAT&mi=DefaultDashboard' system.
The name will be "D365FO".
.NOTES
Author: Mötz Jensen (@Splaxi)
#>
function Set-BrowserBookmark {
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "")]
[CmdletBinding()]
param (
[string] $PathBrowser,

[string] $Uri,

[string] $Name
)

$pathBase = "$PathBrowser"

if ((Test-PathExists -Path $pathBase -Type Container)) {
$prefRaw = Get-Content -Path "$pathBase\Preferences" -Raw
$prefObj = $prefRaw | ConvertFrom-Json

if ($null -eq $prefObj.bookmark_bar) {
# Sometimes the needed settings is missing
$prefRaw.Replace(',"browser":', ',"bookmark_bar":{"show_on_all_tabs": true,"show_only_on_ntp":false},"browser":') | Out-File -FilePath "$pathBase\Preferences" -Encoding utf8 -Force
}
else {
$prefObj.bookmark_bar.show_on_all_tabs = $true
$($prefObj | ConvertTo-Json -Depth 10).Replace("`r`n", "") | Out-File -FilePath "$pathBase\Preferences" -Encoding utf8 -Force > $null
}

if (-not (Test-PathExists -Path "$pathBase\Bookmarks" -Type Leaf)) {
# We might be handling bookmarks / favorites for the first time
Copy-Item -Path "$script:ModuleRoot\internal\misc\Bookmarks" -Destination "$pathBase\Bookmarks" -Force > $null
}

$favRaw = Get-Content -Path "$pathBase\Bookmarks" -Raw
$favObj = $favRaw | ConvertFrom-Json

# If any bookmarks already exists - we need to up the counter / id
$id = [int]$($favObj.roots.bookmark_bar.children.id | Sort-Object -Descending | Select-Object -First 1)
$id++

$bookMark = [PsCustomObject][Ordered]@{
guid = [System.Guid]::NewGuid().Guid
id = $id
name = $name
type = "url"
url = $URL
}

# The children property is an array - which is a fixed size
$children = [System.Collections.Generic.List[System.Object]]::new($favObj.roots.bookmark_bar.children)

$children.Add($bookMark)
$favObj.roots.bookmark_bar.children = $children.ToArray()
$($favObj | ConvertTo-Json -Depth 10) | Out-File -FilePath "$pathBase\Bookmarks" -Encoding utf8 -Force > $null
}
}
30 changes: 30 additions & 0 deletions d365fo.tools/internal/misc/Bookmarks
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"checksum": "a38a604289a5cdc83c5a0397d18a310e",
"roots": {
"bookmark_bar": {
"children": [],
"guid": "0bc5d13f-2cba-5d74-951f-3f233fe6c908",
"id": "1",
"name": "Favourites bar",
"source": "unknown",
"type": "folder"
},
"other": {
"children": [],
"guid": "82b081ec-3dd3-529c-8475-ab6c344590dd",
"id": "2",
"name": "Other favourites",
"source": "unknown",
"type": "folder"
},
"synced": {
"children": [],
"guid": "4cf2e351-0e85-532b-bb37-df045d8f8d0f",
"id": "3",
"name": "Mobile favourites",
"source": "unknown",
"type": "folder"
}
},
"version": 1
}
2 changes: 1 addition & 1 deletion docs/Set-D365FavoriteBookmark.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Set-D365FavoriteBookmark [-URL <String>] [-AzureDevOps] [<CommonParameters>]
```

## DESCRIPTION
Enable the favorite bar in internet explorer and put in the URL as a favorite
Enable the favorite bar in Edge & Chrome and put in the URL as a favorite/bookmark

## EXAMPLES

Expand Down

0 comments on commit 1c80b96

Please sign in to comment.