将页面的 HTML 片段复制到剪贴板

发布于 2024-12-21 06:03:22 字数 439 浏览 0 评论 0原文

我正在开发 Web 开发工具,我想使用 Javascript 将当前网页的 HTML 代码的一部分复制到剪贴板。

这可能涉及

  • 使用 DOM innerHTML 获取问题中的 HTML 片段

  • 使用 Javascript 将此文本复制到剪贴板

有人知道这里有什么陷阱吗?例如,与剪贴板处理相关 - 当不使用 documentEditable 模式时,我是否需要创建一个隐藏的位置来放置 HTML 有效负载以进行复制?

另外,如果可能的话,我希望与 TinyMCE 等 WYSIWYG 组件进行交互,这样当人们在可视化编辑模式下粘贴 HTML 时,它就会以格式化的 HTML 而不是纯文本的形式出现。

如果解决方案在 Chrome 和 Firefox 中有效就足够了。不需要支持 Internet Explorer。

I am developing web development tools and I'd like to copy of a part of current web page's HTML code to the clipboard using Javascript.

This probably would involve

  • Getting the piece of HTML in the question using DOM innerHTML

  • Copying this text to clipboard using Javascript

Is anyone aware of any gotchas here? E.g. related to clipboard handling - when one is not using documentEditable mode do I need to create a hidden where to put the HTML payload for copying?

Also if possible I'd like to make the interaction with WYSIWYG components, like TinyMCE, work so that when one pastes the HTML in the visual edit mode it comes through as formatted HTML instead of plain text.

It is enough if solution works in Chrome and Firefox. Internet Explorer does not need to be supported.

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

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

发布评论

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

评论(2

夜光 2024-12-28 06:03:22

JavaScript 无法将内容添加到剪贴板。好吧,至少没有任何可以跨浏览器工作的。

然而,有一种效果很好的闪存解决方案。 http://code.google.com/p/zeroclipboard/

Javascript has no way of adding things to the clipboard. Well at least not any that works cross browser.

There is however a flash solution which works well. http://code.google.com/p/zeroclipboard/

她比我温柔 2024-12-28 06:03:22

我们开发了一个小型 Firefox-AddOn,用于在从编辑器复制/粘贴内容时删除特殊字符(连字符)。这是必要的,因为没有 JavaScript 方法可以将任何内容填充到剪贴板中。我想也应该可以为 Chrome 编写一个扩展(googel 是你的朋友)。从我的角度来看,这似乎是获得你想要的东西的唯一方法。

示例:
这是 FireFox-Addon 删除特殊字符 onCopy 所需的代码片段

// get Clipboard Object
var clip  = Components.classes["@mozilla.org/widget/clipboard;1"].createInstance(Components.interfaces.nsIClipboard);

// get Transferable Object
    var tr_unicode = new Transferable(); 
var tr_html    = new Transferable();

// read "text/unicode flavors" (the clipboard has several "flavours" (html, plain text, ...))
    tr_unicode.addDataFlavor("text/unicode");
tr_html.addDataFlavor("text/html");
    clip.getData(tr_unicode, clip.kGlobalClipboard); // Systemclipboard
    clip.getData(tr_html, clip.kGlobalClipboard); // Systemclipboard

// generate objects to write the contents into (used for the clipboard)         
    var unicode = { }, ulen = { }, html = { }, hlen = { };

tr_html.getTransferData("text/html", html, hlen);
tr_unicode.getTransferData("text/unicode", unicode, ulen);

var unicode_obj = unicode.value.QueryInterface(Components.interfaces.nsISupportsString);
var html_obj    = html.value.QueryInterface(Components.interfaces.nsISupportsString);

// we remove Softhyphen and another control character here
var re = new RegExp('[\u200b' + String.fromCharCode(173)+ ']','g');

if (unicode_obj && html_obj)
{
    var unicode_str = unicode_obj.data.replace(re, '');
    var html_str    = html_obj.data.replace(re, '');

    // Neue Stringkomponenten für unicode und HTML-Content anlegen      
    var unicode_in = new StringComponent();
    unicode_in.data = unicode_str;

    var html_in = new StringComponent();
    html_in.data = html_str;

    // generate new transferable to write the data back to the clipboard
    // fill html + unicode flavors
    var tr_in = new Transferable();
    tr_in.setTransferData("text/html", html_in, html_in.data.length * 2);
    tr_in.setTransferData("text/unicode", unicode_in, unicode_in.data.length * 2);

    // copy content from transferable back to clipboard     
    clip.setData(tr_in, null, clip.kGlobalClipboard);
}

We developed a small Firefox-AddOn to remove special characters (hyphens) when copy/pasting content from the editor. This has been necessary because there is no javascript way to fill anyting into the clipboard. I guess it should be possible to write an extension for Chrome too (googel is your friend here). This seems to be the only way to get what you want from my point of view.

Example:
Here is the necessary code snippet for a FireFox-Addon to remove special characters onCopy

// get Clipboard Object
var clip  = Components.classes["@mozilla.org/widget/clipboard;1"].createInstance(Components.interfaces.nsIClipboard);

// get Transferable Object
    var tr_unicode = new Transferable(); 
var tr_html    = new Transferable();

// read "text/unicode flavors" (the clipboard has several "flavours" (html, plain text, ...))
    tr_unicode.addDataFlavor("text/unicode");
tr_html.addDataFlavor("text/html");
    clip.getData(tr_unicode, clip.kGlobalClipboard); // Systemclipboard
    clip.getData(tr_html, clip.kGlobalClipboard); // Systemclipboard

// generate objects to write the contents into (used for the clipboard)         
    var unicode = { }, ulen = { }, html = { }, hlen = { };

tr_html.getTransferData("text/html", html, hlen);
tr_unicode.getTransferData("text/unicode", unicode, ulen);

var unicode_obj = unicode.value.QueryInterface(Components.interfaces.nsISupportsString);
var html_obj    = html.value.QueryInterface(Components.interfaces.nsISupportsString);

// we remove Softhyphen and another control character here
var re = new RegExp('[\u200b' + String.fromCharCode(173)+ ']','g');

if (unicode_obj && html_obj)
{
    var unicode_str = unicode_obj.data.replace(re, '');
    var html_str    = html_obj.data.replace(re, '');

    // Neue Stringkomponenten für unicode und HTML-Content anlegen      
    var unicode_in = new StringComponent();
    unicode_in.data = unicode_str;

    var html_in = new StringComponent();
    html_in.data = html_str;

    // generate new transferable to write the data back to the clipboard
    // fill html + unicode flavors
    var tr_in = new Transferable();
    tr_in.setTransferData("text/html", html_in, html_in.data.length * 2);
    tr_in.setTransferData("text/unicode", unicode_in, unicode_in.data.length * 2);

    // copy content from transferable back to clipboard     
    clip.setData(tr_in, null, clip.kGlobalClipboard);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文