使用 CSS 或 JavaScript 复制/剪切时删除文本样式
如何让我的用户从我的网站复制/剪切样式文本,而不带任何样式包袱(背景颜色、颜色等)?
已挫败的攻击路线:
- 使用 ::select 为文本设置不同的样式? 不起作用,::style 未复制
- 设置所选文本的样式使用 jQuery 的选择绑定 这只适用于输入,不适用于 p、div
- 通过使用 jQuery 将事件绑定到复制/粘贴来拦截和删除样式? 无法访问复制的对象以删除内容,尝试使用e.preventDefault();然后返回事件对象,但这也不起作用
- 保存剪贴板数据后修改它吗? 也没有骰子,大多数浏览器不会让你在没有 flash 和一些的情况下进入这个排序确认
How can I let my users copy/cut styled text from my site, without bringing along any style baggage (background-color, color, etc)?
Routes of attacks that have been foiled:
- Style the text differently using ::select? Doesn't work, ::style isn't copied
- Style the selected text using jQuery's select binding This only works for inputs, not p, divs
- Intercept and remove style by binding an event to copy/paste using jQuery? Can't access the copied object to remove stuff, tried using e.preventDefault(); then returning the event object but that didn't work either
- Modify the clipboard data once it's been saved? Also no dice, most browsers wont let you into this without flash and some sort confirmation
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
考虑到当前的浏览器功能,您可以拦截复制事件,获取不带样式的选择,并将其放入剪贴板。
我已经用 Chrome/Safari/Firefox 测试了这段代码。相信它也应该适用于 MS 浏览器。
Given current browser capabilities, you can intercept the copy event, get the selection without style, and put that into the clipboard.
I've tested this code with Chrome/Safari/Firefox. Believe is should work on MS browsers as well.
我现在没有时间编写示例,但是您可以对键盘快捷键触发的剪切/复制执行此操作。它不适用于通过上下文菜单或编辑菜单选项进行剪切/复制,因为它依赖于在剪切或复制事件触发之前更改用户选择。
步骤:
window.getSelection().getRangeAt(0).cloneContents()
来完成此操作,但 IE需要单独的代码。 9 然后您应该检查选择是否折叠。
window.setTimeout()
添加短暂的延迟(几毫秒),该延迟调用一个删除屏幕外元素并恢复原始选择的函数。I haven't got time to code up an example now, but you could do this for cut/copy triggered by keyboard shortcuts. It wouldn't work for cut/copy via context menu or Edit menu options because it relies on changing the user selection before the cut or copy event fires.
The steps:
window.getSelection().getRangeAt(0).cloneContents()
, although you'll need separate code for IE < 9 and you should check the selection is not collapsed.window.setTimeout()
that calls a function that removes the offscreen element and restores the original selection.触发复制或剪切后,您可以剥离 html 标签,只剥离带有某些正则表达式的文本。
此外,如果您将存储的文本存储在 ID 为“sample_div”的 div 中
使用 var String=$('sample_div').text(''); 只获取里面的文本
Once you have triggered the copy or cut you can strip the html tags and only the text with some regex
Also if you have your stored text in a div with id "sample_div"
Use
var String=$('sample_div').text('');
to get only the text inside您是否需要在浏览器中为每个用户执行此操作?
如果不 - 而且它只适合您 - 那么您可以使用 Clipmate 软件来完成此操作。
http://www.clipmate.com/index.htm
它具有删除所有样式的功能。
Do you need this to occur in the browser... for each user?
If not - and it is just for you - then you can do this with Clipmate software.
http://www.clipmate.com/index.htm
It has a feature that removes all styling.