forked from spyth/Incognito-or-not
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
59 lines (52 loc) · 1.82 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
chrome.browserAction.onClicked.addListener(function (tab) {
// executes function when clicking on extension
chrome.windows.getAll({ populate: true }, function (windows) {
for (var i = 0; i < windows.length; i++) {
if (windows[i].id == tab.windowId) {
createNewTabsInOppositeMode(windows[i].tabs, tab.incognito);
chrome.windows.remove(windows[i].id)
return;
}
}
})
});
chrome.contextMenus.create({
// creates context menu option on links
id: "incognitoornot",
title: "Open Link in Incognito/Normal Window",
contexts: ["link"]
});
chrome.contextMenus.onClicked.addListener(function (info, tab) {
// executes function when clicking through context menu on a link
createNewTabsInOppositeMode([info.linkUrl], tab.incognito);
});
function openTabsInWindow(tabs, windowid, incognito) {
// creates tabs in window of given mode if window given, else creates tabs in new window
urls = [];
tabs.forEach(function getUrls(tab) {
urls.push(tab.url)
})
if (!windowid) {
chrome.windows.create({ url: urls, incognito: incognito });
return;
}
urls.forEach(function createTab(url) {
chrome.tabs.create({ windowId: windowid, url: url, active: true });
})
}
function createNewTabsInOppositeMode(tabs, incognito) {
// creates tab in opp. mode given array of tabs and initial mode
// comment this section
chrome.windows.getAll({ windowTypes: ["normal"] }, function (windows) {
for (var i = 0; i < windows.length; i++) {
if (windows[i].incognito != incognito) {
chrome.windows.update(windows[i].id, { focused: true }, function (focused_window) {
openTabsInWindow(tabs, focused_window.id, !incognito)
});
return;
}
}
// to here to force all conversions in new window
openTabsInWindow(tabs, false, !incognito);
});
}