Skip to content

Commit

Permalink
[Feature] Automatically import bookmarks into Chrome/Edge browser
Browse files Browse the repository at this point in the history
  • Loading branch information
Physton committed Nov 26, 2023
1 parent ef50b4a commit 1706e54
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 23 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
/.env
/venv
/release
/.idea
/.idea
/bookmark.html
/bookmark.json
22 changes: 20 additions & 2 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@

- [x] Export to HTML bookmark file.
- [x] Export to JSON file.
- [ ] Automatically import bookmarks into Chrome browser.
- [x] Automatically import bookmarks into Chrome browser.
- [x] Automatically import bookmarks into Edge browser.
- [ ] Automatically import bookmarks into Firefox browser.
- [ ] Automatically import bookmarks into Safari browser.
- [ ] Automatically import bookmarks into Edge browser.

## Usage

Expand Down Expand Up @@ -62,6 +62,12 @@
| sidebar-file | ~/Library/Application Support/Arc/StorableSidebar.json | The path to the StorableSidebar.json file of Arc Browser. It is the file used to store the sidebar data of Arc Browser. In general, you don't need to specify this parameter. If not specified, the program will use the default path. | --sidebar-file="~/Library/Application Support/Arc/StorableSidebar.json" |
| save-json | | Save bookmarks to a JSON file. | --save-json=bookmark.json |
| save-html | | Save bookmarks to an HTML file. Can be imported into browsers. | --save-html=bookmark.html |
| import-to-chrome | | Import bookmarks into Chrome browser. | --import-to-chrome |
| chrome-bookmarks-file | ~/Library/Application Support/Google/Chrome/Default/Bookmarks | The path to the Bookmarks file of Chrome browser. It is the file used to store the bookmarks of Chrome browser. In general, you don't need to specify this parameter. If not specified, the program will use the default path. | --chrome-bookmarks-file="~/Library/Application Support/Google/Chrome/Default/Bookmarks" |
| chrome-import-node | bookmark_bar | The node to import bookmarks into the Chrome browser. The default is bookmark_bar. Can be imported into the bookmark_bar or other nodes. | --chrome-import-node=bookmark_bar |
| import-to-edge | | Import bookmarks into Edge browser. | --import-to-edge |
| edge-bookmarks-file | ~/Library/Application Support/Microsoft Edge/Default/Bookmarks | The path to the Bookmarks file of Edge browser. It is the file used to store the bookmarks of Edge browser. In general, you don't need to specify this parameter. If not specified, the program will use the default path. | --edge-bookmarks-file="~/Library/Application Support/Microsoft Edge/Default/Bookmarks" |
| edge-import-node | bookmark_bar | The node to import bookmarks into the Edge browser. The default is bookmark_bar. Can be imported into the bookmark_bar or other nodes. | --edge-import-node=bookmark_bar |
## Examples
Expand All @@ -79,3 +85,15 @@
```bash
./arc-bookmarks --save-html=~/Downloads/bookmark.html --save-json=~/Downloads/bookmark.json
```
- Import bookmarks into Chrome browser.
```bash
./arc-bookmarks --import-to-chrome
```
**After the import is successful, you need to restart the Chrome browser to see the imported bookmarks.**
- Import bookmarks into Chrome and Edge browsers.
```bash
./arc-bookmarks --import-to-chrome --import-to-edge
```
**After the import is successful, you need to restart the Chrome and Edge browsers to see the imported bookmarks.**
22 changes: 20 additions & 2 deletions README_CN.MD
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@

- [x] 导出到 html 书签文件。
- [x] 导出到 json 文件。
- [ ] 自动将书签导入到 Chrome 浏览器中。
- [x] 自动将书签导入到 Chrome 浏览器中。
- [x] 自动将书签导入到 Edge 浏览器中。
- [ ] 自动将书签导入到 Firefox 浏览器中。
- [ ] 自动将书签导入到 Safari 浏览器中。
- [ ] 自动将书签导入到 Edge 浏览器中。

## 使用

