复制到Firefox扩展中的剪贴板不起作用 [英] Copy to clipboard in Firefox extension not working
问题描述
我发现这个链接 MDN使用剪贴板进行剪贴板复制,但不是加工。当我尝试执行cfx运行总是我得到这个错误信息
(c:\\\\\\\\\\\\\\\\\\\ -1.17)
c:\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ Firefox \ExternalOpener\lib\main.js:11:const
gClipboardHelper = Components.classes [@ mozilla.org/widget/clipboard
helper; 1]。getService(Components.interfaces .nsIClipboardHelper);使用
'Components'来访问chrome权限。要做到这一点,您需要添加一个
行,如下所示:
$ bconst {Cc,Ci} = require(chrome); p>
然后,您可以使用任何快捷方式来从
导入属性的'chrome'模块('Cc','Ci','Cm','Cr
'class','interfaces','manager','results'和'utils'属性,
和组件。
$ p
$ b $ p $这是我的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);
}
});
欢迎任何想法,关于使用参数执行剪贴板复制或外部应用程序...
谢谢 解决方案
该文档属于基于XUL的插件,重新写作基于一个 Addon-SDK 。为了使用 c>在你的基于sdk的插件中,你需要 Chrome Authority < a>。 这个错误告诉你:Components.classes和Components.interfaces没有定义。为了使用你必须首先要求他们: Component.classes
和 Component.interfaces $ c $ <
$ b $ pre code> const {Cc,Ci} = require(chrome);
然后以这种方式使用它们:
const gClipboardHelper = Cc [@ mozilla.org/widget/clipboardhelper;1\"].getService(Ci.nsIClipboardHelper);
gClipboardHelper.copyString(selectionText);
Cc 代表 Component.classes
和 Ci (
Components.interfaces
)。请阅读有关Chrome授权的文档以了解它们和其他属性;)
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
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 ;)
这篇关于复制到Firefox扩展中的剪贴板不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!