From 3f377d4efc2050b1f3cdf27fcc9ff361b199ea49 Mon Sep 17 00:00:00 2001 From: qianlifeng Date: Wed, 8 Jan 2014 23:21:37 +0800 Subject: [PATCH] add browser bookmark (chrome) plugin --- WinAlfred.Plugin.System/BrowserBookmarks.cs | 141 ++++++++++++++++++ .../WinAlfred.Plugin.System.csproj | 10 ++ WinAlfred.Plugin.System/packages.config | 4 + WinAlfred.Plugin/Result.cs | 5 + WinAlfred/Helper/SelectedRecords.cs | 49 +++++- WinAlfred/Images/bookmark.png | Bin 0 -> 4497 bytes 6 files changed, 202 insertions(+), 7 deletions(-) create mode 100644 WinAlfred.Plugin.System/BrowserBookmarks.cs create mode 100644 WinAlfred.Plugin.System/packages.config create mode 100644 WinAlfred/Images/bookmark.png diff --git a/WinAlfred.Plugin.System/BrowserBookmarks.cs b/WinAlfred.Plugin.System/BrowserBookmarks.cs new file mode 100644 index 0000000000..d7c86eeba4 --- /dev/null +++ b/WinAlfred.Plugin.System/BrowserBookmarks.cs @@ -0,0 +1,141 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Runtime.InteropServices; +using System.Text; +using System.Text.RegularExpressions; +using System.Windows.Forms; +using Newtonsoft.Json; +using WinAlfred.Plugin.System.Common; + +namespace WinAlfred.Plugin.System +{ + public class BrowserBookmarks : ISystemPlugin + { + + private List bookmarks = new List(); + + [DllImport("shell32.dll")] + static extern bool SHGetSpecialFolderPath(IntPtr hwndOwner, [Out] StringBuilder lpszPath, int nFolder, bool fCreate); + const int CSIDL_LOCAL_APPDATA = 0x001c; + + public List Query(Query query) + { + if (string.IsNullOrEmpty(query.RawQuery) || query.RawQuery.EndsWith(" ") || query.RawQuery.Length <= 1) return new List(); + + List returnList = bookmarks.Where(o => MatchProgram(o, query)).ToList(); + + return returnList.Select(c => new Result() + { + Title = c.Name, + SubTitle = "Bookmark: " + c.Url, + IcoPath = Directory.GetCurrentDirectory() + @"\Images\bookmark.png", + Score = 5, + Action = () => + { + try + { + Process.Start(c.Url); + } + catch (Exception e) + { + MessageBox.Show("open url failed:" + c.Url); + } + } + }).ToList(); + } + + private bool MatchProgram(Bookmark bookmark, Query query) + { + if (bookmark.Name.ToLower().Contains(query.RawQuery.ToLower()) || bookmark.Url.ToLower().Contains(query.RawQuery.ToLower())) return true; + if (ChineseToPinYin.ToPinYin(bookmark.Name).Replace(" ", "").ToLower().Contains(query.RawQuery.ToLower())) return true; + + return false; + } + + public void Init(PluginInitContext context) + { + LoadChromeBookmarks(); + } + + private void LoadChromeBookmarks() + { + StringBuilder platformPath = new StringBuilder(560); + SHGetSpecialFolderPath(IntPtr.Zero, platformPath, CSIDL_LOCAL_APPDATA, false); + + string path = platformPath + @"\Google\Chrome\User Data\Default\Bookmarks"; + if (File.Exists(path)) + { + string all = File.ReadAllText(path); + Regex nameRegex = new Regex("\"name\": \"(?.*?)\""); + MatchCollection nameCollection = nameRegex.Matches(all); + Regex typeRegex = new Regex("\"type\": \"(?.*?)\""); + MatchCollection typeCollection = typeRegex.Matches(all); + Regex urlRegex = new Regex("\"url\": \"(?.*?)\""); + MatchCollection urlCollection = urlRegex.Matches(all); + + List names = (from Match match in nameCollection select match.Groups["name"].Value).ToList(); + List types = (from Match match in typeCollection select match.Groups["type"].Value).ToList(); + List urls = (from Match match in urlCollection select match.Groups["url"].Value).ToList(); + + int urlIndex = 0; + for (int i = 0; i < names.Count; i++) + { + string name = DecodeUnicode(names[i]); + string type = types[i]; + if (type == "url") + { + string url = urls[urlIndex]; + urlIndex++; + + bookmarks.Add(new Bookmark() + { + Name = name, + Url = url, + Source = "Chrome" + }); + } + } + } + else + { +#if (DEBUG) + { + MessageBox.Show("load chrome bookmark failed"); + } +#endif + } + } + + 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()); + } + + public string Name + { + get + { + return "BrowserBookmark"; + } + } + + public string Description + { + get + { + return "BrowserBookmark"; + } + } + } + + public class Bookmark + { + public string Name { get; set; } + public string Url { get; set; } + public string Source { get; set; } + } +} diff --git a/WinAlfred.Plugin.System/WinAlfred.Plugin.System.csproj b/WinAlfred.Plugin.System/WinAlfred.Plugin.System.csproj index 779af70e42..9119d543e2 100644 --- a/WinAlfred.Plugin.System/WinAlfred.Plugin.System.csproj +++ b/WinAlfred.Plugin.System/WinAlfred.Plugin.System.csproj @@ -11,6 +11,8 @@ WinAlfred.Plugin.System v3.5 512 + ..\ + true true @@ -30,6 +32,9 @@ 4 + + ..\packages\Newtonsoft.Json.5.0.8\lib\net35\Newtonsoft.Json.dll + @@ -39,6 +44,7 @@ + @@ -54,11 +60,15 @@ WinAlfred.Plugin + + + +