Merge pull request #99 from JohnTheGr8/master

Firefox bookmarks
This commit is contained in:
qianlifeng 2014-07-03 21:11:16 +08:00
commit d0d9de8583
9 changed files with 263 additions and 114 deletions

View File

@ -0,0 +1,109 @@
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)
{
//TODO: Maybe load bookmarks here instead of pre-loading them at startup?
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());
}
}
}

View File

@ -0,0 +1,97 @@
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)
{
// Return empty list if the places.sqlite file cannot be found
if (string.IsNullOrEmpty(PlacesPath) || !File.Exists(PlacesPath))
return new List<Bookmark>();
// 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\");
// return null if the Profiles folder does not exist
if (!Directory.Exists(profilesPath)) return null;
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);
}
}
}
}

View File

@ -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,38 @@ 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>();
//TODO: Let the user select which browser's bookmarks are displayed
// 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 +62,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"; }
}
}
}

View File

@ -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">

View 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>

View 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>

View File

@ -1,10 +1,10 @@
{
"ID":"0ECADE17459B49F587BF81DC3A125110",
"ActionKeyword":"b",
"Name":"Browser Bookmark",
"Name":"Browser Bookmarks",
"Description":"Search your browser bookmarks",
"Author":"qianlifeng",
"Version":"1.0",
"Author":"qianlifeng, Ioannis G.",
"Version":"1.1",
"Language":"csharp",
"Website":"http://www.getwox.com/plugin",
"ExecuteFileName":"Wox.Plugin.browserBookmark.dll",