-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
170 lines (135 loc) · 3.45 KB
/
background.js
File metadata and controls
170 lines (135 loc) · 3.45 KB
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
/**
* RapidTabOpener
*
* Action to be executed when toolbar button is clicked. Determines the
* current window mode and the desired window type then with that
* information either opens a new window first or the specified URLs.
*
* @author cedricium - Cedric Amaya
*/
(function setDefaultSettings() {
var urls = [];
var windowSettings = {type: 'normal'};
browser.storage.local.set({
urls,
windowSettings
});
})();
browser.browserAction.onClicked.addListener(getDataFromStorage);
var urls,
windowSettings,
currentWindow;
function getDataFromStorage(window) {
currentWindow = window;
var getting = browser.storage.local.get(["urls", "windowSettings"]);
getting.then(determineWindow);
}
function determineWindow(item) {
if (Object.keys(item).length == 0)
openOptionsPage();
else {
urls = item.urls;
windowSettings = item.windowSettings;
}
var windowType = windowSettings.type;
if (urls.length <= 0)
openOptionsPage();
else {
if (windowType === "incognito" && currentWindow.incognito)
openURLs();
if (windowType === "incognito" && !currentWindow.incognito)
openWindow(windowType);
if (windowType === "normal" && !currentWindow.incognito)
openURLs();
if (windowType === "normal" && currentWindow.incognito)
openWindow(windowType);
}
}
function openOptionsPage() {
browser.runtime.openOptionsPage();
var title = "Oops!",
message = "Add some URLs that you want opened.";
createNotification(title, message);
}
function openWindow(type) {
var newWindow;
if (type === "normal") {
newWindow = browser.windows.create({
incognito: false,
state: "maximized"
});
}
else if (type === "incognito") {
newWindow = browser.windows.create({
incognito: true,
state: "maximized"
});
}
newWindow.then(openURLs);
}
function closeNewTab(tabs) {
var newTab = tabs[tabs.length - 1];
var removingNewTab = browser.tabs.remove(newTab.id);
}
function openURLs() {
var querying = browser.tabs.query({currentWindow: true});
querying.then(closeNewTab);
for (var i = 0; i < urls.length; i++) {
var url = urls[i];
browser.tabs.create({
index: i,
url: url
});
}
}
browser.contextMenus.create({
id: "add_page",
title: "Add Page to RapidTabOpener",
type: "normal",
contexts: [
"page",
"tab"
],
onclick: listener
});
browser.contextMenus.create({
id: "open_options_page",
title: "Open Options Page",
type: "normal",
contexts: [
"page",
"tab"
],
onclick: listener
});
function listener(info, tab) {
if (info.menuItemId == "open_options_page")
browser.runtime.openOptionsPage();
else { // for handling the add_page context
var getting = browser.storage.local.get(["urls"]);
getting.then(function(item) {
addURL(info, item);
});
}
}
function addURL(info, item) {
var urls = item.urls,
urlToAdd = info.pageUrl;
urls.push(urlToAdd);
browser.storage.local.set({ urls });
browser.runtime.sendMessage({
message: 'URL added via context menu.'
});
var title = "Website Added!",
message = 'The following URL was added to your RapidTabs: "' +
urlToAdd + '".';
createNotification(title, message);
}
function createNotification(heading, message) {
browser.notifications.create({
"type": "basic",
"title": heading,
"message": message,
"iconUrl": "icons/icon-128.png"
});
}