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

Unified Diff: tools/chrome_extensions/open_my_editor/ext/background.js

Issue 2720793005: Add Open my editor (Closed)
Patch Set: add owners and change to py2 Created 3 years, 10 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 side-by-side diff with in-line comments
Download patch
Index: tools/chrome_extensions/open_my_editor/ext/background.js
diff --git a/tools/chrome_extensions/open_my_editor/ext/background.js b/tools/chrome_extensions/open_my_editor/ext/background.js
new file mode 100644
index 0000000000000000000000000000000000000000..3a98c86db261aa214815c047fe49c67afcded686
--- /dev/null
+++ b/tools/chrome_extensions/open_my_editor/ext/background.js
@@ -0,0 +1,110 @@
+// Copyright 2017 Google Inc.
watk 2017/03/02 23:13:49 Is this Google copyright intended? Never seen this
+//
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+function get_query(uri, key) {
+ if (uri.includes('?')) {
+ let query_str = uri.split('?')[1];
+ let queries = query_str.split('&');
+ for (let query of queries) {
+ let ss = query.split('=');
+ if (ss.length == 2 && ss[0] == key)
+ return ss[1];
+ }
+ }
+ return undefined;
+}
+
+function open_by_selection(selectionText) {
+ if (selectionText)
+ fetch('http://127.0.0.1:8989/file?f=' + selectionText + '&l=1');
+}
+
+function open_by_link(pageUrl, info, tabId) {
+ if (pageUrl.startsWith('https://cs.chromium.org/')) {
+ if (info.linkUrl.startsWith('https://cs.chromium.org/chromium/src/')) {
+ let filepath =
+ info.linkUrl.replace('https://cs.chromium.org/chromium/src/', '')
+ .replace(/\?.*/, '');
+ let line = get_query(info.linkUrl, 'l');
+ line = line != undefined ? line : '1';
+ fetch('http://127.0.0.1:8989/file?f=' + filepath + '&l=' + line);
+ }
+ } else if (pageUrl.startsWith('https://codereview.chromium.org/')) {
+ if (info.linkUrl.match('https://codereview.chromium.org/.*/patch/') !=
+ null) {
+ chrome.tabs.sendMessage(tabId, 'getFile', (res) => {
+ if (res.file)
+ fetch('http://127.0.0.1:8989/file?f=' + res.file + '&l=1');
+ });
+ } else if (
+ info.linkUrl.match(
+ /https:\/\/codereview.chromium.org\/\d*\/diff\/\d*\//) != null) {
+ let filepath = info.linkUrl.replace(
+ /https:\/\/codereview.chromium.org\/\d*\/diff\/\d*\//, '');
+ fetch('http://127.0.0.1:8989/file?f=' + filepath + '&l=1');
+ }
+ }
+}
+
+function cs_open_by_current_line(tabId, url) {
+ chrome.tabs.sendMessage(tabId, 'getLine', (res) => {
+ let line = res.line;
+
+ let filepath = url.replace('https://cs.chromium.org/chromium/src/', '')
+ .replace(/\?.*/, '');
+
+ fetch('http://127.0.0.1:8989/file?f=' + filepath + '&l=' + line);
+ });
+}
+
+function cr_open_all_in_patchset(tabId) {
+ chrome.tabs.sendMessage(tabId, 'getFiles', (res) => {
+ 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
+ });
+}
+
+chrome.contextMenus.onClicked.addListener((info, tab) => {
+ if (info.menuItemId == 'ome-selection') {
+ open_by_selection(info.selectionText);
+ } else if (info.menuItemId == 'ome-link') {
+ open_by_link(tab.url, info, tab.id);
+ } else if (info.menuItemId == 'ome') {
+ if (tab.url.startsWith('https://cs.chromium.org/chromium/src/')) {
+ cs_open_by_current_line(tab.id, tab.url);
+ } else if (tab.url.startsWith('https://codereview.chromium.org/')) {
+ cr_open_all_in_patchset(tab.id);
+ }
+ }
+});
+
+chrome.runtime.onInstalled.addListener(() => {
+ chrome.contextMenus.create({
+ 'title': 'Open My Editor',
+ 'id': 'ome',
+ 'contexts': ['page'],
+ 'documentUrlPatterns': [
+ 'https://cs.chromium.org/chromium/src/*',
+ 'https://codereview.chromium.org/*'
+ ]
+ });
+ chrome.contextMenus.create({
+ 'title': 'Open My Editor by Link',
+ 'id': 'ome-link',
+ 'contexts': ['link'],
+ 'documentUrlPatterns':
+ ['https://cs.chromium.org/*', 'https://codereview.chromium.org/*']
+ });
+ chrome.contextMenus.create({
+ 'title': 'Open My Editor for "%s"',
+ 'id': 'ome-selection',
+ 'contexts': ['selection'],
+ 'documentUrlPatterns': [
+ // TODO(chaopeng) Should be only except CS and CR, But I dont know how to.
+ // So only list the sites here.
+ 'https://build.chromium.org/*', 'https://chromium.org/*'
+ ]
+ });
+});

Powered by Google App Engine
This is Rietveld 408576698