mirror of
https://github.com/microsoft/PowerToys.git
synced 2024-11-27 23:19:13 +08:00
add browser bookmark (chrome) plugin
This commit is contained in:
parent
881d265579
commit
3f377d4efc
141
WinAlfred.Plugin.System/BrowserBookmarks.cs
Normal file
141
WinAlfred.Plugin.System/BrowserBookmarks.cs
Normal file
@ -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<Bookmark> bookmarks = new List<Bookmark>();
|
||||
|
||||
[DllImport("shell32.dll")]
|
||||
static extern bool SHGetSpecialFolderPath(IntPtr hwndOwner, [Out] StringBuilder lpszPath, int nFolder, bool fCreate);
|
||||
const int CSIDL_LOCAL_APPDATA = 0x001c;
|
||||
|
||||
public List<Result> Query(Query query)
|
||||
{
|
||||
if (string.IsNullOrEmpty(query.RawQuery) || query.RawQuery.EndsWith(" ") || query.RawQuery.Length <= 1) return new List<Result>();
|
||||
|
||||
List<Bookmark> 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\": \"(?<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);
|
||||
|
||||
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++)
|
||||
{
|
||||
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; }
|
||||
}
|
||||
}
|
@ -11,6 +11,8 @@
|
||||
<AssemblyName>WinAlfred.Plugin.System</AssemblyName>
|
||||
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
|
||||
<RestorePackages>true</RestorePackages>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
@ -30,6 +32,9 @@
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="Newtonsoft.Json">
|
||||
<HintPath>..\packages\Newtonsoft.Json.5.0.8\lib\net35\Newtonsoft.Json.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Windows.Forms" />
|
||||
@ -39,6 +44,7 @@
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="BrowserBookmarks.cs" />
|
||||
<Compile Include="CMD.cs" />
|
||||
<Compile Include="Common\ChineseToPinYin.cs" />
|
||||
<Compile Include="DirectoryIndicator.cs" />
|
||||
@ -54,11 +60,15 @@
|
||||
<Name>WinAlfred.Plugin</Name>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="packages.config" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<PropertyGroup>
|
||||
<PostBuildEvent>
|
||||
</PostBuildEvent>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
<Target Name="BeforeBuild">
|
||||
|
4
WinAlfred.Plugin.System/packages.config
Normal file
4
WinAlfred.Plugin.System/packages.config
Normal file
@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="Newtonsoft.Json" version="5.0.8" targetFramework="net35" />
|
||||
</packages>
|
@ -36,5 +36,10 @@ namespace WinAlfred.Plugin
|
||||
Result r = (Result)obj;
|
||||
return r.Title == Title && r.SubTitle == SubTitle;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return Title + SubTitle;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,14 +1,37 @@
|
||||
using WinAlfred.Plugin;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.Serialization.Formatters.Binary;
|
||||
using System.Windows.Documents;
|
||||
using WinAlfred.Plugin;
|
||||
|
||||
namespace WinAlfred.Helper
|
||||
{
|
||||
public class SelectedRecords
|
||||
{
|
||||
private int hasAddedCount = 0;
|
||||
private Dictionary<string, int> dict = new Dictionary<string, int>();
|
||||
private string filePath = Directory.GetCurrentDirectory() + "\\selectedRecords.dat";
|
||||
|
||||
public void LoadSelectedRecords()
|
||||
{
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
|
||||
BinaryFormatter b = new BinaryFormatter();
|
||||
dict = (Dictionary<string, int>)b.Deserialize(fileStream);
|
||||
fileStream.Close();
|
||||
}
|
||||
|
||||
if (dict.Count > 1000)
|
||||
{
|
||||
List<string> onlyOnceKeys = (from c in dict where c.Value == 1 select c.Key).ToList();
|
||||
foreach (string onlyOnceKey in onlyOnceKeys)
|
||||
{
|
||||
dict.Remove(onlyOnceKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void AddSelect(Result result)
|
||||
@ -20,19 +43,31 @@ namespace WinAlfred.Helper
|
||||
hasAddedCount = 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
if (dict.ContainsKey(result.ToString()))
|
||||
{
|
||||
dict[result.ToString()] += 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
dict.Add(result.ToString(), 1);
|
||||
}
|
||||
}
|
||||
|
||||
public int GetSelectedCount(Result result)
|
||||
{
|
||||
if (dict.ContainsKey(result.ToString()))
|
||||
{
|
||||
return dict[result.ToString()];
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public void SaveSelectedRecords()
|
||||
private void SaveSelectedRecords()
|
||||
{
|
||||
|
||||
FileStream fileStream = new FileStream("selectedRecords.dat", FileMode.Create);
|
||||
BinaryFormatter b = new BinaryFormatter();
|
||||
b.Serialize(fileStream, dict);
|
||||
fileStream.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
BIN
WinAlfred/Images/bookmark.png
Normal file
BIN
WinAlfred/Images/bookmark.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 4.4 KiB |
Loading…
Reference in New Issue
Block a user