1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . IO ;
4+ using System . Linq ;
5+ using System . Text . RegularExpressions ;
6+
7+ namespace Flow . Launcher . Plugin . BrowserBookmark
8+ {
9+ public class EdgeBookmarks
10+ {
11+ private List < Bookmark > bookmarks = new List < Bookmark > ( ) ;
12+
13+ public List < Bookmark > GetBookmarks ( )
14+ {
15+ bookmarks . Clear ( ) ;
16+ LoadEdgeBookmarks ( ) ;
17+
18+ return bookmarks ;
19+ }
20+
21+ private void ParseEdgeBookmarks ( String path , string source )
22+ {
23+ if ( ! File . Exists ( path ) ) return ;
24+
25+ string all = File . ReadAllText ( path ) ;
26+ Regex nameRegex = new Regex ( "\" name\" : \" (?<name>.*?)\" " ) ;
27+ MatchCollection nameCollection = nameRegex . Matches ( all ) ;
28+ Regex typeRegex = new Regex ( "\" type\" : \" (?<type>.*?)\" " ) ;
29+ MatchCollection typeCollection = typeRegex . Matches ( all ) ;
30+ Regex urlRegex = new Regex ( "\" url\" : \" (?<url>.*?)\" " ) ;
31+ MatchCollection urlCollection = urlRegex . Matches ( all ) ;
32+
33+ List < string > names = ( from Match match in nameCollection select match . Groups [ "name" ] . Value ) . ToList ( ) ;
34+ List < string > types = ( from Match match in typeCollection select match . Groups [ "type" ] . Value ) . ToList ( ) ;
35+ List < string > urls = ( from Match match in urlCollection select match . Groups [ "url" ] . Value ) . ToList ( ) ;
36+
37+ int urlIndex = 0 ;
38+ for ( int i = 0 ; i < names . Count ; i ++ )
39+ {
40+ string name = DecodeUnicode ( names [ i ] ) ;
41+ string type = types [ i ] ;
42+ if ( type == "url" )
43+ {
44+ string url = urls [ urlIndex ] ;
45+ urlIndex ++ ;
46+
47+ if ( url == null ) continue ;
48+ if ( url . StartsWith ( "javascript:" , StringComparison . OrdinalIgnoreCase ) ) continue ;
49+ if ( url . StartsWith ( "vbscript:" , StringComparison . OrdinalIgnoreCase ) ) continue ;
50+
51+ bookmarks . Add ( new Bookmark ( )
52+ {
53+ Name = name ,
54+ Url = url ,
55+ Source = source
56+ } ) ;
57+ }
58+ }
59+ }
60+
61+ private void LoadEdgeBookmarks ( string path , string name )
62+ {
63+ if ( ! Directory . Exists ( path ) ) return ;
64+ var paths = Directory . GetDirectories ( path ) ;
65+
66+ foreach ( var profile in paths )
67+ {
68+ if ( File . Exists ( Path . Combine ( profile , "Bookmarks" ) ) )
69+ ParseEdgeBookmarks ( Path . Combine ( profile , "Bookmarks" ) , name + ( Path . GetFileName ( profile ) == "Default" ? "" : ( " (" + Path . GetFileName ( profile ) + ")" ) ) ) ;
70+ }
71+ }
72+
73+ private void LoadEdgeBookmarks ( )
74+ {
75+ String platformPath = Environment . GetFolderPath ( Environment . SpecialFolder . LocalApplicationData ) ;
76+ LoadEdgeBookmarks ( Path . Combine ( platformPath , @"Microsoft\Edge\User Data" ) , "Microsoft Edge" ) ;
77+ LoadEdgeBookmarks ( Path . Combine ( platformPath , @"Microsoft\Edge SxS\User Data" ) , "Microsoft Edge Canary" ) ;
78+ }
79+
80+ private String DecodeUnicode ( String dataStr )
81+ {
82+ Regex reg = new Regex ( @"(?i)\\[uU]([0-9a-f]{4})" ) ;
83+ return reg . Replace ( dataStr , m => ( ( char ) Convert . ToInt32 ( m . Groups [ 1 ] . Value , 16 ) ) . ToString ( ) ) ;
84+ }
85+ }
86+ }
0 commit comments