Flow.Launcher/Plugins/Flow.Launcher.Plugin.BrowserBookmark/ChromiumBookmarkLoader.cs
Jeremy Wu 6dedb4fc40
Release 1.9.4 (#1287)
* Merge pull request #1205 from Flow-Launcher/fix_cmd_command_with_blank

Fix shell cmd command with quote and space

* Bump NuGet.CommandLine from 5.4.0 to 5.7.2 (#1241)

* Merge pull request #1098 from Flow-Launcher/ScrollToSelectedPlugin

Scroll to selected item when expanded or size changed

* fix RemoveOldQueryResults NullPointerException (#1204)

* Merge pull request #1005 from Flow-Launcher/KillProcess

Use Cancellation Token to avoid potential race tracing issue

* Merge pull request #1187 from Flow-Launcher/update_python_download_mirrors

Update Python download mirrors

* Merge pull request #1108 from Flow-Launcher/CalculatorDecimalSeparator

Respect Decimal Separator for query not just result

* Merge pull request #1087 from Flow-Launcher/turnoff_replace_win_r_shell

Set Shell plugin's default replace Win R hotkey to off

* Merge pull request #1077 from Flow-Launcher/fix_explorer_button_visibility

Fix incorrect button visibility in Explorer's expander control

* Merge pull request #1076 from Flow-Launcher/fix_path_search_with_index

Fix the use of index in path search

* Merge pull request #1071 from medlir/fix-browser-bookmarks-plugin-exception

avoid exception in ChromiumBookmarkLoader.cs

* Merge pull request #1056 from Flow-Launcher/fix_context_menu_typo

Fix typo for plugin title in context menu and WindowsSettings name

* Merge pull request #1119 from onesounds/antialising

Remove All Cleartype Rendering

* Version bump

* Remove Calculator plugin CopyText feature for 1.9.4 release
2022-07-24 11:39:31 +10:00

68 lines
2.6 KiB
C#

using Flow.Launcher.Plugin.BrowserBookmark.Models;
using Microsoft.AspNetCore.Authentication;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
namespace Flow.Launcher.Plugin.BrowserBookmark
{
public abstract class ChromiumBookmarkLoader : IBookmarkLoader
{
public abstract List<Bookmark> GetBookmarks();
protected List<Bookmark> LoadBookmarks(string browserDataPath, string name)
{
var bookmarks = new List<Bookmark>();
if (!Directory.Exists(browserDataPath)) return bookmarks;
var paths = Directory.GetDirectories(browserDataPath);
foreach (var profile in paths)
{
var bookmarkPath = Path.Combine(profile, "Bookmarks");
if (!File.Exists(bookmarkPath))
continue;
var source = name + (Path.GetFileName(profile) == "Default" ? "" : $" ({Path.GetFileName(profile)})");
bookmarks.AddRange(LoadBookmarksFromFile(bookmarkPath, source));
}
return bookmarks;
}
protected List<Bookmark> LoadBookmarksFromFile(string path, string source)
{
if (!File.Exists(path))
return new();
var bookmarks = new List<Bookmark>();
using var jsonDocument = JsonDocument.Parse(File.ReadAllText(path));
if (!jsonDocument.RootElement.TryGetProperty("roots", out var rootElement))
return new();
foreach (var folder in rootElement.EnumerateObject())
{
if (folder.Value.ValueKind == JsonValueKind.Object)
EnumerateFolderBookmark(folder.Value, bookmarks, source);
}
return bookmarks;
}
private void EnumerateFolderBookmark(JsonElement folderElement, List<Bookmark> bookmarks, string source)
{
if (!folderElement.TryGetProperty("children", out var childrenElement))
return;
foreach (var subElement in childrenElement.EnumerateArray())
{
switch (subElement.GetProperty("type").GetString())
{
case "folder":
EnumerateFolderBookmark(subElement, bookmarks, source);
break;
default:
bookmarks.Add(new Bookmark(
subElement.GetProperty("name").GetString(),
subElement.GetProperty("url").GetString(),
source));
break;
}
}
}
}
}