-
Notifications
You must be signed in to change notification settings - Fork 7
/
background.js
221 lines (179 loc) · 6.1 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
/*
-
- Awesome New Tab Page
- http://antp.co/
- Copyright 2011+ Michael Hart (http://h4r7.me/).
-
- Want to make it even more awesome?
- https://github.com/michaelhart/Awesome-New-Tab-Page/
-
- background.html
- Where every journey begins.
-
- Licensed under GPL v3:
- http://www.gnu.org/licenses/gpl-3.0.txt
-
*/
window.setTimeout(' window.location = window.location; ', 1*60*60*1000);
/* START :: Recently Closed Tabs & Tab Manager Widget */
var recently_closed = JSON.parse(localStorage.getItem("recently_closed"));
chrome.tabs.onRemoved.addListener( onRemoved );
function onRemoved(tabId) {
var tab = tabs.filter(function (tab) { return tab.id === tabId})[0];
if (recently_closed === null ) {
recently_closed = [];
}
if ( !tab || tab.incognito === true
|| tab.title === ""
|| (tab.url).indexOf("chrome://" ) !== -1 ) {
return;
}
recently_closed.unshift({title: tab.title, url: tab.url});
if(recently_closed.length > 15) {
recently_closed.pop();
}
localStorage.setItem("recently_closed", JSON.stringify( recently_closed ));
getAllTabs();
}
chrome.tabs.onMoved.addListener( getAllTabs );
chrome.tabs.onCreated.addListener( getAllTabs );
chrome.tabs.onUpdated.addListener( getAllTabs );
chrome.tabs.onHighlighted.addListener( getAllTabs );
var tabs = null;
function getAllTabs() {
chrome.tabs.getAllInWindow(null, getAllTabs_callback);
}
function getAllTabs_callback(data) {
tabs = data;
localStorage.setItem("open_tabs", JSON.stringify( tabs ));
}
chrome.tabs.getAllInWindow(null, getAllTabs);
/* START :: Tab Manager Widget */
$(window).bind('storage', function (e) {
if ( e.originalEvent.key === "switch_to_tab" ) {
if (localStorage.getItem("switch_to_tab") != -1) {
var id = parseInt(localStorage.getItem("switch_to_tab"));
chrome.tabs.getSelected(null, function(tab){
if (id != tab.id) {
chrome.tabs.remove(tab.id);
}
});
chrome.tabs.update(id, {active: true});
localStorage.setItem("switch_to_tab", -1);
}
}
if ( e.originalEvent.key === "close_tab" ) {
var id = parseInt(localStorage.getItem("close_tab"));
if (localStorage.getItem("close_tab") !== "-1") {
chrome.tabs.remove(id);
localStorage.setItem("close_tab", "-1");
}
}
if ( e.originalEvent.key === "pin_toggle" ) {
var id = parseInt(localStorage.getItem("pin_toggle"));
if ( typeof(id) === "null" ) {
return false;
}
var tab = tabs.filter(function (tab) { return tab.id === id; })[0];
if ( typeof(tab) === "undefined" ) {
console.error("Tab wasn't found");
return false;
}
// Invert pin state
chrome.tabs.update(id, { pinned: !tab.pinned });
localStorage.setItem("pin_toggle", "null");
}
});
/* END :: Tab Manager Widget */
/* END :: Recently Closed Tabs */
/* START :: Get Installed Widgets */
var extensions,
event_lock = false;
chrome.management.getAll(reloadExtensions);
function reloadExtensions(data) {
extensions = data;
reloadInstalledWidgets();
event_lock = false;
}
$(window).bind('storage', function (e) {
// When Refresh button in the widgets window is pressed
if ( typeof(e.originalEvent) === "object"
&& typeof(e.originalEvent.key) === "string"
&& e.originalEvent.key === "refresh_widgets" )
chrome.management.getAll(reloadExtensions);
// When Awesome New Tab Page is reset
if ( typeof(e.originalEvent) === "object"
&& typeof(e.originalEvent.key) === "string"
&& e.originalEvent.key === "" )
chrome.management.getAll(reloadExtensions);
});
function reloadInstalledWidgets() {
localStorage.setItem("installed_widgets", JSON.stringify( [] ));
sayHelloToPotentialWidgets();
setTimeout(function() {
window.location = window.location;
}, 10000);
}
chrome.management.onEnabled.addListener( onInstalled );
chrome.management.onInstalled.addListener( onInstalled );
function onInstalled(ExtensionInfo) {
if ( event_lock === true ) {
return;
} else {
event_lock = true;
}
chrome.management.getAll(reloadExtensions);
setTimeout(function() {
recently_installed = null;
}, 5000);
}
chrome.management.onDisabled.addListener( onUninstalled );
chrome.management.onUninstalled.addListener( onUninstalled );
function onUninstalled(ExtensionInfo) {
if ( event_lock === true ) {
return;
} else {
event_lock = true;
}
chrome.management.getAll(reloadExtensions);
// console.log(ExtensionInfo);
}
if ( Math.round(new Date().getTime()/1000.0) > parseInt(localStorage.getItem("last_widget_update")) + 30*60 ) {
chrome.management.getAll(reloadExtensions);
localStorage.setItem("last_widget_update", Math.round(new Date().getTime()/1000.0) );
}
if ( localStorage.getItem("installed_widgets") === null ) {
chrome.management.getAll(reloadExtensions);
}
if ( localStorage.getItem("installed_widgets") === "[]" ) {
if ( Math.round(new Date().getTime()/1000.0) > parseInt(localStorage.getItem("last_widget_update")) + 5*60 ) {
chrome.management.getAll(reloadExtensions);
localStorage.setItem("last_widget_update", Math.round(new Date().getTime()/1000.0) );
}
}
/* END :: Get Installed Widgets */
/* START :: External Communication Stuff */
// Attempts to establish a connection to all installed widgets
function sayHelloToPotentialWidgets() {
for (var i in extensions) {
chrome.extension.sendRequest(
extensions[i].id,
"mgmiemnjjchgkmgbeljfocdjjnpjnmcg-poke"
);
}
};
// Listens for responses
chrome.extension.onRequestExternal.addListener( onRequestExternal );
function onRequestExternal(request, sender, sendResponse) {
if(request.head) {
if(request.head === "mgmiemnjjchgkmgbeljfocdjjnpjnmcg-pokeback") {
var installed_widgets_temp = JSON.parse(localStorage.getItem("installed_widgets"));
installed_widgets_temp.push({
"request": request,
"sender": sender,
});
localStorage.setItem("installed_widgets", JSON.stringify( installed_widgets_temp ));
}
}
}
/* END :: External Communication Stuff */