Skip to content
This repository has been archived by the owner on Jan 4, 2019. It is now read-only.

Commit

Permalink
Add chrome.contextMenus.create and chrome.contextMenus.removeAll
Browse files Browse the repository at this point in the history
  • Loading branch information
darkdh committed Oct 28, 2016
1 parent 9b2f304 commit 7e68e3e
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 8 deletions.
50 changes: 42 additions & 8 deletions atom/common/api/resources/context_menus_bindings.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,49 @@
var ipc = require('ipc_utils');
var process = requireNative('process');
var extensionId = process.GetExtensionId();
var ipc = require('ipc_utils')
var process = requireNative('process')
var extensionId = process.GetExtensionId()

var id = 0

var createMenuItemId = () => {
return 'context-menus-' + extensionId + id++
}

var createResponseId = () => {
return extensionId + id++
}

var binding = {
removeAll: function () {
console.warn('chrome.contentMenus.removeAll is not supported yet')
onClicked: {
addListener: function (cb) {
ipc.on('chrome-context-menus-clicked', function(evt, info, tab) {
cb(info, tab)
})
},
removeListener: function (cb) {
// TODO
// ipc.off('chrome-context-menus-clicked', cb)
}
},
removeAll: function (cb) {
var responseId = createResponseId()
cb && ipc.once('chrome-context-menus-remove-all-response-' + responseId, function(evt) {
cb()
})
ipc.send('chrome-context-menus-remove-all', responseId, extensionId)
},
create: function (properties, cb) {
console.warn('chrome.contentMenus.create is not supported yet')
cb && cb()
var responseId = createResponseId()
var menuItemId = properties.id || createMenuItemId()
cb && ipc.once('chrome-context-menus-create-response-' + responseId, function(evt) {
cb()
})
properties.onclick && ipc.on('chrome-context-menus-clicked', (evt, info, tab) => {
if (info.menuItemId === menuItemId) {
properties.onclick(info, tab)
}
})
ipc.send('chrome-context-menus-create', responseId, extensionId, menuItemId, properties)
}
}

exports.binding = binding;
exports.binding = binding
18 changes: 18 additions & 0 deletions lib/browser/api/extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ process.on('EXTENSION_READY_INTERNAL', (installInfo) => {
addBackgroundPageEvent(installInfo.id, 'chrome-browser-action-clicked')
process.emit('chrome-browser-action-registered', installInfo.id, details)
}
addBackgroundPageEvent(installInfo.id, 'chrome-context-menus-clicked')
})

// TODO(bridiver) move these into modules
Expand Down Expand Up @@ -611,3 +612,20 @@ ipcMain.on('autofill-popup-hidden', function (evt, tabId) {
if (webContents)
webContents.autofillPopupHidden()
})

ipcMain.on('chrome-context-menus-remove-all', function (evt, responseId, extensionId) {
process.emit('chrome-context-menus-remove-all', extensionId)
evt.sender.send('chrome-context-menus-remove-all-response-' + responseId)
})

ipcMain.on('chrome-context-menus-create', function (evt, responseId, extensionId, menuItemId, properties) {
process.emit('chrome-context-menus-create', extensionId, menuItemId, properties)
evt.sender.send('chrome-context-menus-create-response-' + responseId)
})

process.on('chrome-context-menus-clicked', function (extensionId, tabId, info) {
let response = getTabValue(tabId)
if (response) {
sendToBackgroundPages(extensionId, getSessionForTab(tabId), 'chrome-context-menus-clicked', info, response)
}
})

0 comments on commit 7e68e3e

Please sign in to comment.