javascript 如何使用 Greasemonkey 将数据复制到剪贴板?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13075645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to copy data to the clipboard with Greasemonkey?
提问by LA_
I found this questionbut trying to use the code given there:
我发现了这个问题,但试图使用那里给出的代码:
unsafeWindow.netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
const clipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"]
.getService(Components.interfaces.nsIClipboardHelper)
;
clipboardHelper.copyString('test');
Gives the error message:
给出错误信息:
A script from ?http://example.com? was denied UniversalXPConnect privileges.
来自 ? http://example.com?被拒绝 UniversalXPConnect 权限。
I also tried to use (from Mozilla site):
我也尝试使用(来自 Mozilla 网站):
const gClipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"]
.getService(Components.interfaces.nsIClipboardHelper)
;
gClipboardHelper.copyString("test2");
Which gives the error message: Components.classes is undefined
.
这给出了错误消息:Components.classes is undefined
。
In both cases it doesn't work with the latest Firefox on Windows 7.
What else should I try?
在这两种情况下,它都不适用于 Windows 7 上的最新 Firefox。
我还应该尝试什么?
回答by Brock Adams
Update:
更新:
As of version 1.10 (June 20, 2013), Greasemonkey now supports the GM_setClipboard()
function.
Use like so:
随着1.10版(2013 6月20日),Greasemonkey的现在支持的GM_setClipboard()
功能。
像这样使用:
// ==UserScript==
// @name _YOUR_SCRIPT_NAME
// @include http://YOUR_SERVER.COM/YOUR_PATH/*
// @grant GM_setClipboard
// ==/UserScript==
GM_setClipboard ("The clipboard now contains this sentence.");
Older GM versions:
较旧的 GM 版本:
This is very difficult to do with Greasemonkey since GM devs refuse to support it and FF and Flash security settings must be overcome.
You can do it if the copy will be initiated by a manual click. In which case, use one of the techniques from this question.
Greasemonkey 很难做到这一点,因为 GM 开发人员拒绝支持它,并且必须克服 FF 和 Flash 安全设置。
如果复制将通过手动单击启动,则您可以执行此操作。在这种情况下,请使用此问题中的一种技术。
If you wish to have some kind of fully-automatic clipboard operation, then you will have to develop your own add-on or plugin for your GM script to use.
如果您希望拥有某种全自动剪贴板操作,那么您必须开发自己的附加组件或插件供您的 GM 脚本使用。
Scriptish has supported this for years:
Scriptish 多年来一直支持这一点:
If you are open to switching scripting add-ons, note that Scriptishprovides GM_setClipboard()
to set the clipboard. (But no function to read it?!)
如果您愿意切换脚本加载项,请注意Scriptish提供GM_setClipboard()
设置剪贴板。(但没有读取它的功能?!)
Scriptish is not perfect, but it is better than Greasemonkey in a few ways. (Note I'm not affiliated with either add-on, nor am I completely happy with either.)
Scriptish 并不完美,但在某些方面它比 Greasemonkey 更好。(请注意,我不隶属于任何一个附加组件,我也不完全满意。)
Most GM scripts will run in Scriptish with no problem.
大多数 GM 脚本都可以在 Scriptish 中运行,没有问题。
As for the code snippets, from the question; they are essentially the same thing (the first just accounts for the GM sandbox). That approach has been obsolete for many versions of Firefox and the replacement code is poorly documented.
You'll have to use techniques that require user interaction, or you'll have to write a custom helper add-on.
至于代码片段,来自问题;它们本质上是一样的(第一个只是考虑了 GM 沙箱)。对于许多版本的 Firefox 来说,这种方法已经过时了,而且替换代码的文档也很差。
您将不得不使用需要用户交互的技术,或者您将不得不编写自定义帮助程序附加组件。