This repository has been archived by the owner on Dec 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 975
/
importer.js
300 lines (270 loc) · 8.52 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/* 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 app = electron.app
const importer = electron.importer
const dialog = electron.dialog
const BrowserWindow = electron.BrowserWindow
const session = electron.session
// Store
const appStore = require('../js/stores/appStore')
// State
const tabState = require('./common/state/tabState')
const bookmarksState = require('./common/state/bookmarksState')
const bookmarkFoldersState = require('./common/state/bookmarkFoldersState')
// Constants
const messages = require('../js/constants/messages')
const settings = require('../js/constants/settings')
// Actions
const appActions = require('../js/actions/appActions')
const syncActions = require('../js/actions/syncActions')
// Utils
const {getSetting} = require('../js/settings')
const {syncEnabled} = require('../js/state/syncUtil')
const locale = require('./locale')
const tabMessageBox = require('./browser/tabMessageBox')
const {makeImmutable} = require('./common/state/immutableUtil')
const bookmarkFoldersUtil = require('./common/lib/bookmarkFoldersUtil')
const FunctionBuffer = require('../js/lib/functionBuffer')
let isImportingBookmarks = false
let hasBookmarks
let bookmarkList
exports.init = () => {
importer.initialize()
}
exports.importData = (selected) => {
if (selected.get('favorites')) {
isImportingBookmarks = true
const state = appStore.getState()
hasBookmarks = bookmarksState.getBookmarks(state).size > 0 || bookmarkFoldersState.getFolders(state).size > 0
}
if (selected !== undefined) {
importer.importData(selected.toJS())
}
}
exports.importHTML = (selected) => {
isImportingBookmarks = true
const state = appStore.getState()
hasBookmarks = bookmarksState.getBookmarks(state).size > 0 || bookmarkFoldersState.getFolders(state).size > 0
dialog.showDialog(BrowserWindow.getFocusedWindow(), {
type: 'select-open-file',
extensions: [['html', 'htm']]
}, (files) => {
if (files && files.length === 1) {
const file = files[0]
importer.importHTML(file)
}
})
}
importer.on('update-supported-browsers', (e, detail) => {
isImportingBookmarks = false
if (BrowserWindow.getFocusedWindow()) {
BrowserWindow.getFocusedWindow().webContents.send(messages.IMPORTER_LIST, detail)
}
})
importer.on('add-history-page', (e, history) => {
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.addHistorySite(makeImmutable(sites))
})
importer.on('add-homepage', (e, detail) => {
})
const getParentFolderId = (path, pathMap, addFolderFunction, 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 = {
title: parentFolder,
folderId: parentFolderId,
parentFolderId: getParentFolderId(path, pathMap, addFolderFunction, topLevelFolderId, nextFolderIdObject)
}
addFolderFunction(folder)
}
return parentFolderId
}
importer.on('add-bookmarks', (e, importedBookmarks, topLevelFolder) => {
const state = appStore.getState()
const bookmarkFolders = bookmarkFoldersState.getFolders(state)
let nextFolderId = bookmarkFoldersUtil.getNextFolderId(bookmarkFolders)
let nextFolderIdObject = { id: nextFolderId }
let pathMap = {}
let folders = []
let bookmarks = []
let topLevelFolderId = nextFolderIdObject.id++
const isSyncEnabled = syncEnabled()
const syncRecords = []
const functionBuffer = new FunctionBuffer((args) => makeImmutable(args), this)
const bufferedAddBookmark = (bookmark) => {
if (isSyncEnabled) {
bookmark.skipSync = true
syncRecords.push(bookmark)
}
functionBuffer.buffer(appActions.addBookmark, bookmark)
bookmarks.push(bookmark)
}
const bufferedAddFolder = (folder) => {
if (isSyncEnabled) {
folder.skipSync = true
syncRecords.push(folder)
}
functionBuffer.buffer(appActions.addBookmarkFolder, folder)
folders.push(folder)
}
const importTopLevelFolder = {
title: bookmarkFoldersUtil.getNextFolderName(bookmarkFolders, topLevelFolder),
folderId: topLevelFolderId,
parentFolderId: 0
}
bufferedAddFolder(importTopLevelFolder)
for (let i = 0; i < importedBookmarks.length; ++i) {
const importedBookmark = importedBookmarks[i]
const path = importedBookmark.path
const title = importedBookmark.title
const parentFolderId = getParentFolderId(path, pathMap, bufferedAddFolder, topLevelFolderId, nextFolderIdObject)
if (importedBookmark.is_folder) {
const folderId = nextFolderIdObject.id++
pathMap[title] = folderId
const folder = {
title,
folderId,
parentFolderId
}
bufferedAddFolder(folder)
} else {
const location = importedBookmark.url
const bookmark = {
title,
location,
parentFolderId
}
bufferedAddBookmark(bookmark)
}
}
functionBuffer.flush()
bookmarkList = makeImmutable(bookmarks)
if (isSyncEnabled && syncRecords.length) {
syncActions.addSites(syncRecords)
}
})
importer.on('add-favicons', (e, detail) => {
let faviconMap = {}
detail.forEach((entry) => {
if (entry.favicon_url.includes('made-up-favicon')) {
for (let url of entry.urls) {
faviconMap[url] = entry.png_data
}
} else {
for (let url of entry.urls) {
faviconMap[url] = entry.favicon_url
}
}
})
let updatedSites = bookmarkList.map((site) => {
if (
(
site.get('favicon') === undefined &&
site.get('location') !== undefined &&
faviconMap[site.get('location')] !== undefined
) ||
(
site.get('favicon') !== undefined &&
site.get('favicon').includes('made-up-favicon'))
) {
return site.set('favicon', faviconMap[site.get('location')])
} else {
return site
}
})
// TODO can we call addBookmark only once? we need to create a new functions addFavicons
appActions.addBookmark(updatedSites)
})
importer.on('add-keywords', (e, templateUrls, uniqueOnHostAndPath) => {
})
importer.on('add-autofill-form-data-entries', (e, detail) => {
})
const shouldSkipCookie = (cookie) => {
// Bypassing cookie mismatch error in
// https://github.com/brave/browser-laptop/issues/11401
const googleDomain = /^.*\.google.com(\..+)*$/
if (cookie.domain.match(googleDomain) &&
['OSID', 'LSID', 'SIDCC'].includes(cookie.name)) {
return true
}
return false
}
module.exports.shouldSkipCookie = shouldSkipCookie
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
}
if (shouldSkipCookie(cookie)) {
continue
}
session.defaultSession.cookies.set(cookie, (error) => {
if (error) {
console.error(error)
console.error(cookie)
}
})
}
})
const getActiveTabId = () => {
return tabState.getActiveTabId(appStore.getState())
}
const showImportWarning = function () {
const tabId = getActiveTabId()
if (tabId) {
tabMessageBox.show(tabId, {
message: `${locale.translation('closeFirefoxWarning')}`,
title: 'Brave',
buttons: [locale.translation('closeFirefoxWarningOk')]
})
}
}
const showImportSuccess = function () {
const tabId = getActiveTabId()
if (tabId) {
tabMessageBox.show(tabId, {
message: `${locale.translation('importSuccess')}`,
title: 'Brave',
buttons: [locale.translation('importSuccessOk')]
})
}
}
app.on('show-warning-dialog', (e) => {
showImportWarning()
})
importer.on('import-success', (e) => {
if (isImportingBookmarks) {
const showBookmarksToolbar = getSetting(settings.SHOW_BOOKMARKS_TOOLBAR)
if (!showBookmarksToolbar && !hasBookmarks) {
appActions.changeSetting(settings.SHOW_BOOKMARKS_TOOLBAR, true)
}
}
showImportSuccess()
})
importer.on('import-dismiss', (e) => {
})