mirror of
https://github.com/microsoft/PowerToys.git
synced 2024-12-13 02:39:22 +08:00
add search for Firefox bookmarks
This commit is contained in:
parent
e275ce6063
commit
6807dcbd5d
108
Plugins/Wox.Plugin.BrowserBookmark/ChromeBookmarks.cs
Normal file
108
Plugins/Wox.Plugin.BrowserBookmark/ChromeBookmarks.cs
Normal file
@ -0,0 +1,108 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using Wox.Infrastructure;
|
||||
|
||||
namespace Wox.Plugin.BrowserBookmark
|
||||
{
|
||||
public class ChromeBookmarks
|
||||
{
|
||||
private List<Bookmark> bookmarks = new List<Bookmark>();
|
||||
|
||||
public ChromeBookmarks()
|
||||
{
|
||||
bookmarks.Clear();
|
||||
LoadChromeBookmarks();
|
||||
|
||||
bookmarks = bookmarks.Distinct().ToList();
|
||||
}
|
||||
|
||||
public List<Bookmark> GetBookmarks(string search = null)
|
||||
{
|
||||
if (string.IsNullOrEmpty(search)) return bookmarks;
|
||||
|
||||
var fuzzyMatcher = FuzzyMatcher.Create(search);
|
||||
var returnList = bookmarks.Where(o => MatchProgram(o, fuzzyMatcher)).ToList();
|
||||
|
||||
return returnList;
|
||||
}
|
||||
|
||||
private bool MatchProgram(Bookmark bookmark, FuzzyMatcher matcher)
|
||||
{
|
||||
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;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private void ParseChromeBookmarks(String path, string source)
|
||||
{
|
||||
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);
|
||||
|
||||
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++;
|
||||
|
||||
if (url == null) continue;
|
||||
if (url.StartsWith("javascript:", StringComparison.OrdinalIgnoreCase)) continue;
|
||||
if (url.StartsWith("vbscript:", StringComparison.OrdinalIgnoreCase)) continue;
|
||||
|
||||
bookmarks.Add(new Bookmark()
|
||||
{
|
||||
Name = name,
|
||||
Url = url,
|
||||
Source = source
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
89
Plugins/Wox.Plugin.BrowserBookmark/FirefoxBookmarks.cs
Normal file
89
Plugins/Wox.Plugin.BrowserBookmark/FirefoxBookmarks.cs
Normal file
@ -0,0 +1,89 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SQLite;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
||||
namespace Wox.Plugin.BrowserBookmark
|
||||
{
|
||||
public class FirefoxBookmarks
|
||||
{
|
||||
private const string queryBookmarks = @"SELECT url, title
|
||||
FROM moz_places
|
||||
WHERE id in (
|
||||
SELECT bm.fk FROM moz_bookmarks bm WHERE bm.fk NOT NULL
|
||||
)
|
||||
AND ( url LIKE '%{0}%' OR title LIKE '%{0}%' )
|
||||
ORDER BY visit_count DESC
|
||||
LIMIT 20
|
||||
";
|
||||
|
||||
private const string queryTopBookmarks = @"SELECT url, title
|
||||
FROM moz_places
|
||||
WHERE id in (
|
||||
SELECT bm.fk FROM moz_bookmarks bm WHERE bm.fk NOT NULL
|
||||
)
|
||||
ORDER BY visit_count DESC
|
||||
LIMIT 20
|
||||
";
|
||||
|
||||
private const string dbPathFormat = "Data Source ={0};Version=3;New=False;Compress=True;";
|
||||
|
||||
public List<Bookmark> GetBookmarks(string search = null, bool top = false)
|
||||
{
|
||||
// Create the query command for the given case
|
||||
string query = top ? queryTopBookmarks : string.Format(queryBookmarks, search);
|
||||
|
||||
return GetResults(query);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Searches the places.sqlite db based on the given query and returns the results
|
||||
/// </summary>
|
||||
private List<Bookmark> GetResults(string query)
|
||||
{
|
||||
// create the connection string and init the connection
|
||||
string dbPath = string.Format(dbPathFormat, PlacesPath);
|
||||
var dbConnection = new SQLiteConnection(dbPath);
|
||||
|
||||
// Open connection to the database file and execute the query
|
||||
dbConnection.Open();
|
||||
var reader = new SQLiteCommand(query, dbConnection).ExecuteReader();
|
||||
|
||||
// return results in List<Bookmark> format
|
||||
return reader.Select(x => new Bookmark()
|
||||
{
|
||||
Name = (x["title"] is DBNull) ? string.Empty : x["title"].ToString(),
|
||||
Url = x["url"].ToString()
|
||||
}).ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Path to places.sqlite
|
||||
/// </summary>
|
||||
private string PlacesPath
|
||||
{
|
||||
get
|
||||
{
|
||||
var profilesPath = Environment.ExpandEnvironmentVariables(@"%appdata%\Mozilla\Firefox\Profiles\");
|
||||
var folders = new DirectoryInfo(profilesPath).GetDirectories().Select(x => x.FullName).ToList();
|
||||
|
||||
// Look for the default profile folder
|
||||
return string.Format(@"{0}\places.sqlite",
|
||||
folders.FirstOrDefault(d => File.Exists(d + @"\places.sqlite") && d.EndsWith(".default"))
|
||||
?? folders.First(d => File.Exists(d + @"\places.sqlite")));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class Extensions
|
||||
{
|
||||
public static IEnumerable<T> Select<T>(this SQLiteDataReader reader, Func<SQLiteDataReader, T> projection)
|
||||
{
|
||||
while (reader.Read())
|
||||
{
|
||||
yield return projection(reader);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,9 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using Wox.Infrastructure;
|
||||
|
||||
namespace Wox.Plugin.BrowserBookmark
|
||||
@ -11,40 +7,37 @@ namespace Wox.Plugin.BrowserBookmark
|
||||
public class Main : IPlugin
|
||||
{
|
||||
private PluginInitContext context;
|
||||
private List<Bookmark> bookmarks = new List<Bookmark>();
|
||||
|
||||
private ChromeBookmarks chromeBookmarks = new ChromeBookmarks();
|
||||
private FirefoxBookmarks mozBookmarks = new FirefoxBookmarks();
|
||||
|
||||
public void Init(PluginInitContext context)
|
||||
{
|
||||
bookmarks.Clear();
|
||||
LoadChromeBookmarks();
|
||||
|
||||
bookmarks = bookmarks.Distinct().ToList();
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
public List<Result> Query(Query query)
|
||||
{
|
||||
if (query.ActionParameters.Count == 0)
|
||||
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 = new List<Bookmark>();
|
||||
|
||||
// Add Firefox bookmarks
|
||||
returnList.AddRange(mozBookmarks.GetBookmarks(param, topResults));
|
||||
// Add Chrome bookmarks
|
||||
returnList.AddRange(chromeBookmarks.GetBookmarks(param));
|
||||
|
||||
if (!topResults)
|
||||
{
|
||||
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();
|
||||
// Since we mixed chrome and firefox bookmarks, we should order them again
|
||||
var fuzzyMatcher = FuzzyMatcher.Create(param);
|
||||
returnList = returnList.Where(o => MatchProgram(o, fuzzyMatcher)).ToList();
|
||||
returnList = returnList.OrderByDescending(o => o.Score).ToList();
|
||||
}
|
||||
|
||||
|
||||
var fuzzyMather = FuzzyMatcher.Create(query.GetAllRemainingParameter());
|
||||
List<Bookmark> returnList = bookmarks.Where(o => MatchProgram(o, fuzzyMather)).ToList();
|
||||
returnList = returnList.OrderByDescending(o => o.Score).ToList();
|
||||
|
||||
return returnList.Select(c => new Result()
|
||||
{
|
||||
Title = c.Name,
|
||||
@ -68,87 +61,5 @@ namespace Wox.Plugin.BrowserBookmark
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private void ParseChromeBookmarks(String path, string source)
|
||||
{
|
||||
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);
|
||||
|
||||
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++;
|
||||
|
||||
if (url == null) continue;
|
||||
if (url.StartsWith("javascript:", StringComparison.OrdinalIgnoreCase)) continue;
|
||||
if (url.StartsWith("vbscript:", StringComparison.OrdinalIgnoreCase)) continue;
|
||||
|
||||
bookmarks.Add(new Bookmark()
|
||||
{
|
||||
Name = name,
|
||||
Url = url,
|
||||
Source = source
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
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 "Bookmarks"; }
|
||||
}
|
||||
|
||||
public string IcoPath
|
||||
{
|
||||
get { return @"Images\bookmark.png"; }
|
||||
}
|
||||
|
||||
public string Description
|
||||
{
|
||||
get { return "System workflow"; }
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -11,6 +11,8 @@
|
||||
<AssemblyName>Wox.Plugin.BrowserBookmark</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>
|
||||
@ -34,6 +36,14 @@
|
||||
<Reference Include="PresentationFramework" />
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Data.SQLite, Version=1.0.93.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139, processorArchitecture=x86">
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\..\packages\System.Data.SQLite.Core.1.0.93.0\lib\net20\System.Data.SQLite.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System.Data.SQLite.Linq, Version=1.0.93.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139, processorArchitecture=MSIL">
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\..\packages\System.Data.SQLite.Linq.1.0.93.0\lib\net20\System.Data.SQLite.Linq.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
<Reference Include="System.Data" />
|
||||
@ -42,6 +52,8 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Bookmark.cs" />
|
||||
<Compile Include="ChromeBookmarks.cs" />
|
||||
<Compile Include="FirefoxBookmarks.cs" />
|
||||
<Compile Include="Main.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
@ -56,6 +68,8 @@
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="app.config" />
|
||||
<None Include="packages.config" />
|
||||
<None Include="plugin.json">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
@ -64,8 +78,15 @@
|
||||
<Content Include="Images\bookmark.png">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<Content Include="x64\SQLite.Interop.dll">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<Content Include="x86\SQLite.Interop.dll">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<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
Plugins/Wox.Plugin.BrowserBookmark/app.config
Normal file
4
Plugins/Wox.Plugin.BrowserBookmark/app.config
Normal file
@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<configuration>
|
||||
|
||||
<system.data><DbProviderFactories><remove invariant="System.Data.SQLite" /><add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".Net Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" /></DbProviderFactories></system.data></configuration>
|
6
Plugins/Wox.Plugin.BrowserBookmark/packages.config
Normal file
6
Plugins/Wox.Plugin.BrowserBookmark/packages.config
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="System.Data.SQLite" version="1.0.93.0" targetFramework="net35" />
|
||||
<package id="System.Data.SQLite.Core" version="1.0.93.0" targetFramework="net35" />
|
||||
<package id="System.Data.SQLite.Linq" version="1.0.93.0" targetFramework="net35" />
|
||||
</packages>
|
BIN
Plugins/Wox.Plugin.BrowserBookmark/x64/SQLite.Interop.dll
Normal file
BIN
Plugins/Wox.Plugin.BrowserBookmark/x64/SQLite.Interop.dll
Normal file
Binary file not shown.
BIN
Plugins/Wox.Plugin.BrowserBookmark/x86/SQLite.Interop.dll
Normal file
BIN
Plugins/Wox.Plugin.BrowserBookmark/x86/SQLite.Interop.dll
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user