-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy patheventPage.js
332 lines (281 loc) · 9.19 KB
/
eventPage.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
//#region globals
const REPO_MAP_LOCAL_STORAGE_KEY = 'repoMap';
const GITHUB_COM = 'github.com';
const GITHUB_DEV = 'github.dev';
const AZURE_REPOS = 'dev.azure.com';
const AZURE_REPOS_LEGACY = 'visualstudio.com';
const VSCODE_DEV = 'vscode.dev';
const INSIDERS_VSCODE_DEV = 'insiders.vscode.dev';
const DEFAULT_VSCODE_BUILD = VSCODE_DEV;
const OPTIONS_KEY_FOR_VSCODE_BUILD = 'vsCodeBuild';
const invalidGitHubRepositoryOwners = [
'blog',
'explore',
'showcases',
'trending',
'stars',
'contact',
'about',
'orgs',
'codespaces',
'settings',
'marketplace',
'pulls',
'issues',
'notifications',
'account',
'discussions',
'sponsors',
'login',
'auth',
'codesearch',
'enterprise',
'enterprises',
'security',
'team',
'customer-stories',
'readme',
'pricing',
'features'
];
let defaultSuggestionURL = '';
//#endregion
//#region extension options
let preferredVSCodeBuildDomain = DEFAULT_VSCODE_BUILD;
function updatePreferredVSCodeBuildDomain(newDomain) {
preferredVSCodeBuildDomain = newDomain;
// Set icon
if (newDomain === DEFAULT_VSCODE_BUILD) {
chrome.action.setIcon({
path: "/vscode.png"
})
}
else {
chrome.action.setIcon({
path: "/vscode-insiders.png"
})
}
}
chrome.storage.sync.get({
vsCodeBuild: DEFAULT_VSCODE_BUILD
}, function (items) {
updatePreferredVSCodeBuildDomain(items.vsCodeBuild);
});
chrome.storage.sync.onChanged.addListener((changes) => {
if (changes[OPTIONS_KEY_FOR_VSCODE_BUILD] !== undefined) {
updatePreferredVSCodeBuildDomain(changes[OPTIONS_KEY_FOR_VSCODE_BUILD].newValue);
}
})
//#endregion
//#region URL transformation helpers
function redirect(tab) {
const url = dotComToDotDev(tab.url);
chrome.tabs.update({ url: url !== undefined ? url.toString() : `https://${preferredVSCodeBuildDomain}` });
}
function dotComToDotDev(url) {
try {
let parsedUrl = new URL(url);
if (parsedUrl.hostname.endsWith(GITHUB_COM) || parsedUrl.hostname.endsWith(GITHUB_DEV)) {
parsedUrl.hostname = preferredVSCodeBuildDomain;
parsedUrl.pathname = `/github${parsedUrl.pathname}`;
return parsedUrl.toString();
} else if (parsedUrl.hostname.endsWith(AZURE_REPOS) || parsedUrl.hostname.endsWith(AZURE_REPOS_LEGACY)) {
parsedUrl.hostname = preferredVSCodeBuildDomain;
const urlWithoutSearch = new URL(url);
const search = urlWithoutSearch.search;
urlWithoutSearch.search = '';
parsedUrl.pathname = urlWithoutSearch.toString();
parsedUrl.search = search;
return parsedUrl.toString();
}
} catch { }
return undefined;
}
function shouldRedirect(url) {
try {
const parser = new URL(url);
// Do not redirect to vscode.dev if we are already on vscode.dev, github.dev, or Codespaces
return !parser.hostname.endsWith(GITHUB_DEV) && !parser.hostname.endsWith(VSCODE_DEV);
} catch {
return false;
}
}
function parseRepoFromUrl(url) {
let owner, repoName, fullName;
try {
const parser = new URL(url);
if (parser.hostname == GITHUB_COM || (parser.hostname === GITHUB_DEV)) {
var paths = parser.pathname.split('/');
if (paths.length >= 3) {
owner = paths[1];
repoName = paths[2];
fullName = owner + '/' + repoName;
}
} else if (parser.hostname === VSCODE_DEV || parser.hostname === INSIDERS_VSCODE_DEV) {
var paths = parser.pathname.split('/');
if (paths[1] === 'github' && paths.length >= 4) {
owner = paths[2];
repoName = paths[3];
fullName = owner + '/' + repoName;
}
}
} catch { }
return { owner, repoName, fullName };
}
//#endregion
//#region Helpers for suggesting recently-visited GitHub repos
// Build a hashmap of accessed repos
function buildRepoMap(cb) {
chrome.history.search({
text: 'github',
startTime: 0,
maxResults: 1000000
}, function (hits) {
var repoMap = {};
hits.forEach((hit) => {
const { owner, repoName, fullName } = parseRepoFromUrl(hit.url);
if (!owner || !repoName || !fullName) {
return;
}
addRepoToMap(repoMap, owner, repoName, fullName);
});
cb(repoMap);
});
}
// After building repoMap, set it in local storage
function buildAndSetRepoMap() {
buildRepoMap(function (repoMap) {
chrome.storage.local.set({ repoMap: repoMap }, function () {
console.log('RepoMap built successfully');
});
});
}
function addRepoToMap(repoMap, owner, repoName, fullName) {
fullName = fullName.toLowerCase();
if (!repoMap[fullName] && !invalidGitHubRepositoryOwners.includes(owner)) {
repoMap[fullName] = {
url: 'https://github.com/' + fullName,
owner: owner,
repoName: repoName
}
return true;
}
return false;
}
//#endregion
//#region Chrome extension API event listeners
chrome.action.onClicked.addListener((tab) => redirect(tab));
chrome.commands.onCommand.addListener((command) => {
if (command === "launchVSCode") {
chrome.tabs.query({ active: true, lastFocusedWindow: true }, (tabs) => {
const tab = tabs[0];
if (tab && shouldRedirect(tab.url)) {
redirect(tab);
}
});
}
});
chrome.runtime.onStartup.addListener(buildAndSetRepoMap);
chrome.runtime.onInstalled.addListener(buildAndSetRepoMap);
chrome.omnibox.onDeleteSuggestion.addListener(function (text) {
chrome.storage.local.get(REPO_MAP_LOCAL_STORAGE_KEY, function (storageObj) {
const repoMap = storageObj.repoMap;
delete repoMap[text];
chrome.storage.local.set({ repoMap: repoMap }, function () {
console.log('RepoMap updated successfully in local storage');
});
})
});
chrome.omnibox.onInputChanged.addListener(function (input, suggest) {
input = input.trim().toLowerCase();
const isSingleKeyword = input.split(' ').length === 1 && input !== '';
chrome.storage.local.get(REPO_MAP_LOCAL_STORAGE_KEY, function (storageObj) {
// Go through repoMap, find suggestions based on keyword
var repoMap = storageObj.repoMap ?? {};
var suggestions = [];
for (const fullName in repoMap) {
const repo = repoMap[fullName];
const suggestion = {
content: dotComToDotDev(repo.url),
description: fullName,
deletable: true
};
// See if we have multiple or just a single keyword
if (isSingleKeyword) {
// Single keyword
const keyword = input.trim().toLowerCase();
// Put exact match in the front of suggestions
if (repo.repoName.toLowerCase() == keyword) {
suggestions.unshift(suggestion);
} else if (fullName.toLowerCase().includes(keyword)) {
suggestions.push(suggestion);
}
} else {
// Multiple keywords
const keywords = input.trim().toLowerCase().split(' ');
const inFullName = function (keyword) {
return fullName.toLowerCase().includes(keyword);
};
if (keywords.filter((keyword) => !inFullName(keyword)).length === 0) {
suggestions.push(suggestion);
}
}
}
// Use the first suggestion as default
let defaultSuggestionDescription;
if (suggestions.length > 0) {
defaultSuggestionDescription = '<match>' + suggestions[0].description + '</match>';
defaultSuggestionURL = suggestions[0].content;
suggestions = suggestions.slice(1);
} else if (isSingleKeyword && input.split('/').filter((segment) => segment.trim() !== '').length === 2) {
const url = `https://${preferredVSCodeBuildDomain}/github/` + input;
defaultSuggestionDescription = '<match>Open ' + url + '"</match>';
defaultSuggestionURL = url;
} else {
defaultSuggestionDescription = '<match>No match found. Search GitHub with "' +
input + '"</match>';
defaultSuggestionURL = 'https://github.com/search?q=' + input.replace(' ', '+');
}
chrome.omnibox.setDefaultSuggestion({
description: defaultSuggestionDescription
});
suggest(suggestions);
});
});
chrome.omnibox.onInputEntered.addListener(function (input) {
let url;
if (input === undefined) {
// Launch root vscode.dev instance, equivalent to typing `code` in CLI
url = `https://${preferredVSCodeBuildDomain}`;
} else if (input.startsWith('https://github.com/') || input.startsWith('https://insiders.vscode.dev')) {
// If input is a valid Github or vscode.dev URL, the user has selected something else than the default option
url = input;
} else {
url = defaultSuggestionURL;
}
if (!url) {
return;
}
chrome.tabs.query({ highlighted: true }, function (tab) {
chrome.tabs.update(tab.id, { url });
});
});
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo) {
if (changeInfo.url) {
const { owner, repoName, fullName } = parseRepoFromUrl(changeInfo.url);
if (!owner || !repoName || !fullName) {
return;
}
chrome.storage.local.get(REPO_MAP_LOCAL_STORAGE_KEY, function (storageObj) {
const repoMap = storageObj.repoMap;
const didUpdateMap = addRepoToMap(repoMap, owner, repoName, fullName);
if (didUpdateMap) {
console.log('Adding ' + fullName + ' to repoMap in local storage');
chrome.storage.local.set({ repoMap: repoMap }, function () {
console.log('RepoMap updated successfully in local storage');
});
}
});
}
});
//#endregion