Expand Down Expand Up @@ -62,6 +62,12 @@
| sidebar-file | ~/Library/Application Support/Arc/StorableSidebar.json | Arc 浏览器的 StorableSidebar.json 文件路径。它是用于存储 Arc 浏览器的侧边栏数据的文件。 一般情况下,不需要指定该参数。如果不指定该参数,程序会使用默认的路径。 | --sidebar-file="~/Library/Application Support/Arc/StorableSidebar.json" |
| save-json | | 将书签保存到 json 文件中。 | --save-json=bookmark.json |
| save-html | | 将书签保存到 html 文件中。可用于导入到浏览器中。 | --save-html=bookmark.html |
| import-to-chrome | | 将书签导入到 Chrome 浏览器中。 | --import-to-chrome |
| chrome-bookmarks-file | ~/Library/Application Support/Google/Chrome/Default/Bookmarks | Chrome 浏览器的 Bookmarks 文件路径。它是用于存储 Chrome 浏览器的书签的文件。 一般情况下,不需要指定该参数。如果不指定该参数,程序会使用默认的路径。 | --chrome-bookmarks-file="~/Library/Application Support/Google/Chrome/Default/Bookmarks" |
| chrome-import-node | bookmark_bar | 将书签导入到 Chrome 浏览器中的节点。默认为 bookmark_bar。可以导入到 bookmark_bar 或 other 节点中。 | --chrome-import-node=bookmark_bar |
| import-to-edge | | 将书签导入到 Edge 浏览器中。 | --import-to-edge |
| edge-bookmarks-file | ~/Library/Application Support/Microsoft Edge/Default/Bookmarks | Edge 浏览器的 Bookmarks 文件路径。它是用于存储 Edge 浏览器的书签的文件。 一般情况下,不需要指定该参数。如果不指定该参数,程序会使用默认的路径。 | --edge-bookmarks-file="~/Library/Application Support/Microsoft Edge/Default/Bookmarks" |
| edge-import-node | bookmark_bar | 将书签导入到 Edge 浏览器中的节点。默认为 bookmark_bar。可以导入到 bookmark_bar 或 other 节点中。 | --edge-import-node=bookmark_bar |

## 示例

Expand All @@ -79,3 +85,15 @@
```bash
./arc-bookmarks --save-html=~/Downloads/bookmark.html --save-json=~/Downloads/bookmark.json
```

- 将书签导入到 Chrome 浏览器中。
```bash
./arc-bookmarks --import-to-chrome
```
**导入成功后,需要重启 Chrome 浏览器才能看到导入的书签。**

- 将书签导入到 Chrome 和 Edge 浏览器中。
```bash
./arc-bookmarks --import-to-chrome --import-to-edge
```
**导入成功后,需要重启 Chrome 和 Edge 浏览器才能看到导入的书签。**
150 changes: 132 additions & 18 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import os
import json
import sys
import time
import requests

VERSION = 'v0.0.2'
VERSION = 'v0.0.3'
REPO = 'Physton/arc-bookmarks'
GITHUB = f'https://github.com/{REPO}'

Expand Down Expand Up @@ -172,6 +173,41 @@ def _bookmarks2html(bookmarks):
html += '</DL><p>\n'
return html

def bookmarks2chrome(bookmarks):
chromeBookmarks = {
'children': [],
'date_added': '0',
'date_last_used': '0',
'date_modified': '0',
'guid': '',
'id': '1',
'name': 'Bookmarks bar',
'type': 'folder',
}
def _bookmarks2chrome(bookmarks, id = 0):
chromeBookmarks = []
for bookmark in bookmarks:
id += 1
chromeBookmarkItem = {
'date_added': '0',
'date_last_used': '0',
'date_modified': '0',
'guid': bookmark['id'],
'id': id,
'name': bookmark['title'],
'type': 'folder',
}
if bookmark['children'] and len(bookmark['children']) > 0:
chromeBookmarkItem['children'] = _bookmarks2chrome(bookmark['children'], id)
chromeBookmarkItem['type'] = 'folder'
else:
chromeBookmarkItem['type'] = 'url'
chromeBookmarkItem['url'] = bookmark['url']
chromeBookmarks.append(chromeBookmarkItem)
return chromeBookmarks
chromeBookmarks['children'] = _bookmarks2chrome(bookmarks, 1)
return chromeBookmarks

