-
Notifications
You must be signed in to change notification settings - Fork 4
/
background.js
170 lines (150 loc) · 5.06 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
// References:
// - https://chromium.googlesource.com/chromium/src/+/master/chrome/common/extensions/docs/examples/api/windows/merge_windows
CONTEXT_MENU_ID = null;
currentTab = null;
targetWindow = null;
// Creates/updates the context menu item
// Adds different text depending on whether the current tab is in
// incognito or normal window
function updateContextMenu() {
chrome.tabs.query({ active: true, lastFocusedWindow: true }, (tabs) => {
currentTab = tabs[0];
var title = "Open in incognito";
if (currentTab.incognito) title = "Open in normal tab";
if (CONTEXT_MENU_ID == null) {
CONTEXT_MENU_ID = chrome.contextMenus.create({
id: "toggle-incognito",
title: title,
visible: true,
contexts: ["all"],
});
} else {
chrome.contextMenus.update(CONTEXT_MENU_ID, { title: title });
}
});
}
// The main function which toggles the current tab from a
// normal window to incognito window and vice-versa
// It automatically detects the current window type and reloads
function toggle() {
// Fetch the current active tab from the last focussed window
chrome.tabs.query({ active: true, lastFocusedWindow: true }, (tabs) => {
currentTab = tabs[0];
chrome.windows.getAll({ populate: true }, (allWindows) => {
if (currentTab.incognito) {
// Current tab is in incognito
// Open in normal window
targetWindow = getNormal(allWindows);
} else {
// Current tab is not in incognito
// Open in incognito window
targetWindow = getIncognito(allWindows);
}
// Move the current tab to target window
moveTab(currentTab, targetWindow);
});
});
}
// Fetch the most recently created normal window
// from all the windows
// Returns null if doesn't exist
function getNormal(allWindows) {
var normalWindow = null;
if (allWindows != null) {
for (var i = allWindows.length - 1; i >= 0; i--) {
var currentWindow = allWindows[i];
if (!currentWindow.incognito) {
normalWindow = currentWindow;
break;
}
}
}
return normalWindow;
}
// Fetch the most recently created incognito window
// from all the windows
// Returns null if doesn't exist
function getIncognito(allWindows) {
var incognitoWindow = null;
if (allWindows != null) {
for (var i = allWindows.length - 1; i >= 0; i--) {
var currentWindow = allWindows[i];
if (currentWindow.incognito) {
incognitoWindow = currentWindow;
break;
}
}
}
return incognitoWindow;
}
// Fetch the new tab (without any loaded URL) if any
// in the given window
// Returns null if doesn't exist
function getNewTabInWindow(window) {
var newTab = null;
if (window != null) {
for (var i = window.tabs.length - 1; i >= 0; i--) {
var currentTab = window.tabs[i];
if (currentTab.url == "chrome://newtab/") {
newTab = currentTab;
break;
}
}
}
return newTab;
}
// Reload the given tab in the target window
// Cannot use chrome.tabs.move() function to move instead of reloading
// since normal window and incognito window are not of the same profile
//
// This is the reason to require 'tabs' permission to get the URL
// of current tab
function moveTab(currentTab, targetWindow) {
if (currentTab != null) {
if (targetWindow != null) {
// Cannot move a tab from normal window to incognito window or vice-versa,
// since they are not of the same profile
// chrome.tabs.move(currentTab.id, {"windowId": targetWindow.id, "index": numTabsInTargetWindow + 1} );
var newTab = getNewTabInWindow(targetWindow);
if (newTab != null) {
// There is a new tab without a loaded URL in target window
chrome.tabs.update(newTab.id, {
url: currentTab.url,
active: true,
});
} else {
// Calculate the number of tabs in the target window
var numTabsInTargetWindow = targetWindow.tabs.length;
// Create a new tab in the target window
chrome.tabs.create({
windowId: targetWindow.id,
index: numTabsInTargetWindow + 1,
url: currentTab.url,
});
}
// Creating a new tab doesn't make the window focussed
// Focus the targetWindow
chrome.windows.update(targetWindow.id, {
focused: true,
});
} else {
// If there is no existing target window, open the URL in a new window
// If current tab is in incognito, open in a new normal window
// If current tab is not in incognito, open in a new incognito window
chrome.windows.create({
url: currentTab.url,
incognito: !currentTab.incognito,
});
}
}
}
// Create the context menu
updateContextMenu();
chrome.contextMenus.onClicked.addListener(toggle);
// Trigger the toggle upon clicked
chrome.browserAction.onClicked.addListener(toggle);
// Add event listener to update context menu for dynamic content
chrome.extension.onMessage.addListener((request, sender, sendResponse) => {
if (request.message == "updateContextMenu") updateContextMenu();
else sendResponse({});
});