Back to XUL: Completing the JavaScript Logic
(Page 1 of 6 )
The previous article saw us complete the
File menu, making use of XPCOM to handle some of the more complex behaviors. This article will go on to discuss adding behavior to the two remaining menus, the
Edit and
Help menus.
A downloadable file for this article is available
here.
The Edit menu is at this stage very basic, having just Copy, Paste and Cut actions to script. As before, we are going to have to use XPCOM to enable this. Compared to some of the previous functions we have used, the first one is relatively simple. First, declare the function and get the editor element:
function copyToClip() {
var editor = document.getElementById('mainContent');
In order to copy some text from the editor, some text will need to be selected and you will need to obtain whatever is selected:
var selected = editor.contentDocument.getSelection();
Once you have the selection, it is simple to copy this to the OS clipboard using the copy() method, but to do this, you need to get the interface that supports it, which is the nsIPlaintextEditor. Once you have the interface, you can call the method and close the function:
editor = editor.getEditor(editor.contentWindow).QueryInterface
(Components.interfaces.nsIPlaintextEditor);
editor.copy(selected);
}
There’s no need to use the CI constant to save on code as only one interface is used in this operation.
Next: Defining a Function >>
More XML Articles
More By Dan Wellman