-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
newtab.js
81 lines (74 loc) · 2.43 KB
/
newtab.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
'use strict';
/* global defaults */
const BACKGROUND_COLOR_PAGE = 'html/background_color.html';
const LOCAL_FILE_PAGE = 'html/local_file.html';
const LOCAL_FILE_MISSING_PAGE = 'html/local_file_missing.html';
const FEED_PAGE = 'html/feed.html';
const NEW_TAB_PAGE = 'html/newtab.html';
/**
* @exports newtab
*/
const newtab = {
/**
* This method is used to navigate to the set new tab page.
*
* @returns {void}
*/
async init () {
const options = await browser.storage.local.get(defaults);
const url = options.type === 'about:home' ? options.type : options.url;
switch (options.type) {
case 'about:blank':
browser.tabs.update({ url : options.type });
break;
case 'about:home':
case 'custom_url':
newtab.openNewTabPage(url, options.focus_website);
break;
case 'background_color':
newtab.openNewTabPage(browser.extension.getURL(BACKGROUND_COLOR_PAGE), options.focus_website);
break;
case 'feed':
newtab.openNewTabPage(browser.extension.getURL(FEED_PAGE), options.focus_website);
break;
case 'local_file':
if (options.local_file) {
newtab.openNewTabPage(browser.extension.getURL(LOCAL_FILE_PAGE), options.focus_website);
}
else {
newtab.openNewTabPage(browser.extension.getURL(LOCAL_FILE_MISSING_PAGE), options.focus_website);
}
break;
default:
browser.tabs.update({ url : 'about:blank' });
}
},
/**
* This method is used to set the focus either on the address bar or on the web page.
*
* @param {string} url - url to open
* @param {boolean} focus_website - whether the focus should be on the web page instead of the address bar
*
* @returns {void}
*/
async openNewTabPage (url, focus_website) {
// set focus on website
if (focus_website) {
await browser.tabs.getCurrent((tab) => {
const tabId = tab.id;
browser.tabs.create({ url : url || 'about:blank' }, () => {
browser.tabs.remove(tabId);
});
});
}
// set focus on address bar
else {
await browser.tabs.update({ url : url || 'about:blank' }, () => {
// there is nothing to do, but it's needed, otherwise browser.history.deleteUrl() does not work
});
}
// delete spammy new tab page entry from history
browser.history.deleteUrl({ url : browser.extension.getURL(NEW_TAB_PAGE) });
}
};
newtab.init();