def check_version():
try:
response = requests.get(f'https://api.github.com/repos/{REPO}/releases/latest', timeout=5)
Expand All @@ -188,27 +224,13 @@ def print_help():
commander = os.path.basename(sys.executable)

'''
--import-to-chrome
Import bookmarks into Chrome browser.
将书签导入到 Chrome 浏览器中。
--import-to-firefox
Import bookmarks into Firefox browser.
将书签导入到 Firefox 浏览器中。
--import-to-safari
Import bookmarks into Safari browser.
将书签导入到 Safari 浏览器中。
--import-to-edge
Import bookmarks into Edge browser.
将书签导入到 Edge 浏览器中。
{commander} --import-to-chrome
{commander} --import-to-firefox
{commander} --import-to-safari
{commander} --import-to-edge
{commander} --save-html=bookmark.html --import-to-chrome --import-to-safari --import-to-edge
'''

print(f'''Use: {commander} [options]
Expand All @@ -227,11 +249,45 @@ def print_help():
Save bookmarks to html file. Can be used to import into the browser.
将书签保存到 html 文件中。可用于导入到浏览器中。
--import-to-chrome
Import bookmarks into Chrome browser.
将书签导入到 Chrome 浏览器中。
--chrome-bookmarks-file="~/Library/Application Support/Google/Chrome/Default/Bookmarks"
The path of Bookmarks file of Chrome browser. It is a file used to store the bookmarks of Chrome browser.
In general, you don't need to specify this parameter. If you don't specify this parameter, the program will use the default path.
Chrome 浏览器的 Bookmarks 文件路径。它是用于存储 Chrome 浏览器的书签的文件。
一般情况下,不需要指定该参数。如果不指定该参数,程序会使用默认的路径。
--chrome-import-node=bookmark_bar
The node where the bookmarks are imported into Chrome browser. The default is bookmark_bar.
Can be imported into bookmark_bar or other nodes.
将书签导入到 Chrome 浏览器中的节点。默认为 bookmark_bar。
可以导入到 bookmark_bar 或 other 节点中。
--import-to-edge
Import bookmarks into Edge browser.
将书签导入到 Edge 浏览器中。
--edge-bookmarks-file="~/Library/Application Support/Microsoft Edge/Default/Bookmarks"
The path of Favorites file of Edge browser. It is a file used to store the bookmarks of Edge browser.
In general, you don't need to specify this parameter. If you don't specify this parameter, the program will use the default path.
Edge 浏览器的 Favorites 文件路径。它是用于存储 Edge 浏览器的书签的文件。
一般情况下,不需要指定该参数。如果不指定该参数,程序会使用默认的路径。
--edge-import-node=bookmark_bar
The node where the bookmarks are imported into Edge browser. The default is bookmark_bar.
Can be imported into bookmark_bar or other nodes.
将书签导入到 Edge 浏览器中的节点。默认为 bookmark_bar。
可以导入到 bookmark_bar 或 other 节点中。
Examples:
{commander} --save-json=bookmark.json
{commander} --save-html=bookmark.html
{commander} --save-json=bookmark.json --save-html=bookmark.html
{commander} --sidebar-file="~/Library/Application Support/Arc/StorableSidebar.json" --save-json=bookmark.json --save-html=bookmark.html
{commander} --import-to-chrome
{commander} --import-to-chrome --import-to-edge
''')

