2019-08-06 18:36:28 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using Wox.Infrastructure;
|
2019-08-06 20:23:41 +08:00
|
|
|
|
using Wox.Plugin.SharedCommands;
|
2019-08-06 18:36:28 +08:00
|
|
|
|
|
|
|
|
|
namespace Wox.Plugin.BrowserBookmark
|
|
|
|
|
{
|
|
|
|
|
public class Main : IPlugin
|
|
|
|
|
{
|
|
|
|
|
private PluginInitContext context;
|
|
|
|
|
|
|
|
|
|
// TODO: periodically refresh the Cache?
|
|
|
|
|
private List<Bookmark> cachedBookmarks = new List<Bookmark>();
|
|
|
|
|
|
|
|
|
|
public void Init(PluginInitContext context)
|
|
|
|
|
{
|
|
|
|
|
this.context = context;
|
|
|
|
|
|
|
|
|
|
// Cache all bookmarks
|
|
|
|
|
var chromeBookmarks = new ChromeBookmarks();
|
|
|
|
|
var mozBookmarks = new FirefoxBookmarks();
|
|
|
|
|
|
|
|
|
|
//TODO: Let the user select which browser's bookmarks are displayed
|
|
|
|
|
// Add Firefox bookmarks
|
|
|
|
|
cachedBookmarks.AddRange(mozBookmarks.GetBookmarks());
|
|
|
|
|
// Add Chrome bookmarks
|
|
|
|
|
cachedBookmarks.AddRange(chromeBookmarks.GetBookmarks());
|
|
|
|
|
|
|
|
|
|
cachedBookmarks = cachedBookmarks.Distinct().ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<Result> Query(Query query)
|
|
|
|
|
{
|
|
|
|
|
string param = query.GetAllRemainingParameter().TrimStart();
|
|
|
|
|
|
|
|
|
|
// Should top results be returned? (true if no search parameters have been passed)
|
|
|
|
|
var topResults = string.IsNullOrEmpty(param);
|
|
|
|
|
|
|
|
|
|
var returnList = cachedBookmarks;
|
|
|
|
|
|
|
|
|
|
if (!topResults)
|
|
|
|
|
{
|
2019-09-29 12:30:33 +08:00
|
|
|
|
// Since we mixed chrome and firefox bookmarks, we should order them again
|
|
|
|
|
returnList = cachedBookmarks.Where(o => MatchProgram(o, param)).ToList();
|
2019-08-06 18:36:28 +08:00
|
|
|
|
returnList = returnList.OrderByDescending(o => o.Score).ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return returnList.Select(c => new Result()
|
|
|
|
|
{
|
|
|
|
|
Title = c.Name,
|
|
|
|
|
SubTitle = "Bookmark: " + c.Url,
|
|
|
|
|
IcoPath = @"Images\bookmark.png",
|
|
|
|
|
Score = 5,
|
|
|
|
|
Action = (e) =>
|
|
|
|
|
{
|
|
|
|
|
context.API.HideApp();
|
2019-08-06 20:23:41 +08:00
|
|
|
|
c.Url.NewBrowserWindow("");
|
2019-08-06 18:36:28 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}).ToList();
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-29 12:30:33 +08:00
|
|
|
|
private bool MatchProgram(Bookmark bookmark, string queryString)
|
2019-08-06 18:36:28 +08:00
|
|
|
|
{
|
2019-09-29 12:30:33 +08:00
|
|
|
|
if ((bookmark.Score = StringMatcher.FuzzySearch(queryString, bookmark.Name, new MatchOption()).Score) > 0) return true;
|
|
|
|
|
if ((bookmark.Score = StringMatcher.FuzzySearch(queryString, bookmark.PinyinName, new MatchOption).Score) > 0) return true;
|
|
|
|
|
if ((bookmark.Score = StringMatcher.FuzzySearch(queryString, bookmark.Url, new MatchOption()).Score / 10) > 0) return true;
|
2019-08-06 18:36:28 +08:00
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|