-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathnassh_main.js
204 lines (186 loc) · 6.75 KB
/
nassh_main.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
192
193
194
195
196
197
198
199
200
201
202
203
204
// Copyright 2012 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* @fileoverview The main nassh runtime.
* @suppress {moduleLoad}
*/
import {lib} from '../../libdot/index.js';
import {hterm} from '../../hterm/index.js';
import {cleanupChromeSockets} from '../wassh/js/sockets.js';
import {
disableTabDiscarding, getSyncStorage, loadWebFonts, localize,
openOptionsPage, runtimeSendMessage, sendFeedback, setupForWebApp,
watchBackgroundColor,
} from './nassh.js';
import {CommandInstance} from './nassh_command_instance.js';
/**
* Open a new window to the specified URL.
*
* We have to go through the background page in order to set chrome=no.
* Normally Chrome will ignore it (for security reasons) when run in a
* webpage or tab. Extensions/apps are allowed to though, so grab the
* background page and let it do the open for us.
*
* @param {string} url The URL to open.
* @return {!Promise} A promise resolving once the window opens.
*/
const openNewWindow = function(url) {
const msg = {
command: 'nassh',
width: globalThis.innerWidth,
height: globalThis.innerHeight,
url: url,
window: true,
};
return runtimeSendMessage(msg).then((response) => {
if (response && response.error) {
throw new Error(`request failed: ${response.message}`);
}
});
};
/**
* CSP means that we can't kick off the initialization from the html file,
* so we do it like this instead.
*/
globalThis.addEventListener('DOMContentLoaded', async (event) => {
// If we're being opened by a link from another page, clear the opener setting
// so we can't reach back into them. They should have used noopener, but help
// cover if they don't.
if (globalThis.opener !== null) {
globalThis.opener = null;
globalThis.location.reload();
return;
}
// Check if site's storage has been marked as persistent.
if (globalThis.navigator?.storage?.persist &&
globalThis.navigator?.storage?.persisted) {
if (!await navigator.storage.persisted()) {
// Request persistent storage for site.
const isPersisted = await navigator.storage.persist();
if (!isPersisted) {
console.warn('Failed to request persistent storage.');
}
}
}
const params = new URLSearchParams(globalThis.location.search);
// Allow users to bookmark links that open as a window.
const openas = params.get('openas');
switch (openas) {
case 'window': {
// Delete the 'openas' string so we don't get into a loop. We want to
// preserve the rest of the query string when opening the window.
params.delete('openas');
const url = new URL(globalThis.location.toString());
url.search = params.toString();
openNewWindow(url.href).then(() => globalThis.close);
return;
}
case 'fullscreen':
case 'maximized':
chrome.windows.getCurrent({populate: true}, (win) => {
if (win.tabs.length > 1) {
// If the current window has multiple tabs, create a new window and
// move this tab to it. This avoids confusion if the current window
// has non-secure shell tabs in it.
chrome.tabs.getCurrent((tab) => {
chrome.windows.create({
focused: win.focused,
state: openas,
tabId: tab.id,
});
});
} else {
// If the current window only has 1 tab, reuse the window.
chrome.windows.update(win.id, {state: openas});
}
});
break;
}
const execNaSSH = function() {
const profileId = params.get('profile');
const storage = getSyncStorage();
const terminal = new hterm.Terminal({profileId, storage});
// TODO(crbug.com/1063219) We need this to not prompt the user for clipboard
// permission.
terminal.alwaysUseLegacyPasting = true;
terminal.decorate(lib.notNull(document.querySelector('#terminal')));
terminal.installKeyboard();
const runNassh = function() {
terminal.onOpenNewSession = function() {
openNewWindow(lib.f.getURL('/html/nassh_connect_dialog.html'));
};
terminal.onOpenOptionsPage = openOptionsPage;
terminal.setCursorPosition(0, 0);
terminal.setCursorVisible(true);
let environment = terminal.getPrefs().get('environment');
if (typeof environment !== 'object' || environment === null) {
environment = {};
}
// If the connection profile isn't passed via the hash, check the query
// string for profile-id= override.
let argstr = globalThis.location.hash.substr(1);
if (argstr === '') {
const nasshProfileId = params.get('profile-id');
if (nasshProfileId !== null) {
argstr = `profile-id:${nasshProfileId}`;
}
}
const nasshCommand = new CommandInstance({
io: terminal.io,
syncStorage: storage,
args: [argstr],
environment: environment,
onExit: (code) => {
if (terminal.getPrefs().get('close-on-exit')) {
globalThis.close();
}
},
});
nasshCommand.run();
};
terminal.onTerminalReady = function() {
watchBackgroundColor(terminal.getPrefs());
loadWebFonts(terminal.getDocument());
if (globalThis.chrome?.accessibilityFeatures?.spokenFeedback) {
chrome.accessibilityFeatures.spokenFeedback.onChange.addListener(
(details) => terminal.setAccessibilityEnabled(details.value));
chrome.accessibilityFeatures.spokenFeedback.get({}, (details) => {
// In case it fails, don't break startup.
if (details) {
terminal.setAccessibilityEnabled(details.value);
}
runNassh();
});
} else {
runNassh();
}
};
terminal.contextMenu.setItems([
{name: localize('TERMINAL_CLEAR_MENU_LABEL'),
action: function() { terminal.wipeContents(); }},
{name: localize('TERMINAL_RESET_MENU_LABEL'),
action: function() { terminal.reset(); }},
{name: localize('NEW_WINDOW_MENU_LABEL'),
action: function() {
openNewWindow(lib.f.getURL('/html/nassh_connect_dialog.html'));
}},
{name: localize('FAQ_MENU_LABEL'),
action: function() {
lib.f.openWindow('https://hterm.org/x/ssh/faq', '_blank');
}},
{name: localize('CLEAR_KNOWN_HOSTS_MENU_LABEL'),
action: function() { openOptionsPage('ssh-files'); }},
{name: localize('HTERM_OPTIONS_BUTTON_LABEL'),
action: function() { openOptionsPage(); }},
{name: localize('SEND_FEEDBACK_LABEL'),
action: sendFeedback},
]);
// Useful for console debugging.
globalThis.term_ = terminal;
};
await cleanupChromeSockets();
disableTabDiscarding();
await setupForWebApp();
execNaSSH();
});