javascript – 在firefox和谷歌浏览器上复制到剪贴板

因为我环顾四周,找不到任何好的解决方案,可以将firefox或chrome上的文本复制到剪贴板.但是,我已经尝试了firefox在其开发者网站上提供的一些代码,但仍然无法正常工作,并且有一个错误被拒绝.这是我最后一分钟尝试的代码.

var copytext = "Text to copy";  
var str      = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);  
str.data     = copytext; 

有没有人有一个很好的解决方案来处理这个问题?我很感激你的分享.谢谢.

解决方法:

我找到了下一个解决方案:

在键向下处理程序上创建“pre”标记.设置要复制到此标记的内容.对此标记进行选择并在处理程序中返回true.此调用chrome的标准处理程序并复制所选文本.

如果你需要你可以设置超时功能恢复以前的选择.我对Mootools的实施:

   function EnybyClipboard() {
        this.saveSelection = false;
        this.callback = false;
        this.pastedText = false;

        this.restoreSelection = function () {
            if (this.saveSelection) {
                window.getSelection().removeAllRanges();        
                for (var i = 0; i < this.saveSelection.length; i++) {
                    window.getSelection().addRange(this.saveSelection[i]);
                }
                this.saveSelection = false;
            }
        };

        this.copyText = function (text) {
            var div = $('special_copy');
            if (!div) {
                div = new Element('pre', {'id' : 'special_copy', 'style': 'opacity: 0;position: absolute;top: -10000px;right: 0;'});
                div.injectInside(document.body);
            }
            div.set('text', text);
            if (document.createRange) {
                var rng = document.createRange();
                rng.selectNodeContents(div);
                this.saveSelection = [];
                var selection = window.getSelection();
                for (var i = 0; i < selection.rangeCount; i++) {
                    this.saveSelection[i] = selection.getRangeAt(i);
                }
                window.getSelection().removeAllRanges();
                window.getSelection().addRange(rng);
                setTimeout(this.restoreSelection.bind(this), 100);
            } else
                return alert('Copy not work. :(');
        };

        this.getPastedText = function () {
            if (!this.pastedText) 
                alert('Nothing to paste. :(');
            return this.pastedText;
        };

        this.pasteText = function (callback) {
            var div = $('special_paste');
            if (!div) {
                div = new Element('textarea', {'id' : 'special_paste', 'style': 'opacity: 0;position: absolute;top: -10000px;right: 0;'});
                div.injectInside(document.body);
                div.addEvent('keyup', function() {
                    if (this.callback) {
                        this.pastedText = $('special_paste').get('value');
                        this.callback.call(this.pastedText);
                        this.callback = false;
                        this.pastedText = false;
                        setTimeout(this.restoreSelection.bind(this), 100);
                    }
                }.bind(this));
            }
            div.set('value', '');
            if (document.createRange) {
                var rng = document.createRange();
                rng.selectNodeContents(div);
                this.saveSelection = [];
                var selection = window.getSelection();
                for (var i = 0; i < selection.rangeCount; i++) {
                    this.saveSelection[i] = selection.getRangeAt(i);
                }
                window.getSelection().removeAllRanges();
                window.getSelection().addRange(rng);
                div.focus();
                this.callback = callback;
            } else
                return alert('Fail to paste. :(');
        };
    }

用法:

enyby_clip = new EnybyClipboard(); //init 

enyby_clip.copyText('some_text'); // place this in CTRL+C handler and return true;

enyby_clip.pasteText(function callback(pasted_text) {
        alert(pasted_text);
}); // place this in CTRL+V handler and return true;

在粘贴它创建textarea并且工作相同.

抱歉英语不好 – 不是我的母语.

上一篇:Gvim/Vim 配置好了常用插件(Windows 与 Linux 通用)


下一篇:c# – 剪贴板监视器