2014-01-08 23:21:37 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Text.RegularExpressions;
|
2014-02-06 22:22:02 +08:00
|
|
|
|
using Wox.Infrastructure;
|
2014-01-08 23:21:37 +08:00
|
|
|
|
|
2014-06-30 18:05:42 +08:00
|
|
|
|
namespace Wox.Plugin.BrowserBookmark
|
2014-01-08 23:21:37 +08:00
|
|
|
|
{
|
2014-06-30 18:05:42 +08:00
|
|
|
|
public class Main : IPlugin
|
2014-01-08 23:21:37 +08:00
|
|
|
|
{
|
2014-03-21 03:53:18 +08:00
|
|
|
|
private PluginInitContext context;
|
2014-01-08 23:21:37 +08:00
|
|
|
|
private List<Bookmark> bookmarks = new List<Bookmark>();
|
|
|
|
|
|
2014-06-30 18:05:42 +08:00
|
|
|
|
|
|
|
|
|
public void Init(PluginInitContext context)
|
2014-01-08 23:21:37 +08:00
|
|
|
|
{
|
2014-06-30 18:05:42 +08:00
|
|
|
|
if (!Wox.Infrastructure.Storage.UserSettings.UserSettingStorage.Instance.EnableBookmarkPlugin)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bookmarks.Clear();
|
|
|
|
|
LoadChromeBookmarks();
|
|
|
|
|
|
|
|
|
|
bookmarks = bookmarks.Distinct().ToList();
|
|
|
|
|
this.context = context;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<Result> Query(Query query)
|
|
|
|
|
{
|
|
|
|
|
if (query.ActionParameters.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
return bookmarks.Select(c => new Result()
|
|
|
|
|
{
|
|
|
|
|
Title = c.Name,
|
|
|
|
|
SubTitle = "Bookmark: " + c.Url,
|
|
|
|
|
IcoPath = @"Images\bookmark.png",
|
|
|
|
|
Score = 5,
|
|
|
|
|
Action = (e) =>
|
|
|
|
|
{
|
|
|
|
|
context.HideApp();
|
|
|
|
|
context.ShellRun(c.Url);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}).ToList();
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-08 23:21:37 +08:00
|
|
|
|
|
2014-03-17 03:37:52 +08:00
|
|
|
|
var fuzzyMather = FuzzyMatcher.Create(query.RawQuery);
|
2014-03-25 13:36:33 +08:00
|
|
|
|
List<Bookmark> returnList = bookmarks.Where(o => MatchProgram(o, fuzzyMather)).ToList();
|
|
|
|
|
returnList = returnList.OrderByDescending(o => o.Score).ToList();
|
2014-01-08 23:21:37 +08:00
|
|
|
|
return returnList.Select(c => new Result()
|
|
|
|
|
{
|
|
|
|
|
Title = c.Name,
|
|
|
|
|
SubTitle = "Bookmark: " + c.Url,
|
2014-06-30 18:05:42 +08:00
|
|
|
|
IcoPath = @"Images\bookmark.png",
|
2014-01-08 23:21:37 +08:00
|
|
|
|
Score = 5,
|
2014-03-21 03:53:18 +08:00
|
|
|
|
Action = (e) =>
|
2014-01-08 23:21:37 +08:00
|
|
|
|
{
|
2014-03-21 03:53:18 +08:00
|
|
|
|
context.HideApp();
|
|
|
|
|
context.ShellRun(c.Url);
|
2014-02-28 23:21:01 +08:00
|
|
|
|
return true;
|
2014-01-08 23:21:37 +08:00
|
|
|
|
}
|
|
|
|
|
}).ToList();
|
|
|
|
|
}
|
2014-06-30 18:05:42 +08:00
|
|
|
|
|
2014-03-25 13:36:33 +08:00
|
|
|
|
private bool MatchProgram(Bookmark bookmark, FuzzyMatcher matcher)
|
2014-01-08 23:21:37 +08:00
|
|
|
|
{
|
2014-03-25 13:36:33 +08:00
|
|
|
|
if ((bookmark.Score = matcher.Evaluate(bookmark.Name).Score) > 0) return true;
|
|
|
|
|
if ((bookmark.Score = matcher.Evaluate(bookmark.PinyinName).Score) > 0) return true;
|
|
|
|
|
if ((bookmark.Score = matcher.Evaluate(bookmark.Url).Score / 10) > 0) return true;
|
|
|
|
|
|
2014-01-08 23:21:37 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-17 12:32:19 +08:00
|
|
|
|
private void ParseChromeBookmarks(String path, string source)
|
2014-01-08 23:21:37 +08:00
|
|
|
|
{
|
2014-03-17 12:32:19 +08:00
|
|
|
|
if (!File.Exists(path)) return;
|
|
|
|
|
|
|
|
|
|
string all = File.ReadAllText(path);
|
|
|
|
|
Regex nameRegex = new Regex("\"name\": \"(?<name>.*?)\"");
|
|
|
|
|
MatchCollection nameCollection = nameRegex.Matches(all);
|
|
|
|
|
Regex typeRegex = new Regex("\"type\": \"(?<type>.*?)\"");
|
|
|
|
|
MatchCollection typeCollection = typeRegex.Matches(all);
|
|
|
|
|
Regex urlRegex = new Regex("\"url\": \"(?<url>.*?)\"");
|
|
|
|
|
MatchCollection urlCollection = urlRegex.Matches(all);
|
2014-01-08 23:21:37 +08:00
|
|
|
|
|
2014-03-17 12:32:19 +08:00
|
|
|
|
List<string> names = (from Match match in nameCollection select match.Groups["name"].Value).ToList();
|
|
|
|
|
List<string> types = (from Match match in typeCollection select match.Groups["type"].Value).ToList();
|
|
|
|
|
List<string> urls = (from Match match in urlCollection select match.Groups["url"].Value).ToList();
|
|
|
|
|
|
|
|
|
|
int urlIndex = 0;
|
|
|
|
|
for (int i = 0; i < names.Count; i++)
|
2014-01-08 23:21:37 +08:00
|
|
|
|
{
|
2014-03-17 12:32:19 +08:00
|
|
|
|
string name = DecodeUnicode(names[i]);
|
|
|
|
|
string type = types[i];
|
|
|
|
|
if (type == "url")
|
2014-01-08 23:21:37 +08:00
|
|
|
|
{
|
2014-03-17 12:32:19 +08:00
|
|
|
|
string url = urls[urlIndex];
|
|
|
|
|
urlIndex++;
|
|
|
|
|
|
|
|
|
|
if (url == null) continue;
|
|
|
|
|
if (url.StartsWith("javascript:", StringComparison.OrdinalIgnoreCase)) continue;
|
|
|
|
|
if (url.StartsWith("vbscript:", StringComparison.OrdinalIgnoreCase)) continue;
|
|
|
|
|
|
|
|
|
|
bookmarks.Add(new Bookmark()
|
2014-01-08 23:21:37 +08:00
|
|
|
|
{
|
2014-03-17 12:32:19 +08:00
|
|
|
|
Name = name,
|
|
|
|
|
Url = url,
|
|
|
|
|
Source = source
|
|
|
|
|
});
|
2014-01-08 23:21:37 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-17 12:32:19 +08:00
|
|
|
|
private void LoadChromeBookmarks(string path, string name)
|
|
|
|
|
{
|
|
|
|
|
if (!Directory.Exists(path)) return;
|
|
|
|
|
var paths = Directory.GetDirectories(path);
|
|
|
|
|
|
|
|
|
|
foreach (var profile in paths)
|
|
|
|
|
{
|
|
|
|
|
if (File.Exists(Path.Combine(profile, "Bookmarks")))
|
|
|
|
|
ParseChromeBookmarks(Path.Combine(profile, "Bookmarks"), name + (Path.GetFileName(profile) == "Default" ? "" : (" (" + Path.GetFileName(profile) + ")")));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void LoadChromeBookmarks()
|
|
|
|
|
{
|
|
|
|
|
String platformPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
|
|
|
|
|
LoadChromeBookmarks(Path.Combine(platformPath, @"Google\Chrome\User Data"), "Google Chrome");
|
|
|
|
|
LoadChromeBookmarks(Path.Combine(platformPath, @"Google\Chrome SxS\User Data"), "Google Chrome Canary");
|
|
|
|
|
LoadChromeBookmarks(Path.Combine(platformPath, @"Chromium\User Data"), "Chromium");
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-08 23:21:37 +08:00
|
|
|
|
private String DecodeUnicode(String dataStr)
|
|
|
|
|
{
|
|
|
|
|
Regex reg = new Regex(@"(?i)\\[uU]([0-9a-f]{4})");
|
|
|
|
|
return reg.Replace(dataStr, m => ((char)Convert.ToInt32(m.Groups[1].Value, 16)).ToString());
|
|
|
|
|
}
|
2014-03-28 22:42:28 +08:00
|
|
|
|
|
2014-06-30 18:05:42 +08:00
|
|
|
|
public string Name
|
2014-03-28 22:42:28 +08:00
|
|
|
|
{
|
|
|
|
|
get { return "Bookmarks"; }
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-30 18:05:42 +08:00
|
|
|
|
public string IcoPath
|
2014-03-28 22:42:28 +08:00
|
|
|
|
{
|
|
|
|
|
get { return @"Images\bookmark.png"; }
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-30 18:05:42 +08:00
|
|
|
|
public string Description
|
2014-03-17 03:37:52 +08:00
|
|
|
|
{
|
2014-06-30 18:05:42 +08:00
|
|
|
|
get { return "System workflow"; }
|
2014-03-17 03:37:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-08 23:21:37 +08:00
|
|
|
|
}
|
|
|
|
|
}
|