-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackground.js
86 lines (66 loc) · 2.44 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
var leftButton, rightButton, extension = opera.extension;
var tabs = extension.tabs.getAll ? extension.tabs.getAll() : extension.tabs;
// for compatibility with Opera 11.x
function getFocusedWindow() {
return extension.windows.getFocused ? extension.windows.getFocused() : extension.windows.getLastFocused();
}
function closeTab(tab) {
tabs.close ? tabs.close(tab) : tab.close();
}
function isFocused(tab) {
return tab.focused || tab.selected;
}
function closeTabsRightOfCurrent(current_tabs) {
var current_tabs_arr = current_tabs.map ? current_tabs : current_tabs.getAll();
var i = current_tabs_arr.map(isFocused).indexOf(true);
current_tabs_arr.slice(i + 1 - parseInt(widget.preferences.includeSelf)).forEach(closeTab);
}
function closeTabsLeftOfCurrent(current_tabs) {
var current_tabs_arr = current_tabs.map ? current_tabs : current_tabs.getAll();
var i = current_tabs_arr.map(isFocused).indexOf(true);
current_tabs_arr.slice(0, i + parseInt(widget.preferences.includeSelf)).forEach(closeTab);
}
function setupConnection() {
extension.onmessage = function(event) {
resetButtons();
};
extension.onconnect = function(event) {
event.source.postMessage("hello");
};
}
function resetButtons() {
opera.contexts.toolbar.removeItem(leftButton);
opera.contexts.toolbar.removeItem(rightButton);
if (widget.preferences.option == '1') {
opera.contexts.toolbar.addItem(leftButton);
}
else if (widget.preferences.option == '2') {
opera.contexts.toolbar.addItem(rightButton);
}
}
window.addEventListener('load', function() {
var leftProperties = {
disabled: false,
title: "Close tabs left of current",
icon: "icons/icon_left_18.png"
};
var rightProperties = {
disabled: false,
title: "Close tabs right of current",
icon: "icons/icon_right_18.png"
};
leftButton = opera.contexts.toolbar.createItem(leftProperties);
rightButton = opera.contexts.toolbar.createItem(rightProperties);
leftButton.onclick = function() {
closeTabsLeftOfCurrent(getFocusedWindow().tabs);
};
rightButton.onclick = function() {
closeTabsRightOfCurrent(getFocusedWindow().tabs);
};
setupConnection();
if (widget.preferences.option == undefined)
widget.preferences.option = '2'; //default: right
if (widget.preferences.includeSelf == undefined)
widget.preferences.includeSelf = 0; //default: no
resetButtons();
}, false);