Greasemonkey \JavaScript复制到剪贴板按钮 [英] Greasemonkey\JavaScript Copy to Clipboard button
问题描述
我正在尝试编写一个JavaScript脚本来添加到添加按钮后的按钮上。该按钮的onClick应该将父母元素文本复制到键盘。我已经看到很多例子,将已经选择的文本复制到剪贴板中,例如:
< SCRIPT LANGUAGE =JavaScript >
<! - 开始
函数copyit(theField){
var selectedText = document.selection;
if(selectedText.type =='Text'){
var newRange = selectedText.createRange();
theField.focus();
theField.value = newRange.text;
} else {
alert('Alert:Select the text in the textarea then then click on this button');
}
}
//结束 - >
< / script>
< input onclick =copyit(this.form.text_select)type =buttonvalue =单击此处复制突出显示的文本name =copy_button>
找到 here 。
我也发现你可以在输入元素中选择文本。我尝试过将这两种技术以及许多其他方法结合起来,但还没有可行的解决方案。我甚至不知道为什么上面的代码复制到剪贴板。有没有人有这个解决方案?如果您花时间阅读完整的文章,作者指出这不适用于Firefox ... b
实际上,我认为它甚至不能用于IE,因为它与剪贴板没有任何关系!
有一种使用Flash的技术,因为默认情况下,出于安全原因,Firefox禁止剪贴板访问。
否则,传统的复制方式是:
$ p $ code> var tc = textToCopy.replace(/ \\\
\\\
/ g,'\\\
');
if(window.clipboardData)// IE
{
window.clipboardData.setData(Text,tc);
}
else
{
unsafeWindow.netscape.security.PrivilegeManager.enablePrivilege(UniversalXPConnect);
const clipboardHelper = Components.classes
[@ mozilla.org / widget / clipboardhelper; 1]。
getService(Components.interfaces.nsIClipboardHelper);
clipboardHelper.copyString(tc);
}
启用复制后(给定网站)。
I am trying to write a JavaScript script to add to greasemonkey that adds a button after an element. The onClick for this button should copy the parents element text to the keyboard. I have seen plenty of examples that copy already selected text to the clipboard such as this:
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function copyit(theField) {
var selectedText = document.selection;
if (selectedText.type == 'Text') {
var newRange = selectedText.createRange();
theField.focus();
theField.value = newRange.text;
} else {
alert('Alert: Select The text in the textarea then click on this button');
}
}
// End -->
</script>
<input onclick="copyit(this.form.text_select)" type="button" value="Click Here to Copy the Highlighted Text" name="copy_button">
Found here.
I have also found that you can select text in input elements. I have tried combining both techniques, as well as many others with no viable solution yet. I am not even sure why the code above copies to the clipboard. Does anyone have a solution to this?
If you took the time to read the full article, the author states this doesn't work for Firefox...
Actually, I think it doesn't even work for IE, as it does nothing related to the clipboard!
There is a technique using Flash, because by default, Firefox inhibits clipboard access for security reasons.
Otherwise, the classical way to do copy is:
var tc = textToCopy.replace(/\n\n/g, '\n');
if (window.clipboardData) // IE
{
window.clipboardData.setData("Text", tc);
}
else
{
unsafeWindow.netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
const clipboardHelper = Components.classes
["@mozilla.org/widget/clipboardhelper;1"].
getService(Components.interfaces.nsIClipboardHelper);
clipboardHelper.copyString(tc);
}
after enabling copy (for a given site).
这篇关于Greasemonkey \JavaScript复制到剪贴板按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!