This repository has been archived by the owner on Mar 18, 2024. It is now read-only.
forked from Emano-Waldeck/Single-Window
-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
191 lines (182 loc) · 5.22 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
'use strict';
// Open link from right-click context menu
// Open in window Ctrl + N
// Open a new private window
// Open an internal window
// Open an internal page and without leaving focus, use right-click to on a link to open new window
// Focus desktop, use right-click to on a link to open new window
// https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_win_open2
var moveTo = (tab, windowId) => {
windowId = Number(windowId);
chrome.tabs.query({
windowId,
active: true
}, tabs => {
const opt = {
windowId
};
if (tabs && tabs.length) {
opt.index = tabs[0].index + 1;
}
chrome.tabs.move(tab.id, opt, () => {
const lastError = chrome.runtime.lastError;
if (!lastError) {
// https://github.com/Emano-Waldeck/Single-Window/issues/1
chrome.tabs.update(tab.id, {
active: tab.active
}, () => chrome.windows.update(windowId, {
focused: tab.active
}));
}
});
});
};
var observers = {
onFocusChanged: windowId => {
if (windowId !== chrome.windows.WINDOW_ID_NONE) {
localStorage.setItem('window.id', windowId);
// if window is not a browser window, find the active one
chrome.windows.get(windowId, win => {
if (win.type !== 'normal') {
chrome.windows.getAll(wins => {
const win = wins.filter(w => w.type === 'normal').shift();
if (win) {
localStorage.setItem('window.id', win.id);
}
});
}
});
}
},
onCreated: tab => {
if (tab.incognito) {
return;
}
const windowId = localStorage.getItem('window.id');
if (
(tab.url.startsWith('http') || tab.url.startsWith('file')) &&
String(tab.windowId) !== windowId
) {
console.log('block', 'site');
moveTo(tab, windowId);
}
else if (
(tab.url === 'about:blank' || tab.url === '') &&
String(tab.windowId) !== windowId
) {
window.setTimeout(() => chrome.tabs.get(tab.id, tab => {
if (
tab &&
((tab.url.startsWith('http') || tab.url.startsWith('file')) || tab.url === 'about:blank' || tab.url === '')
) {
console.log('block', 'about:blank');
moveTo(tab, windowId);
}
else {
console.log('allow', tab);
}
}), 500, localStorage.getItem('window.id'));
}
else {
console.log('allow', tab);
}
}
};
var install = () => {
chrome.windows.getCurrent(win => {
localStorage.setItem('window.id', win.id);
});
chrome.windows.onFocusChanged.addListener(observers.onFocusChanged);
chrome.tabs.onCreated.addListener(observers.onCreated);
chrome.browserAction.setIcon({
path: {
'16': 'data/icons/16.png',
'19': 'data/icons/19.png',
'32': 'data/icons/32.png',
'38': 'data/icons/38.png',
'48': 'data/icons/48.png',
'64': 'data/icons/64.png'
}
});
chrome.browserAction.setTitle({
title: 'Single window mode is enabled'
});
};
var remove = () => {
chrome.windows.onFocusChanged.removeListener(observers.onFocusChanged);
chrome.tabs.onCreated.removeListener(observers.onCreated);
chrome.browserAction.setIcon({
path: {
'16': 'data/icons/disabled/16.png',
'19': 'data/icons/disabled/19.png',
'32': 'data/icons/disabled/32.png',
'38': 'data/icons/disabled/38.png',
'48': 'data/icons/disabled/48.png',
'64': 'data/icons/disabled/64.png'
}
});
chrome.browserAction.setTitle({
title: 'Single window mode is disabled'
});
};
{
const onStartup = () => chrome.storage.local.get({
enabled: true
}, prefs => {
if (prefs.enabled) {
install();
}
else {
remove();
}
});
chrome.runtime.onInstalled.addListener(onStartup);
chrome.runtime.onStartup.addListener(onStartup);
}
chrome.storage.onChanged.addListener(prefs => {
if (prefs.enabled) {
if (prefs.enabled.newValue) {
install();
}
else {
remove();
}
}
});
chrome.browserAction.onClicked.addListener(() => chrome.storage.local.get({
enabled: true
}, prefs => chrome.storage.local.set({
enabled: prefs.enabled === false
})));
// FAQs & Feedback
chrome.storage.local.get({
'version': null,
'faqs': true,
'last-update': 0
}, prefs => {
const version = chrome.runtime.getManifest().version;
if (prefs.version ? (prefs.faqs && prefs.version !== version) : true) {
const now = Date.now();
const doUpdate = (now - prefs['last-update']) / 1000 / 60 / 60 / 24 > 30;
chrome.storage.local.set({
version,
'last-update': doUpdate ? Date.now() : prefs['last-update']
}, () => {
// do not display the FAQs page if last-update occurred less than 30 days ago.
if (doUpdate) {
const p = Boolean(prefs.version);
window.setTimeout(() => chrome.tabs.create({
url: chrome.runtime.getManifest().homepage_url + '?version=' + version +
'&type=' + (p ? ('upgrade&p=' + prefs.version) : 'install'),
active: p === false
}), 3000);
}
});
}
});
{
const {name, version} = chrome.runtime.getManifest();
chrome.runtime.setUninstallURL(
chrome.runtime.getManifest().homepage_url + '?rd=feedback&name=' + name + '&version=' + version
);
}