forked from cr/MoveTabTo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
161 lines (135 loc) · 4.65 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
"use strict";
function getWindowTitle(windowId) {
const storedName = localStorage.getItem(windowId);
// console.log(`Stored name for window ${windowId} is "${storedName}"`);
if ((storedName === null) || (storedName.length === 0)) {
return `Window-${windowId}`;
}
return storedName;
}
/*
Called when the item has been created, or when creation failed due to an error.
We'll just log success/failure here.
*/
function onCreated() {
// if (browser.runtime.lastError) {
// console.log(`Error: ${browser.runtime.lastError}`);
// } else {
// console.log("Item created successfully");
// }
}
/*
Called when the item has been removed.
We'll just log success here.
*/
function onRemoved() {
console.log("Item removed successfully");
}
/*
Called when there was an error.
We'll just log the error here.
*/
function onError(error) {
console.log(`Error: ${error}`);
}
/*
Create all the context menu items.
*/
function updateMenu() {
browser.menus.removeAll();
function addWindowsToMenu(windowInfoArray) {
for (const windowInfo of windowInfoArray) {
console.log(`Adding window to menu: ${windowInfo.id}`);
//console.log(windowInfo.tabs.map((tab) => {return tab.url}));
browser.menus.create({
id: `move-to_${windowInfo.id}`,
title: `... to "${getWindowTitle(windowInfo.id)}"`,
contexts: ["tab", "all"]
}, onCreated);
}
return windowInfoArray;
}
function addWindowFocusToMenu(windowInfoArray) {
browser.menus.create({
id: "separator-focus",
type: "separator",
contexts: ["tab", "all"]
}, onCreated);
for (const windowInfo of windowInfoArray) {
console.log(`Adding window focus to menu: ${windowInfo.id}`);
browser.menus.create({
id: `focus-on_${windowInfo.id}`,
title: `Focus "${getWindowTitle(windowInfo.id)}"`,
contexts: ["tab", "all"]
}, onCreated);
}
}
function addSidebarToMenu() {
browser.menus.create({
id: "separator-sidebar",
type: "separator",
contexts: ["tab", "all"]
}, onCreated);
browser.menus.create({
id: "open-sidebar",
title: browser.i18n.getMessage("menuItemOpenSidebar"),
contexts: ["tab", "all"],
command: "_execute_sidebar_action"
}, onCreated);
}
function onError(error) {
console.log(`Error: ${error}`);
}
const getting = browser.windows.getAll({
populate: true,
windowTypes: ["normal"]
});
getting.then(addWindowsToMenu, onError)
.then(addWindowFocusToMenu, onError)
.then(addSidebarToMenu, onError);
}
/*
The click event listener, where we perform the appropriate action given the
ID of the menu item that was clicked.
*/
browser.menus.onClicked.addListener((info, tab) => {
function onMoved(tab) {
console.log("Moved tab: ", tab);
}
function onActive(tab) {
console.log("Activated tab: ", tab);
}
function onFocused(window) {
console.log("Focused on window: ", window);
}
function onError(error) {
console.log(`Error moving tab: ${error}`);
}
if (info.menuItemId === "open-sidebar") {
console.log("Opening Window Manager sidebar");
} else if (info.menuItemId.startsWith("move-to_")) {
const targetWindowId = parseInt(info.menuItemId.split("_")[1]);
if (tab.windowId !== targetWindowId) {
browser.tabs.move(tab.id, {windowId: targetWindowId, index: -1}).then(onMoved, onError);
browser.tabs.update(tab.id, {active: true}).then(onActive, onError);
}
} else if (info.menuItemId.startsWith("focus-on_")) {
const windowId = parseInt(info.menuItemId.split("_")[1]);
console.log("Focusing on window ", windowId);
browser.windows.update(windowId, {focused: true}).then(onFocused, onError);
} else {
console.warn("Dropping unhandled menu onClicked event: ", info);
}
});
browser.windows.onCreated.addListener(updateMenu); // It is inefficient to rebuild the whole menu
browser.windows.onRemoved.addListener(updateMenu); // It is inefficient to rebuild the whole menu
function runtimeMessageHandler(message) {
console.log("Received message:", message);
switch (message.action) {
case "rename_window":
// updateWindowTitle(message.windowId, message.newName);
updateMenu(); // Isn't this inefficient?
}
}
browser.runtime.onMessage.addListener(runtimeMessageHandler);
updateMenu();