if __name__ == '__main__':
Expand All @@ -253,9 +309,13 @@ def print_help():
saveHtml = False
saveHtmlFile = 'bookmark.html'
importToChrome = False
importChromeNode = 'bookmark_bar' # bookmark_bar, other
chromeBookmarksFile = '~/Library/Application Support/Google/Chrome/Default/Bookmarks'
importToEdge = False
importEdgeNode = 'bookmark_bar' # bookmark_bar, other
edgeBookmarksFile = '~/Library/Application Support/Microsoft Edge/Default/Bookmarks'
importToFirefox = False
importToSafari = False
importToEdge = False
for argument in arguments:
if argument.startswith('--sidebar-file='):
SidebarFile = argument.replace('--sidebar-file=', '')
Expand All @@ -274,12 +334,28 @@ def print_help():
saveHtmlFile = os.path.expanduser(saveHtmlFile)
elif argument == '--import-to-chrome':
importToChrome = True
elif argument.startswith('--chrome-bookmarks-file='):
chromeBookmarksFile = argument.replace('--chrome-bookmarks-file=', '')
chromeBookmarksFile = os.path.expanduser(chromeBookmarksFile)
elif argument.startswith('--chrome-import-node='):
importChromeNode = argument.replace('--chrome-import-node=', '')
if importChromeNode not in ['bookmark_bar', 'other']:
print_error(f'Unknown chrome import node: {importChromeNode}')
sys.exit()
elif argument == '--import-to-edge':
importToEdge = True
elif argument.startswith('--edge-bookmarks-file='):
edgeBookmarksFile = argument.replace('--edge-bookmarks-file=', '')
edgeBookmarksFile = os.path.expanduser(edgeBookmarksFile)
elif argument.startswith('--edge-import-node='):
importEdgeNode = argument.replace('--edge-import-node=', '')
if importEdgeNode not in ['bookmark_bar', 'other']:
print_error(f'Unknown edge import node: {importEdgeNode}')
sys.exit()
elif argument == '--import-to-firefox':
importToFirefox = True
elif argument == '--import-to-safari':
importToSafari = True
elif argument == '--import-to-edge':
importToEdge = True
elif argument == '--help':
print_help()
sys.exit()
Expand All @@ -306,4 +382,42 @@ def print_help():
except Exception as e:
print_error(f'Save html file failed: {e}')

if importToChrome:
chromeBookmarksFile = os.path.expanduser(chromeBookmarksFile)
if not os.path.exists(chromeBookmarksFile):
print_error(f'Chrome bookmarks file not exists: {chromeBookmarksFile}')
try:
chromeBookmarks = bookmarks2chrome(bookmarks)
with open(chromeBookmarksFile, 'r', encoding='utf-8') as f:
chromeBookmarksJson = json.load(f)
if importChromeNode == 'bookmark_bar':
chromeBookmarksJson['roots']['bookmark_bar']['children'] = chromeBookmarks['children']
else:
chromeBookmarksJson['roots']['other']['children'] = chromeBookmarks['children']
with open(chromeBookmarksFile, 'w', encoding='utf-8') as f:
json.dump(chromeBookmarksJson, f, indent=4, ensure_ascii=False)
print_success(f'Import bookmarks to Chrome success: {chromeBookmarksFile}')
print_success('Please restart Chrome to take effect.')
except Exception as e:
print_error(f'Import bookmarks to Chrome failed: {e}')

if importToEdge:
edgeBookmarksFile = os.path.expanduser(edgeBookmarksFile)
if not os.path.exists(edgeBookmarksFile):
print_error(f'Edge bookmarks file not exists: {edgeBookmarksFile}')
try:
chromeBookmarks = bookmarks2chrome(bookmarks)
with open(edgeBookmarksFile, 'r', encoding='utf-8') as f:
edgeBookmarksJson = json.load(f)
if importEdgeNode == 'bookmark_bar':
edgeBookmarksJson['roots']['bookmark_bar']['children'] = chromeBookmarks['children']
else:
edgeBookmarksJson['roots']['other']['children'] = chromeBookmarks['children']
with open(edgeBookmarksFile, 'w', encoding='utf-8') as f:
json.dump(edgeBookmarksJson, f, indent=4, ensure_ascii=False)
print_success(f'Import bookmarks to Edge success: {edgeBookmarksFile}')
print_success('Please restart Edge to take effect.')
except Exception as e:
print_error(f'Import bookmarks to Edge failed: {e}')

sys.exit()

0 comments on commit 1706e54

Please sign in to comment.