Reputation: 95
I want to write a Firefox extension, with copy to clipboard and execute an external program with selected text parameter. I have found this link MDN Using Clipboard for clipboard copy, but is not working. When I try to execute with cfx run always I get this error message
(c:\Works\Firefox\addon-sdk-1.17) c:\Works\Firefox\ExternalOpener>cfx run
The following lines from file c:\Works\Firefox\ExternalOpener\lib\main.js: 11: const gClipboardHelper = Components.classes["@mozilla.org/widget/clipboard helper;1"].getService(Components.interfaces.nsIClipboardHelper); use 'Components' to access chrome authority. To do so, you need to add a line somewhat like the following:
const {Cc,Ci} = require("chrome");
Then you can use any shortcuts to its properties that you import from the 'chrome' module ('Cc', 'Ci', 'Cm', 'Cr', and 'Cu' for the 'classes', 'interfaces', 'manager', 'results', and 'utils' properties, respectively. And
components
forComponents
object itself).
This is my main.js
var contextMenu = require("sdk/context-menu");
var menuItem = contextMenu.Item({
label: "Open in External",
context: contextMenu.SelectionContext(),
contentScript: 'self.on("click", function () {' +
' var text = window.getSelection().toString();' +
' self.postMessage(text);' +
'});',
onMessage: function (selectionText) {
console.log(selectionText);
const gClipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"].getService(Components.interfaces.nsIClipboardHelper);
gClipboardHelper.copyString(selectionText);
}
});
Any idea is welcome, about clipboard copy or external application execution with a parameter...
Thanks
Upvotes: 1
Views: 1380
Reputation: 8065
I think this is exactly what you're looking for: https://developer.mozilla.org/en-US/Add-ons/SDK/High-Level_APIs/clipboard
You can use it like this:
var clipboard = require("sdk/clipboard");
clipboard.set("Lorem ipsum dolor sit amet");
var contents = clipboard.get();
Upvotes: 0
Reputation: 6206
That doc belongs to XUL based addons, while the addon you're writting is an Addon-SDK based one.
In order to use Component.classes
and Component.interfaces
in your sdk-based addons, you need Chrome Authority.
What that error is telling you is that Components.classes and Components.interfaces are no defined. In order to use the you must first require them:
const {Cc, Ci} = require("chrome");
And then use them this way:
const gClipboardHelper =Cc["@mozilla.org/widget/clipboardhelper;1"].getService(Ci.nsIClipboardHelper);
gClipboardHelper.copyString(selectionText);
Cc stands for Component.classes
, and Ci for Components.interfaces
. Please read the doc about Chrome Authority to understand them and the other properties ;)
Upvotes: 2