forked from brave/browser-laptop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimporter.js
210 lines (191 loc) · 6 KB
/
importer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
'strict mode'
const electron = require('electron')
const importer = electron.importer
const dialog = electron.dialog
const BrowserWindow = electron.BrowserWindow
const session = electron.session
const Immutable = require('immutable')
const { showImportWarning, showImportSuccess } = require('./aboutDialog')
const siteUtil = require('../js/state/siteUtil')
const AppStore = require('../js/stores/appStore')
const siteTags = require('../js/constants/siteTags')
const appActions = require('../js/actions/appActions')
const messages = require('../js/constants/messages')
var isMergeFavorites = false
exports.init = () => {
importer.initialize()
}
exports.importData = (selected) => {
if (selected.get('mergeFavorites')) {
isMergeFavorites = true
}
if (selected !== undefined) {
importer.importData(selected.toJS())
}
}
exports.importHTML = (selected) => {
if (selected.get('mergeFavorites')) {
isMergeFavorites = true
}
const files = dialog.showOpenDialog({
properties: ['openFile'],
filters: [{
name: 'HTML',
extensions: ['html', 'htm']
}]
})
if (files && files.length > 0) {
const file = files[0]
importer.importHTML(file)
}
}
importer.on('update-supported-browsers', (e, detail) => {
isMergeFavorites = false
if (BrowserWindow.getFocusedWindow()) {
BrowserWindow.getFocusedWindow().webContents.send(messages.IMPORTER_LIST, detail)
}
})
importer.on('add-password-form', (e, detail) => {
})
importer.on('add-history-page', (e, history, visitSource) => {
let sites = []
for (let i = 0; i < history.length; ++i) {
const site = {
title: history[i].title,
location: history[i].url,
lastAccessedTime: history[i].last_visit * 1000
}
sites.push(site)
}
appActions.addSite(Immutable.fromJS(sites))
})
importer.on('add-homepage', (e, detail) => {
})
const getParentFolderId = (path, pathMap, sites, topLevelFolderId, nextFolderIdObject) => {
const pathLen = path.length
if (!pathLen) {
return topLevelFolderId
}
const parentFolder = path.pop()
let parentFolderId = pathMap[parentFolder]
if (parentFolderId === undefined) {
parentFolderId = nextFolderIdObject.id++
pathMap[parentFolder] = parentFolderId
const folder = {
customTitle: parentFolder,
folderId: parentFolderId,
parentFolderId: getParentFolderId(path, pathMap, sites, topLevelFolderId, nextFolderIdObject),
lastAccessedTime: (new Date()).getTime(),
tags: [siteTags.BOOKMARK_FOLDER]
}
sites.push(folder)
}
return parentFolderId
}
importer.on('add-bookmarks', (e, bookmarks, topLevelFolder) => {
let nextFolderId = siteUtil.getNextFolderId(AppStore.getState().get('sites'))
let nextFolderIdObject = { id: nextFolderId }
let pathMap = {}
let sites = []
let topLevelFolderId = 0
if (!isMergeFavorites) {
topLevelFolderId = nextFolderIdObject.id++
sites.push({
customTitle: topLevelFolder,
folderId: topLevelFolderId,
parentFolderId: 0,
lastAccessedTime: (new Date()).getTime(),
tags: [siteTags.BOOKMARK_FOLDER]
})
} else {
// Merge into existing bookmark toolbar
pathMap[topLevelFolder] = topLevelFolderId
pathMap['Bookmarks Toolbar'] = 0 // Firefox
pathMap['Bookmarks Bar'] = 0 // Chrome on mac
pathMap['Other Bookmarks'] = -1 // Chrome on mac
pathMap['Bookmarks bar'] = 0 // Chrome on win/linux
pathMap['Other bookmarks'] = -1 // Chrome on win/linux
pathMap['Bookmark Bar'] = 0 // Safari
pathMap['Links'] = 0 // Edge, IE
}
for (let i = 0; i < bookmarks.length; ++i) {
let path = bookmarks[i].path
let parentFolderId = getParentFolderId(path, pathMap, sites, topLevelFolderId, nextFolderIdObject)
if (bookmarks[i].is_folder) {
const folderId = nextFolderIdObject.id++
pathMap[bookmarks[i].title] = folderId
const folder = {
customTitle: bookmarks[i].title,
folderId: folderId,
parentFolderId: parentFolderId,
lastAccessedTime: bookmarks[i].creation_time * 1000,
tags: [siteTags.BOOKMARK_FOLDER]
}
sites.push(folder)
} else {
const site = {
title: bookmarks[i].title,
location: bookmarks[i].url,
parentFolderId: parentFolderId,
lastAccessedTime: bookmarks[i].creation_time * 1000,
tags: [siteTags.BOOKMARK]
}
sites.push(site)
}
}
appActions.addSite(Immutable.fromJS(sites))
})
importer.on('add-favicons', (e, detail) => {
let faviconMap = {}
detail.forEach((entry) => {
if (entry.favicon_url.startsWith('made-up-favicon:')) {
faviconMap[entry.urls[0]] = entry.png_data
} else {
faviconMap[entry.urls[0]] = entry.favicon_url
}
})
let sites = AppStore.getState().get('sites')
sites = sites.map((site) => {
if (site.get('favicon') === undefined && site.get('location') !== undefined &&
faviconMap[site.get('location')] !== undefined) {
return site.set('favicon', faviconMap[site.get('location')])
} else {
return site
}
})
appActions.addSite(sites)
})
importer.on('add-keywords', (e, templateUrls, uniqueOnHostAndPath) => {
})
importer.on('add-autofill-form-data-entries', (e, detail) => {
})
importer.on('add-cookies', (e, cookies) => {
for (let i = 0; i < cookies.length; ++i) {
const cookie = {
url: cookies[i].url,
name: cookies[i].name,
value: cookies[i].value,
domain: cookies[i].domain,
path: cookies[i].path,
secure: cookies[i].secure,
httpOnly: cookies[i].httponly,
expirationDate: cookies[i].expiry_date
}
session.defaultSession.cookies.set(cookie, (error) => {
if (error) {
console.error(error)
}
})
}
})
importer.on('show-warning-dialog', (e) => {
showImportWarning()
})
importer.on('import-success', (e) => {
showImportSuccess()
})
importer.on('import-dismiss', (e) => {
})