/
tab.js
164 lines (164 loc) · 5.54 KB
/
tab.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
(function() {
var g, _ref;
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
if ((_ref = this.vichrome) == null) {
this.vichrome = {};
}
g = this.vichrome;
g.TabHistory = (function() {
function TabHistory() {}
TabHistory.prototype.closeHistStack = [];
TabHistory.prototype.openTabs = {};
TabHistory.prototype.findOpenTabItem = function(tabId) {
var tabs, win, _ref2;
_ref2 = this.openTabs;
for (win in _ref2) {
tabs = _ref2[win];
if (tabs[tabId]) {
return tabs[tabId];
}
}
};
TabHistory.prototype.popOpenTabItem = function(tabId) {
var result, tabs, win, _ref2;
_ref2 = this.openTabs;
for (win in _ref2) {
tabs = _ref2[win];
if (tabs[tabId]) {
result = tabs[tabId];
tabs[tabId] = void 0;
return result;
}
}
};
TabHistory.prototype.addOpenTabItem = function(tab, history) {
this.openTabs[tab.windowId][tab.id] = {};
this.openTabs[tab.windowId][tab.id].tab = tab;
this.openTabs[tab.windowId][tab.id].frames = 0;
if (history) {
return this.openTabs[tab.windowId][tab.id].history = history;
} else {
this.openTabs[tab.windowId][tab.id].history = [];
return this.openTabs[tab.windowId][tab.id].history.push(tab.url);
}
};
TabHistory.prototype.setTopFrameID = function(tab, id) {
return this.openTabs[tab.windowId][tab.id].topFrame = id;
};
TabHistory.prototype.getTopFrameID = function(tab) {
return this.openTabs[tab.windowId][tab.id].topFrame;
};
TabHistory.prototype.setCommandBoxID = function(tab, id) {
return this.openTabs[tab.windowId][tab.id].comBoxID = id;
};
TabHistory.prototype.getCommandBoxID = function(tab) {
return this.openTabs[tab.windowId][tab.id].comBoxID;
};
TabHistory.prototype.setFrames = function(tab, frames) {
return this.openTabs[tab.windowId][tab.id].frames = frames;
};
TabHistory.prototype.addFrames = function(tab) {
return ++this.openTabs[tab.windowId][tab.id].frames;
};
TabHistory.prototype.getFrames = function(tab) {
var _ref2;
return (_ref2 = this.openTabs[tab.windowId][tab.id]) != null ? _ref2.frames : void 0;
};
TabHistory.prototype.initTabHist = function(winId) {
return chrome.windows.getAll({
populate: true
}, __bind(function(wins) {
var tab, win, _i, _len, _results;
_results = [];
for (_i = 0, _len = wins.length; _i < _len; _i++) {
win = wins[_i];
this.openTabs[win.id] = {};
_results.push((function() {
var _j, _len2, _ref2, _results2;
_ref2 = win.tabs;
_results2 = [];
for (_j = 0, _len2 = _ref2.length; _j < _len2; _j++) {
tab = _ref2[_j];
_results2.push(this.addOpenTabItem(tab));
}
return _results2;
}).call(this));
}
return _results;
}, this));
};
TabHistory.prototype.setupListeners = function() {
chrome.tabs.onRemoved.addListener(__bind(function(tabId, info) {
var item;
g.logger.d("tab removed id:" + tabId);
if (info.isWindowClosing) {
return;
}
item = this.popOpenTabItem(tabId);
if (item) {
this.closeHistStack.push(item);
if (this.closeHistStack.length > 10) {
this.closeHistStack.shift();
}
}
}, this));
chrome.tabs.onCreated.addListener(__bind(function(tab) {
g.logger.d("tab created id:" + tab.id);
return this.addOpenTabItem(tab);
}, this));
chrome.tabs.onAttached.addListener(__bind(function(tabId, aInfo) {
g.logger.d("tab attached tab:" + tabId + " -> win:" + aInfo.newWindowId);
return chrome.tabs.get(tabId, __bind(function(tab) {
return this.addOpenTabItem(tab);
}, this));
}, this));
chrome.tabs.onDetached.addListener(__bind(function(tabId, dInfo) {
g.logger.d("tab detached tab:" + tabId + " <- win:" + dInfo.oldWindowId);
return this.popOpenTabItem(tabId);
}, this));
chrome.tabs.onUpdated.addListener(__bind(function(tabId, info, tab) {
var target;
target = this.openTabs[tab.windowId][tabId];
if (info.url) {
target.tab.url = info.url;
target.history.push(info.url);
}
if (info.pinned) {
return target.tab.pinned = info.pinned;
}
}, this));
chrome.windows.onCreated.addListener(__bind(function(win) {
g.logger.d("win created id:" + win.id);
return this.openTabs[win.id] = {};
}, this));
chrome.windows.onRemoved.addListener(__bind(function(winId) {
return delete this.openTabs[winId];
}, this));
return this;
};
TabHistory.prototype.init = function() {
this.initTabHist();
this.setupListeners();
return this;
};
TabHistory.prototype.restoreLastClosedTab = function() {
var item, opt;
item = this.closeHistStack.pop();
while ((item != null) && !this.openTabs[item.tab.windowId]) {
item = this.closeHistStack.pop();
}
if (item == null) {
return;
}
chrome.windows.update(item.tab.windowId, {
focused: true
});
opt = {
windowId: item.tab.windowId,
url: item.tab.url
};
return chrome.tabs.create(opt, __bind(function(tab) {}, this));
};
return TabHistory;
})();
}).call(this);