Confirmed users
764
edits
No edit summary |
|||
Line 79: | Line 79: | ||
; function : An arbitrary predicate. The context arises when the function returns true. | ; function : An arbitrary predicate. The context arises when the function returns true. | ||
=== Example Usage === | |||
<pre class="brush:js;toolbar:false;"> | |||
let contextMenu = require("context-menu"); | |||
// Add "Edit Image" on images. | |||
contextMenu.add("img", { | |||
label: "Edit Image", | |||
command: function (menuitem, context) { | |||
editImage(context.node.src); | |||
} | |||
}); | |||
// Replace the "Search" menuitem with a menu that searches Google or Wikipedia | |||
// when there is selected text. | |||
let selection = require("selection"); | |||
contextMenu.replace( | |||
function () { | |||
return !!selection.text; | |||
}, | |||
"Search", | |||
{ | |||
label: "Search", | |||
menu: [ | |||
{ label: "Google", data: "http://www.google.com/search?q=" }, | |||
{ label: "Wikipedia", data: "http://en.wikipedia.org/wiki/" } | |||
] | |||
command: function (menuitem, context) { | |||
context.window.location.href = menuitem.data + selection.text; | |||
} | |||
} | |||
); | |||
// Add "Edit Page Source" under the universal context. | |||
contextMenu.add( | |||
function () { | |||
return true; | |||
}, | |||
{ | |||
label: "Edit Page Source", | |||
command: function (menuitem, context) { | |||
editSource(context.document.URL); | |||
} | |||
} | |||
); | |||
// Add "Edit Page Images" when the page contains at least one image. | |||
contextMenu.add( | |||
function (context) { | |||
return !!context.document.querySelector("img"); | |||
}, | |||
{ | |||
label: "Edit Page Images", | |||
command: function (menuitem, context) { | |||
editPageImages(context.document.URL); | |||
} | |||
} | |||
); | |||
</pre> |