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