Skip to content Skip to sidebar Skip to footer

Copy To Clipboard In Firefox Extension Not Working

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 c

Solution 1:

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 ;)

Solution 2:

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();

Post a Comment for "Copy To Clipboard In Firefox Extension Not Working"