Introduction
Chrome as a new browser has the built-in module to import Firefox and Internet Explorer bookmarks. But Internet Explorer has no such module to do the same thing. Today, I want to fix this problem and export Chrome bookmarks to IE Favorites.
Background
Before showing my code, I do think it's necessary to tell you some background about Internet Explorer Favorites and Chrome bookmarks (JSON).
Internet Explorer Favorites
Typically, your Internet Explorer Favorites are under: C:\Users\UserName\Favorites. And each URL is a file; for example: Google.url is the Google IE bookmark.
And the typical structure of an IE URL file is as follows:
[DEFAULT]
BASEURL=http://www.google.com/
[{000214A0-0000-0000-C000-000000000046}]
Prop3=19,2
[InternetShortcut]
URL=http://www.google.com/
IDList=
IconFile=http://www.google.com/favicon.ico
IconIndex=1
Do you find something there? Yes, it's an ini file. In fact, only these two lines:
[InternetShortcut]
URL=http://www.google.com/
will work, you can try. At this point, we can process IE Favorites; we can read and write an ini file with the help of the GetPrivateProfileString
and WritePrivateProfileString
APIs.
Google Chrome Bookmarks
Typically, Google Chrome bookmarks are stored at C:\Users\UserName\AppData\Local\Google\Chrome\User Data\Default\Bookmarks.
Let's look at the Chrome bookmarks structure. The content of a bookmark file is as follows:
{
"checksum": "1ddf61ad79f79c33b6674fc8ccb03ca5",
"roots": {
"bookmark_bar": {
"children": [ {
"date_added": "12915767966508377",
"id": "3",
"name": "Google",
"type": "url",
"url": "http://www.google.com/"
} ],
"date_added": "0",
"date_modified": "12915767966508377",
"id": "1",
"name": "Bookmarks bar",
"type": "folder"
},
"other": {
"children": [ ],
"date_added": "0",
"date_modified": "12915566290018721",
"id": "2",
"name": "Other bookmarks",
"type": "folder"
}
},
"version": 1
}
Even though there is only a single URL inside the file, it's hard to read. We refine it as follows:
{
"checksum": "1ddf61ad79f79c33b6674fc8ccb03ca5",
"roots": {
"bookmark_bar": FOLDER_OBJ,
"other": FOLDER_OBJ
},
"version": 1
}
while the structure of FOLDER_OBJ
is:
{
"children": [ FOLDER_OBJ, URL_OBJ,...],
"date_added": "0",
"date_modified": "12915566290018721",
"id": "2",
"name": "Other bookmarks",
"type": "folder"
}
We can see from the above snippet, FOLDER_OBJ
can hold URL_OBJ
, and also FOLDER_OBJ
. Similar to FOLDER_OBJ
, the structure of URL_OBJ
is:
{
"date_added": "12915767966508377",
"id": "3",
"name": "Google",
"type": "url",
"url": "http://www.google.com/"
}
In fact, the Chrome bookmarks file is a JSON file, which is short for "JavaScript Object Notation". You can get more information from here. There are a lot of libraries to parse JSON, you can get the list from here. After comparing, we finally choose JsonCpp to do this work. At the same time, you can get a copy from my demo project. With the help of the JsonCpp library, it's very easy to parse the Google Chrome bookmarks file.
Implementation Details
Wrapper Class
We write a wrapper chrome
class to export it, with only a few functions. It's obviously easy to understand it, so I will just post the code here.
class chrome
{
public:
chrome() {};
bool load(const wchar_t * file_path);
bool export_2_folder(const wchar_t* folder_path);
static bool is_installed_chrome(wchar_t * path);
static bool is_chrome_running();
static bool close_chrome();
private:
void export_url(Json::Value& url_obj, const wchar_t* path);
void export_folder(Json::Value& folder_obj, const wchar_t* path);
Json::Value chrome_;
};
How To Use
It's very easy to use this code, only three steps will do:
chrome obj;
obj.load(bkmk);
obj.export_2_folder(path);
If you have not installed Chrome in your OS, a Google Chrome Bookmarks file is attached in the demo project. You can give the load
function an absolute file path of the bookmarks file attached.
Others
Unicode support is very important. So I wrote two functions to make it available to other languages.
bool convert_utf8_2_wchar(const char* utf8, wchar_t * wchar_out);
size_t convert_wchar_2_utf8(const wchar_t* wchar_in, char * utf8);
By the way, this article only provides a way to export Chrome bookmarks to IE Favorites. But based on our demo project, it's very easy to import IE Favorites to Chrome bookmarks and even make bookmarks interchanged among IE, Chrome, and Firefox.
The screenshot of the result after running the demo project is:
