-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathcontextmenu.js
191 lines (159 loc) · 4.63 KB
/
contextmenu.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
const PULL_REQUEST_PATH_REGEXP = /.+\/([^/]+)\/(pull)\/[^/]+\/(.*)/;
class OptionValidationError extends Error {
constructor(message) {
super(message);
this.name = 'OptionValidationError';
}
}
async function getOptions() {
const options = await chrome.storage.sync.get({
remoteHost: '',
basePath: '',
insidersBuild: false,
debug: false,
});
if (options.basePath === '') {
throw new OptionValidationError('Looks like you haven\'t configured this extension yet. You can find more information about this by visiting the extension\'s README page.');
}
return options;
}
function getVscodeLink({
repo, file, isFolder, line,
}, {
remoteHost, insidersBuild, basePath, debug,
}) {
let vscodeLink = insidersBuild
? 'vscode-insiders'
: 'vscode';
// vscode://vscode-remote/ssh-remote+[host]/[path/to/file]:[line]
// OR
// vscode://file/[path/to/file]:[line]
if (remoteHost !== '') {
vscodeLink += `://vscode-remote/ssh-remote+${remoteHost}`;
} else {
vscodeLink += '://file';
}
// windows paths don't start with slash
if (basePath[0] !== '/') {
vscodeLink += '/';
}
vscodeLink += `${basePath}/${repo}/${file}`;
// opening a folder and not a file
if (isFolder) {
vscodeLink += '/';
}
if (line) {
vscodeLink += `:${line}:1`;
}
if (debug) {
// eslint-disable-next-line no-console
console.log(`About to open link: ${vscodeLink}`);
}
return vscodeLink;
}
function isPR(linkUrl) {
return PULL_REQUEST_PATH_REGEXP.test(linkUrl);
}
function parseLink(linkUrl, selectionText, pageUrl) {
const url = new URL(linkUrl ?? pageUrl);
const path = url.pathname;
if (isPR(url.pathname)) {
const pathInfo = PULL_REQUEST_PATH_REGEXP.exec(path);
const repo = pathInfo[1];
const isFolder = false;
const file = selectionText;
let line = null;
if (pageUrl.includes(linkUrl)) {
line = pageUrl.replace(linkUrl, '').replace('R', '').replace('L', '');
}
return {
repo,
file,
isFolder,
line,
};
}
const pathRegexp = /.+\/([^/]+)\/(blob|tree)\/[^/]+\/(.*)/;
if (!pathRegexp.test(path)) {
throw new Error(`Invalid link. Could not extract info from: ${path}.`);
}
const pathInfo = pathRegexp.exec(path);
const repo = pathInfo[1];
const isFolder = pathInfo[2] === 'tree';
const file = pathInfo[3];
let line;
if (url.hash.indexOf('#L') === 0) {
line = url.hash.substring(2);
}
return {
repo,
file,
isFolder,
line,
};
}
async function getCurrentTab() {
const queryOptions = { active: true, lastFocusedWindow: true };
const [tab] = await chrome.tabs.query(queryOptions);
return tab;
}
function injectedAlert(message) {
// eslint-disable-next-line no-undef
alert(message);
}
function injectedWindowOpen(url) {
// eslint-disable-next-line no-undef
window.open(url);
}
async function openInVscode({ linkUrl, selectionText, pageUrl }) {
let tab;
try {
tab = await getCurrentTab();
} catch (e) {
// eslint-disable-next-line no-console
console.error('Unexpected error');
// eslint-disable-next-line no-console
console.error(e);
return;
}
try {
const options = await getOptions();
const parsedLinkData = parseLink(linkUrl, selectionText, pageUrl);
const url = getVscodeLink(parsedLinkData, options);
await chrome.scripting.executeScript(
{
target: { tabId: tab.id },
func: injectedWindowOpen,
args: [url],
},
);
} catch (e) {
// eslint-disable-next-line no-console
console.error(e);
await chrome.scripting.executeScript(
{
target: { tabId: tab.id },
func: injectedAlert,
args: [e.message ?? e],
},
);
if (e.name === 'OptionValidationError') {
chrome.runtime.openOptionsPage();
}
}
}
const contextMenuId = 'open-in-vscode-context-menu';
chrome.contextMenus.create({
id: contextMenuId,
title: 'Open in VSCode',
contexts: ['link', 'page'],
});
chrome.contextMenus.onClicked.addListener(({ menuItemId, ...info }) => {
if (menuItemId !== contextMenuId) {
return;
}
openInVscode(info);
});
chrome.action.onClicked.addListener((({ url }) => {
openInVscode({ linkUrl: url, pageUrl: url });
}));