forked from jonhac/Window-Saver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
109 lines (101 loc) · 2.89 KB
/
background.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
const defaultFolderName = 'Window Saver'
browser.bookmarks.onRemoved.addListener(handleFolderRemoved)
browser.runtime.onMessage.addListener(handleMessage)
async function handleMessage(request, sender, sendResponse) {
console.log(request)
console.log(sender)
if (request.task === 'open_separate') {
let tabList = await browser.tabs.query({ currentWindow: true })
let windowInfo = await browser.windows.create()
console.log('I call this bullshit')
console.log(tabList)
console.log(windowInfo.id)
let errors = []
let length = tabList.length
for (var i = 0; i < length; i++) {
let tab = tabList[i]
console.log(windowInfo.id)
// console.log(tab.url)
try {
let response = await browser.tabs.create({
active: false,
discarded: true,
title: tab.title,
url: tab.url,
})
console.log('it worked')
} catch (err) {
console.log('it failed')
console.log(err)
errors.push(tab)
}
}
console.log('what the hell')
errors.length > 0 && browser.browserAction.openPopup()
}
}
initDefaultSettings()
function initDefaultSettings() {
initFolderSettings()
initSetting('deleteAfter', false)
initSetting('confirmSavePrivate', true)
initSetting('confirmDelete', true)
initSetting('confirmOverride', true)
initSetting('confirmCloseNonPrivate', false)
initSetting('confirmClosePrivate', false)
initSetting('showPlaceholder', true)
async function initSetting(name, value) {
let setting = await browser.storage.local.get(name)
if ('undefined' == typeof setting[name]) {
browser.storage.local.set({ [name]: value })
}
}
async function initFolderSettings() {
let foundSettings = await browser.storage.local.get('folderId')
if ('undefined' == typeof foundSettings.folderId) {
let id = await findOrCreateFolder(defaultFolderName)
browser.storage.local.set({ folderId: id })
}
}
}
async function findOrCreateFolder(name) {
let results = await browser.bookmarks.search({ title: name })
if (results.length === 0) {
let id = await createFolder()
return id
} else if (results.length === 1) {
return results[0].id
} else {
let candidate = null
for (let result of results) {
if (
'undefined' == typeof result.url &&
'unfiled_____' === result.parentId
) {
if (candidate) {
// more than one valid candidate
throw new Error('more than one folder with name: ' + name)
} else {
candidate = result
}
}
}
if (candidate) {
return candidate.id
} else {
return await createFolder()
}
}
async function createFolder() {
let folder = await browser.bookmarks.create({ title: name })
return folder.id
}
}
async function handleFolderRemoved(id, info) {
let settings = await browser.storage.local.get('folderId')
if (id === settings.folderId) {
// They deleted our folder! We go back to the default.
let id = await findOrCreateFolder(defaultFolderName)
browser.storage.local.set({ folderId: id })
}
}