html页面如何复制功能,将页面的HTML片段复制到剪贴板

我们开发了一个小型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);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值