在 Firefox 和 google chrome 上复制到剪贴板

发布于 2024-10-06 00:15:59 字数 381 浏览 5 评论 0原文

因为我环顾四周,找不到任何将 Firefox 或 Chrome 上的文本复制到剪贴板的好解决方案。但是,我尝试了 Firefox 在其开发者站点中提供的一些代码,但仍然无法正常工作,并且出现一个错误,权限被拒绝。这是我在最后一刻尝试的代码。

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

有没有人有一个好的解决方案来处理这个问题?我将感谢您的分享。谢谢。

since I have looked around and could not find any nice solution for copying text on firefox or chrome to clipboard. However, I have tried some codes provide by firefox in its developer site, but still not work and there was one errror with permission denied. Here is the code I tried the last minute.

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

Does anyone have a good solution to deal with this? I would appreciate for your sharing. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挽清梦 2024-10-13 00:15:59

我想这不仅仅是为了你看?

如果没有,您可以在 Firefox 浏览器中调整 about:config 中的设置。在过滤器中查找“signed”,并将单个结果设置为“禁用”。

然而,如果您想要整个事情的代码,那就有点麻烦了,因为 Firefox 对此有很好的安全保护。一种棘手的方法是使用 Flash 对象将字符串传递给,然后使用 Flash 复制到剪贴板:)

I take it it's for more than just you viewing?

If not, you can adjust the setting in about:config in your Firefox browser. Look for 'signed' in the filter, and set the single result to DISABLED.

However, if you want code for the whole thing, it's tricker as Firefox is quite well secured against that. One tricky way is to use a Flash object to pass the string to, and then use Flash to copy to the clipboard :)

謸气贵蔟 2024-10-13 00:15:59

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

在按键处理程序上创建“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;

在粘贴时创建文本区域并且工作相同。

抱歉英语不好——不是我的母语。

I am found next solution:

On key down handler create "pre" tag. Set content to copy to this tag. Make Selection on this tag and return true in handler. This call standard handler of chrome and copied selected text.

And if u need u may be set timeout for function for restoring previous selection. My implementantions on 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. :(');
        };
    }

usage:

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;

On paste its create textarea and work same.

Sorry for bad English - not my native language.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文