-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
263 lines (218 loc) · 5.92 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
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
const CHROMIUM_HEADER = 'https://source.chromium.org';
const GERRIT_HEADER = 'https://chromium-review.googlesource.com';
const GOOGLE_GIT_HEADER = 'https://chromium.googlesource.com';
const CODE_REVIEWS_HEADER = 'https://codereview.chromium.org';
const WEBDIFF_HEADER = 'http://localhost';
class ChromiumHandler {
// https://source.chromium.org/chromium/chromium/src/+/main:base/values.h;l=104
constructor(url, tabTitle) {
// Default to success.
this.success = true;
this.url = url;
url = url.split(':')[2];
if (!url) return;
// Get the file path.
let file = url.split(';')[0];
if (!file) {
console.log('Cannot get file path!');
return;
}
this.file = file;
// Get the line number.
// Default to the first line.
this.line = '1';
// Case 1: values.h[;bpv=1;bpt=0]
let line = url.split(';l=')[1];
if (!line) {
return;
}
// Case 2: values.h;l=104[;bpv=1;bpt=0]
line = line.split(';')[0];
if (!line) {
return;
}
// Case 3: values.h;l=104[;bpv=1;bpt=0]?q=values&ss=chromium%2Fchromium%2Fsrc
line = line.split('?')[0];
if (!line) {
return;
}
this.line = line;
}
}
class GerritHandler {
// https://chromium-review.googlesource.com/c/chromium/src/+/3086892/10/chrome/browser/sync/test/integration/two_client_web_apps_bmo_sync_test.cc#646
constructor(url, tabTitle) {
this.url = url;
url = url.split('/+/')[1];
if (!url) return;
// Get the file path
const chunk = url.split('#')[0];
const SEARCH_TERM = '/';
let indexOfFirst = chunk.indexOf(SEARCH_TERM);
const NOT_FIND = -1;
if (indexOfFirst == NOT_FIND) {
console.log('First index of' + SEARCH_TERM + ' not find!');
return;
}
let indexOfSecond = chunk.indexOf(SEARCH_TERM, indexOfFirst + 1);
if (indexOfSecond == NOT_FIND) {
console.log('Second index of' + SEARCH_TERM + ' not find!');
return;
}
let file = chunk.substr(indexOfSecond + 1);
if (!file) return;
// Get the line number.
let line = url.split('#')[1];
if (line) {
// If chose from left diff view on Gerrit, there will be a 'b' in
// the line number which should get removed.
line = line.replace('b', '');
} else {
// Default to open and locate at the first line.
line = '1';
}
this.file = file;
this.line = line;
this.success = true;
}
}
class GoogleGitHandler {
// https://chromium.googlesource.com/chromium/src/+/HEAD/chrome/browser/extensions/app_process_apitest.cc#291
constructor(url, tabTitle) {
this.url = url;
url = url.split('/+/')[1];
if (!url) return;
// Get the file path
const chunk = url.split('#')[0];
const SEARCH_TERM = '/';
let indexOfFirst = chunk.indexOf(SEARCH_TERM);
const NOT_FIND = -1;
if (indexOfFirst == NOT_FIND) {
console.log('First index of' + SEARCH_TERM + ' not find!');
return;
}
let file = chunk.substr(indexOfFirst + 1);
if (!file) return;
// Get the line number.
let line = url.split('#')[1];
if (!line)
line = '1';
this.file = file;
this.line = line;
this.success = true;
}
}
class CodeReviewsHandler {
// https://chromiumcodereview.appspot.com/2433563002/diff/100001/components/sync/protocol/proto_value_conversions.cc
constructor(url, tabTitle) {
this.url = url;
if (!tabTitle) {
console.error('Cannot get current Tab Title!');
return;
}
// Get the file path.
var file = tabTitle.split(' ')[0];
if (!file) {
console.error('Cannot get file path!');
return;
}
this.file = file;
// Get the line number.
// Default to the first line.
this.line = '1';
this.success = true;
console.log('Tab Title: ', tabTitle);
}
}
class WebdiffHandler {
// http://localhost:52789/tools/chrome_extensions/open_my_editor/README.md
constructor(url, tabTitle) {
this.url = url;
url = url.split(':')[2];
if (!url) return;
// Get the file path
const chunk = url;
const SEARCH_TERM = '/';
let indexOfFirst = chunk.indexOf(SEARCH_TERM);
const NOT_FIND = -1;
if (indexOfFirst == NOT_FIND) {
console.log('First index of' + SEARCH_TERM + ' not find!');
return;
}
let file = chunk.substr(indexOfFirst + 1);
if (!file) return;
// Get the line number.
// Default to the first line.
let line = '1';
this.file = file;
this.line = line;
this.success = true;
}
}
function GetHandler(url) {
if (url.startsWith(CHROMIUM_HEADER)) {
return ChromiumHandler;
}
if (url.startsWith(GERRIT_HEADER)) {
return GerritHandler;
}
if (url.startsWith(GOOGLE_GIT_HEADER)) {
return GoogleGitHandler;
}
if (url.startsWith(CODE_REVIEWS_HEADER)) {
return CodeReviewsHandler;
}
if (url.startsWith(WEBDIFF_HEADER)) {
return WebdiffHandler;
}
console.log('Not support website: ' + url);
return null;
}
function Json2Get(json){
let str = `f=${json.file}&l=${json.line}`;
return str;
}
function RequestOpen(url, tabTitle) {
let handlerClass = GetHandler(url);
if (!handlerClass) return;
let handler = new handlerClass(url, tabTitle);
if (!handler || !handler.success) return;
console.log('send', handler);
let param = Json2Get(handler);
console.log(param);
let reqUrl = 'http://127.0.0.1:8989/file?' + param;
fetch(reqUrl)
.then((response) => {
console.log('Response: ', response);
})
.catch((error) => {
console.log('Error: ', error);
});
};
function handleContextMenusClick(info, tab) {
switch (info.menuItemId) {
case 'open_in_editor':
let url = info.linkUrl;
if (url == undefined) {
url = info.pageUrl;
}
if (url) {
console.log(url, "start open");
RequestOpen(url, tab.title);
}
console.log(info);
console.log(tab);
break;
};
}
chrome.runtime.onInstalled.addListener((_) => {
chrome.contextMenus.create({
id: 'open_in_editor',
title: 'Open in Editor',
contexts: ['link', 'selection']
});
// This will get triggered when service worker is active. [fast]
chrome.contextMenus.onClicked.addListener(handleContextMenusClick);
});
// This will get triggered when service worker is inactive. [slow]
chrome.contextMenus.onClicked.addListener(handleContextMenusClick);