-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
248 lines (213 loc) · 6.71 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
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
let block_urls = [
'chrome://newtab/',
'chrome://extensions/',
'chrome-extension://'
];
let tab_list = {};
let remove_bool;
let latest_version = 2.0;
// events for click icon.
chrome.action.onClicked.addListener((tab) => {
chrome.tabs.query({currentWindow: true}, (tabs) => {
chrome.windows.getCurrent((window) => {
add_list(tabs, window);
})
})
})
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.target === 'update list') {
chrome.storage.local.get(['tabs'], (response) => {
tab_list = response.tabs;
})
} else if (message.target === 'update settings') {
update_settings();
} else if (message.target === 'init profile') {
(async () => {
let response = await check_profile();
sendResponse(response);
})();
}
return true;
})
chrome.runtime.onInstalled.addListener(async () => {
console.log('Hello!');
let response = await check_profile();
if (response.tabs) {
await chrome.storage.local.set({'backup': response.tabs});
let new_tabs = [];
for (let group of response.tabs) {
let new_group = {
id: -1,
data: [],
group_name: '',
time: '',
accordion: true
};
new_group['id'] = group['id'];
new_group['group_name'] = group['group_name'];
new_group['time'] = group['time'];
for (let data of group['data']) {
new_group['data'].push({
favIconUrl: data['icon'] ? data['icon'] : data['favIconUrl'],
incognito: data['incognito'],
title: data['title'],
url: data['url']
})
}
new_tabs.push(new_group);
}
await chrome.storage.local.set({'tabs': new_tabs});
console.log('transfer to new version.');
}
})
chrome.commands.onCommand.addListener(async (command) => {
console.log('Command:', command);
if (command === 'open-manage-page') {
chrome.tabs.query({
url: "chrome-extension://" + chrome.runtime.id + "/html/manage_page.html",
}, (response) => {
if (response[0]) {
chrome.windows.update(response[0].windowId, {focused: true}, () => {
chrome.tabs.sendMessage(response[0].id, {target: "refresh"});
});
} else {
chrome.windows.create({
url: "html/manage_page.html"
});
}
})
}
})
/**
*
* @param tabs a list for tabs
* @param window
*/
function add_list(tabs, window) {
if (!tabs) return;
let date = new Date();
let time = date.getTime();
let time_string = date.getFullYear() + '年' +
(date.getMonth() + 1) + '月' +
date.getDate() + '日 ' +
date.getHours() + ':' +
date.getMinutes() + ':' +
date.getSeconds();
let tab_list_add = {
id: time,
group_name: '',
time: time_string,
data: [],
accordion: false
};
// add tabs into list
for (let tab of tabs) {
if (tab.url in block_urls) continue;
tab_list_add['data'].push({
title: tab.title,
url: tab.url,
favIconUrl: tab.favIconUrl ? tab.favIconUrl : '../icon/web icon.png',
incognito: window.incognito
});
}
console.log(tab_list_add);
let manage_window_id;
let tab_id;
chrome.storage.local.get(['tabs'], (response) => {
if (response.tabs) {
tab_list = response.tabs;
} else {
tab_list = [];
}
if (remove_bool === undefined) {
console.log('error, did not get settings!');
return;
}
chrome.tabs.query({
url: 'chrome-extension://' + chrome.runtime.id + '/html/manage_page.html'
}, (tabs_response) => {
// if the manage page is open, then get the window id and focus on it,
// or create a new window and open management page.
if (tabs_response[0]) {
manage_window_id = tabs_response[0].windowId;
tab_id = tabs_response[0].id;
}
if (tab_list_add.data[0]) {
tab_list.unshift(tab_list_add);
chrome.storage.local.set({
tabs: tab_list
}, () => {
if (remove_bool) {
chrome.windows.remove(window.id);
}
})
} else {
if (remove_bool) {
chrome.windows.remove(window.id);
}
}
if (manage_window_id) {
chrome.windows.update(manage_window_id, {focused: true}, () => {
chrome.tabs.sendMessage(tab_id, {target: 'refresh'});
});
} else {
chrome.windows.create({
url: "html/manage_page.html"
}, () => {
})
}
})
})
}
function update_settings() {
console.log('update settings');
chrome.storage.local.get(['settings'], (response) => {
let settings = response.settings;
if (!settings) {
init_profile('settings');
}
try {
remove_bool = settings.remove;
} catch (e) {
remove_bool = true;
}
})
}
function init_profile(profile_type) {
console.log('initialize data');
switch (profile_type) {
case 'version':
chrome.storage.local.set({version: latest_version});
break;
case 'settings':
chrome.storage.local.set({
settings: {
remove: true,
accordion: true,
accordion_active: true,
accordion_active_default: true
}
});
break;
case 'tabs':
chrome.storage.local.set({tabs: []});
}
}
async function check_profile() {
return new Promise(resolve => {
console.log('check data....');
let profile_data = ['version', 'tabs', 'settings'];
chrome.storage.local.get(profile_data, (response_data) => {
console.log(response_data);
for (let i of profile_data) {
if (!(i in response_data)) {
console.log(i, ' has no data.');
init_profile(i);
}
}
console.log('detected data exist.');
resolve(response_data);
})
})
}
update_settings();