2021-09-22 23:09:30 +00:00
|
|
|
|
using Flow.Launcher.Plugin.BrowserBookmark.Models;
|
2021-09-21 18:50:51 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Text.Json;
|
2023-12-18 06:30:18 +00:00
|
|
|
|
using Flow.Launcher.Infrastructure.Logger;
|
2021-09-21 18:50:51 +00:00
|
|
|
|
|
2024-04-16 07:00:19 +00:00
|
|
|
|
namespace Flow.Launcher.Plugin.BrowserBookmark;
|
|
|
|
|
|
|
|
|
|
|
|
public abstract class ChromiumBookmarkLoader : IBookmarkLoader
|
2021-09-21 18:50:51 +00:00
|
|
|
|
{
|
2024-04-16 07:00:19 +00:00
|
|
|
|
public abstract List<Bookmark> GetBookmarks();
|
|
|
|
|
|
|
|
|
|
|
|
protected List<Bookmark> LoadBookmarks(string browserDataPath, string name)
|
2021-09-21 18:50:51 +00:00
|
|
|
|
{
|
2024-04-16 07:00:19 +00:00
|
|
|
|
var bookmarks = new List<Bookmark>();
|
|
|
|
|
|
if (!Directory.Exists(browserDataPath)) return bookmarks;
|
|
|
|
|
|
var paths = Directory.GetDirectories(browserDataPath);
|
2023-12-18 06:30:18 +00:00
|
|
|
|
|
2024-04-16 07:00:19 +00:00
|
|
|
|
foreach (var profile in paths)
|
2021-09-21 18:50:51 +00:00
|
|
|
|
{
|
2024-04-16 07:00:19 +00:00
|
|
|
|
var bookmarkPath = Path.Combine(profile, "Bookmarks");
|
|
|
|
|
|
if (!File.Exists(bookmarkPath))
|
|
|
|
|
|
continue;
|
2021-09-21 18:50:51 +00:00
|
|
|
|
|
2024-04-16 07:00:19 +00:00
|
|
|
|
Main.RegisterBookmarkFile(bookmarkPath);
|
2023-12-18 06:30:18 +00:00
|
|
|
|
|
2024-04-16 07:00:19 +00:00
|
|
|
|
var source = name + (Path.GetFileName(profile) == "Default" ? "" : $" ({Path.GetFileName(profile)})");
|
|
|
|
|
|
bookmarks.AddRange(LoadBookmarksFromFile(bookmarkPath, source));
|
2021-09-21 18:50:51 +00:00
|
|
|
|
}
|
2021-10-29 19:09:58 +00:00
|
|
|
|
|
2024-04-16 07:00:19 +00:00
|
|
|
|
return bookmarks;
|
|
|
|
|
|
}
|
2024-03-01 18:28:25 +00:00
|
|
|
|
|
2024-04-16 07:00:19 +00:00
|
|
|
|
protected List<Bookmark> LoadBookmarksFromFile(string path, string source)
|
|
|
|
|
|
{
|
|
|
|
|
|
var bookmarks = new List<Bookmark>();
|
2022-01-08 22:58:12 +00:00
|
|
|
|
|
2024-04-16 07:00:19 +00:00
|
|
|
|
if (!File.Exists(path))
|
2024-03-01 18:28:25 +00:00
|
|
|
|
return bookmarks;
|
|
|
|
|
|
|
2024-04-16 07:00:19 +00:00
|
|
|
|
using var jsonDocument = JsonDocument.Parse(File.ReadAllText(path));
|
|
|
|
|
|
if (!jsonDocument.RootElement.TryGetProperty("roots", out var rootElement))
|
|
|
|
|
|
return bookmarks;
|
|
|
|
|
|
EnumerateRoot(rootElement, bookmarks, source);
|
|
|
|
|
|
return bookmarks;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void EnumerateRoot(JsonElement rootElement, ICollection<Bookmark> bookmarks, string source)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var folder in rootElement.EnumerateObject())
|
2024-03-01 18:28:25 +00:00
|
|
|
|
{
|
2024-04-16 07:00:19 +00:00
|
|
|
|
if (folder.Value.ValueKind != JsonValueKind.Object)
|
|
|
|
|
|
continue;
|
2024-03-01 18:28:25 +00:00
|
|
|
|
|
2024-04-16 07:00:19 +00:00
|
|
|
|
// Fix for Opera. It stores bookmarks slightly different than chrome. See PR and bug report for this change for details.
|
|
|
|
|
|
// If various exceptions start to build up here consider splitting this Loader into multiple separate ones.
|
|
|
|
|
|
if (folder.Name == "custom_root")
|
|
|
|
|
|
EnumerateRoot(folder.Value, bookmarks, source);
|
|
|
|
|
|
else
|
|
|
|
|
|
EnumerateFolderBookmark(folder.Value, bookmarks, source);
|
2021-09-21 18:50:51 +00:00
|
|
|
|
}
|
2024-04-16 07:00:19 +00:00
|
|
|
|
}
|
2021-09-21 18:50:51 +00:00
|
|
|
|
|
2024-04-16 07:00:19 +00:00
|
|
|
|
private void EnumerateFolderBookmark(JsonElement folderElement, ICollection<Bookmark> bookmarks,
|
|
|
|
|
|
string source)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!folderElement.TryGetProperty("children", out var childrenElement))
|
|
|
|
|
|
return;
|
|
|
|
|
|
foreach (var subElement in childrenElement.EnumerateArray())
|
2021-09-21 18:50:51 +00:00
|
|
|
|
{
|
2024-04-16 07:00:19 +00:00
|
|
|
|
if (subElement.TryGetProperty("type", out var type))
|
2021-09-21 18:50:51 +00:00
|
|
|
|
{
|
2024-04-16 07:00:19 +00:00
|
|
|
|
switch (type.GetString())
|
2023-12-18 06:30:18 +00:00
|
|
|
|
{
|
2024-04-16 07:00:19 +00:00
|
|
|
|
case "folder":
|
|
|
|
|
|
case "workspace": // Edge Workspace
|
|
|
|
|
|
EnumerateFolderBookmark(subElement, bookmarks, source);
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
bookmarks.Add(new Bookmark(
|
|
|
|
|
|
subElement.GetProperty("name").GetString(),
|
|
|
|
|
|
subElement.GetProperty("url").GetString(),
|
|
|
|
|
|
source));
|
|
|
|
|
|
break;
|
2021-09-21 18:50:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-04-16 07:00:19 +00:00
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
Log.Error(
|
|
|
|
|
|
$"ChromiumBookmarkLoader: EnumerateFolderBookmark: type property not found for {subElement.GetString()}");
|
|
|
|
|
|
}
|
2021-09-21 18:50:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
avoid exception in ChromiumBookmarkLoader.cs
Every time I start Flow Launcher since the plugins refactor, the Bookmarks plugin has been throwing an exception. The issue stems from it parsing the Bookmarks file, and assuming that every JsonElement in the "roots" element is a JsonValueKind.Object. My Bookmarks file though has a JsonValueKind.String value off the roots key of `"sync_transaction_version": "20297",` along-side the bookmark_bar, other, and synced keys, which ARE objects. When it hits that sync_transaction_version string, and calls EnumerateFolderBookmark with it, it gets into the method because it IS a JsonElement, but then throws an exception on the folderElement.TryGetProperty("children", ...) call because you can't call TryGetProperty on a String, only Objects.
2022-03-09 01:54:54 +00:00
|
|
|
|
}
|