Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(219)

Side by Side Diff: tools/chrome_extensions/open_my_editor/ext/background.js

Issue 2720793005: Add Open my editor (Closed)
Patch Set: change python2 to python Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 function get_query(uri, key) {
6 if (uri.includes('?')) {
7 let query_str = uri.split('?')[1];
8 let queries = query_str.split('&');
9 for (let query of queries) {
10 let ss = query.split('=');
11 if (ss.length == 2 && ss[0] == key)
12 return ss[1];
13 }
14 }
15 return undefined;
16 }
17
18 function open_by_selection(selectionText) {
19 if (selectionText)
20 fetch('http://127.0.0.1:8989/file?f=' + selectionText + '&l=1');
21 }
22
23 function open_by_link(pageUrl, info, tabId) {
24 if (pageUrl.startsWith('https://cs.chromium.org/')) {
25 if (info.linkUrl.startsWith('https://cs.chromium.org/chromium/src/')) {
26 let filepath =
27 info.linkUrl.replace('https://cs.chromium.org/chromium/src/', '')
28 .replace(/\?.*/, '');
29 let line = get_query(info.linkUrl, 'l');
30 line = line != undefined ? line : '1';
31 fetch('http://127.0.0.1:8989/file?f=' + filepath + '&l=' + line);
32 }
33 } else if (pageUrl.startsWith('https://codereview.chromium.org/')) {
34 if (info.linkUrl.match('https://codereview.chromium.org/.*/patch/') !=
35 null) {
36 chrome.tabs.sendMessage(tabId, 'getFile', (res) => {
37 if (res.file)
38 fetch('http://127.0.0.1:8989/file?f=' + res.file + '&l=1');
39 });
40 } else if (
41 info.linkUrl.match(
42 /https:\/\/codereview.chromium.org\/\d*\/diff\/\d*\//) != null) {
43 let filepath = info.linkUrl.replace(
44 /https:\/\/codereview.chromium.org\/\d*\/diff\/\d*\//, '');
45 fetch('http://127.0.0.1:8989/file?f=' + filepath + '&l=1');
46 }
47 }
48 }
49
50 function cs_open_by_current_line(tabId, url) {
51 chrome.tabs.sendMessage(tabId, 'getLine', (res) => {
52 let line = res.line;
53
54 let filepath = url.replace('https://cs.chromium.org/chromium/src/', '')
55 .replace(/\?.*/, '');
56
57 fetch('http://127.0.0.1:8989/file?f=' + filepath + '&l=' + line);
58 });
59 }
60
61 function cr_open_all_in_patchset(tabId) {
62 chrome.tabs.sendMessage(tabId, 'getFiles', (res) => {
63 fetch('http://127.0.0.1:8989/files?f=' + res.files.join(',,'));
64 });
65 }
66
67 chrome.contextMenus.onClicked.addListener((info, tab) => {
68 if (info.menuItemId == 'ome-selection') {
69 open_by_selection(info.selectionText);
70 } else if (info.menuItemId == 'ome-link') {
71 open_by_link(tab.url, info, tab.id);
72 } else if (info.menuItemId == 'ome') {
73 if (tab.url.startsWith('https://cs.chromium.org/chromium/src/')) {
74 cs_open_by_current_line(tab.id, tab.url);
75 } else if (tab.url.startsWith('https://codereview.chromium.org/')) {
76 cr_open_all_in_patchset(tab.id);
77 }
78 }
79 });
80
81 chrome.runtime.onInstalled.addListener(() => {
82 chrome.contextMenus.create({
83 'title': 'Open My Editor',
84 'id': 'ome',
85 'contexts': ['page'],
86 'documentUrlPatterns': [
87 'https://cs.chromium.org/chromium/src/*',
88 'https://codereview.chromium.org/*'
89 ]
90 });
91 chrome.contextMenus.create({
92 'title': 'Open My Editor by Link',
93 'id': 'ome-link',
94 'contexts': ['link'],
95 'documentUrlPatterns':
96 ['https://cs.chromium.org/*', 'https://codereview.chromium.org/*']
97 });
98 chrome.contextMenus.create({
99 'title': 'Open My Editor for "%s"',
100 'id': 'ome-selection',
101 'contexts': ['selection'],
102 'documentUrlPatterns': [
103 // TODO(chaopeng) Should be only except CS and CR, But I dont know how to.
104 // So only list the sites here.
105 'https://build.chromium.org/*', 'https://chromium.org/*'
106 ]
107 });
108 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698