-
Notifications
You must be signed in to change notification settings - Fork 14
/
plugin.js
107 lines (94 loc) · 2.56 KB
/
plugin.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
var ReloadPlugin = function (settings) {
var self = this;
self.update(settings);
self.isGoing = false;
chrome.tabs.onActivated.addListener(function (activeInfo) {
chrome.tabs.get(activeInfo.tabId, function (t) {
self.currentTab = t;
if (self.isGoing) {
self.startTimer();
}
});
});
};
ReloadPlugin.prototype.update = function (settings) {
var self = this;
self.timeDelay = settings.seconds || 10;
self.tabReload = settings.reload || true;
self.tabInactive = settings.inactive || false;
self.tabAutostart = settings.autostart || false;
self.noRefreshList = settings.noRefreshList || [];
self.reloadTabIds = settings.reloadTabIds || [];
};
ReloadPlugin.prototype.start = function () {
var self = this;
self.isGoing = true;
self.getActiveTab(function (tab) {
self.currentTab = tab;
self.startTimer();
});
};
ReloadPlugin.prototype.stop = function () {
var self = this;
self.isGoing = false;
clearTimeout(self.timer);
};
ReloadPlugin.prototype.startTimer = function () {
var self = this;
clearTimeout(self.timer);
self.timer = setTimeout(function () {
self.loadNextTab();
}, self.timeDelay * 1000);
};
ReloadPlugin.prototype.getActiveTab = function (cb) {
chrome.tabs.query({
'active': true,
'windowId': self.currentWindow
}, function (tab) {
cb(tab[0]);
});
};
ReloadPlugin.prototype.loadNextTab = function () {
var self = this;
var ix = self.currentTab.index + 1;
chrome.tabs.query({ windowId: self.currentWindow }, function (tabs) {
if (ix >= tabs.length) {
ix = 0;
}
console.log(self.currentWindow, ix, tabs);
var nextTab = tabs.filter(function (t) {
return t.index === ix;
});
if (nextTab.length > 0) {
self.activateTab(nextTab[0]);
}
});
};
ReloadPlugin.prototype.shouldReloadTab = function (id) {
var self = this;
return (self.tabReload && self.reloadTabIds.length === 0)
|| (self.reloadTabIds.indexOf(id) > -1);
};
ReloadPlugin.prototype.activateTab = function (tab) {
var self = this;
function setTabActive () {
chrome.tabs.update(tab.id, { active: true }, function () {
self.startTimer();
});
}
if (self.shouldReloadTab(tab.id)) {
chrome.tabs.onUpdated.addListener(function tabLoadComplete (tabId, info, t) {
if (info.status === "complete") {
chrome.tabs.onUpdated.removeListener(tabLoadComplete);
setTabActive();
}
});
chrome.tabs.reload(tab.id, {}, null);
}
else {
setTabActive();
}
};
ReloadPlugin.prototype.destroy = function () {
self.timer = null;
};