From b1f0c2d1741ac15491136ab42415dbdcfd97efc1 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Thu, 3 Apr 2014 20:54:42 +0800 Subject: [PATCH] Override web inspector's context menu. --- atom.gyp | 1 + atom/renderer/lib/init.coffee | 9 +++++++-- atom/renderer/lib/inspector.coffee | 30 ++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 atom/renderer/lib/inspector.coffee diff --git a/atom.gyp b/atom.gyp index cb5256c..6215551 100644 --- a/atom.gyp +++ b/atom.gyp @@ -32,6 +32,7 @@ 'atom/common/api/lib/shell.coffee', 'atom/common/lib/init.coffee', 'atom/renderer/lib/init.coffee', + 'atom/renderer/lib/inspector.coffee', 'atom/renderer/lib/override.coffee', 'atom/renderer/api/lib/ipc.coffee', 'atom/renderer/api/lib/remote.coffee', diff --git a/atom/renderer/lib/init.coffee b/atom/renderer/lib/init.coffee index 33d7d20..e781ac4 100644 --- a/atom/renderer/lib/init.coffee +++ b/atom/renderer/lib/init.coffee @@ -1,4 +1,5 @@ path = require 'path' +url = require 'url' Module = require 'module' # Expose information of current process. @@ -42,5 +43,9 @@ else global.__filename = __filename global.__dirname = __dirname -# Override default web functions. -require path.join(__dirname, 'override') +if location.protocol is 'chrome-devtools:' + # Override some inspector APIs. + require path.join(__dirname, 'inspector') +else + # Override default web functions. + require path.join(__dirname, 'override') diff --git a/atom/renderer/lib/inspector.coffee b/atom/renderer/lib/inspector.coffee new file mode 100644 index 0000000..7030fdd --- /dev/null +++ b/atom/renderer/lib/inspector.coffee @@ -0,0 +1,30 @@ +# Use menu API to show context menu. +window.onload = -> + WebInspector.ContextMenu.prototype.show = -> + menuObject = @_buildDescriptor() + if menuObject.length + WebInspector._contextMenu = this + createMenu(menuObject, @_event) + @_event.consume() + +convertToMenuTemplate = (items) -> + template = [] + for item in items + if item.type is 'subMenu' + template.push + type: 'submenu' + label: item.label + submenu: convertToMenuTemplate item.subItems + else + template.push + type: 'normal' + label: item.label + template + +createMenu = (items, event) -> + remote = require 'remote' + Menu = remote.require 'menu' + + menu = Menu.buildFromTemplate convertToMenuTemplate(items.subItems) + menu.popup() + event.consume true -- 2.7.4