如何向复制的网页文本添加额外信息
一些网站现在使用 Tynt 的 JavaScript 服务,将文本附加到复制的内容中。
如果您使用此功能从网站复制文本,然后粘贴,您将在文本底部获得原始内容的链接。
Tynt 也会在事件发生时对其进行跟踪。这是一个巧妙的技巧,做得很好。
他们执行此操作的脚本令人印象深刻 - 而不是尝试操作剪贴板(默认情况下只有旧版本的 IE 允许他们执行此操作,并且应始终将其关闭),而是操作实际的选择。
因此,当您选择文本块时,额外的内容将作为隐藏的
添加到您的选择中。当您粘贴时,额外的样式将被忽略并显示额外的链接。对于简单的文本块来说,这实际上相当容易做到,但当您考虑不同浏览器中复杂 HTML 中可能的所有选择时,这将是一场噩梦。
我正在开发一个网络应用程序 - 我不希望任何人能够跟踪复制的内容,并且我希望额外的信息包含上下文相关的内容,而不仅仅是链接。 Tynt 的服务不太适合这种情况。
有谁知道有一个开源 JavaScript 库(可能是 jQuery 插件或类似的)提供类似的功能但不公开内部应用程序数据?
Some websites now use a JavaScript service from Tynt that appends text to copied content.
If you copy text from a site using this and then paste you get a link to the original content at the bottom of the text.
Tynt also tracks this as it happens. It's a neat trick well done.
Their script for doing this is impressive - rather than try to manipulate the clipboard (which only older versions of IE lets them do by default and which should always be turned off) they manipulate the actual selection.
So when you select a block of text the extra content is added as a hidden <div>
included in your selection. When you paste the extra style is ignored and the extra link appears.
This is actually fairly easy to do with simple blocks of text, but a nightmare when you consider all the selections possible across complex HTML in different browsers.
I'm developing a web application - I don't want anyone to be able to track the content copied and I would like the extra info to contain something contextual, rather than just a link. Tynt's service isn't really appropriate in this case.
Does anyone know of an open source JavaScript library (maybe a jQuery plug in or similar) that provides similar functionality but that doesn't expose internal application data?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
2022 更新
处理富文本格式的更复杂的解决方案。如果您只处理纯文本,2020 年的解决方案仍然适用。
2020 更新解决
方案适用于所有最新浏览器。
请注意,即使粘贴到富文本编辑器中,此解决方案也会去除富文本格式(例如粗体和斜体)。
[较早的帖子 - 2020 年更新之前]
有两种主要方法可以向复制的网页文本添加额外信息。
想法是监视
复制事件
,然后将带有额外信息的隐藏容器附加到dom
,并将选择扩展到它。此方法改编自这篇文章,作者:c.bavota。另请检查jitbit 的版本 适用于更复杂的情况。
这个想法是观察
复制事件
并直接修改剪贴板数据。这可以使用clipboardData
属性来实现。请注意,此属性在所有主要浏览器中均以只读
方式提供;setData
方法仅在 IE 上可用。2022 Update
More complex solution that handles rich text formatting. The 2020 solution is still relevant if you only deal with plain text.
2020 Update
Solution that works on all recent browsers.
Note that this solution will strip rich text formatting (such as bold and italic), even when pasting into a rich text editor.
[Older post - before the 2020 update]
There are two main ways to add extra info to copied web text.
The idea is to watch for the
copy event
, then append a hidden container with our extra info to thedom
, and extend the selection to it.This method is adapted from this article by c.bavota. Check also jitbit's version for more complex case.
The idea is to watch the
copy event
and directly modify the clipboard data. This is possible using theclipboardData
property. Note that this property is available in all major browsers inread-only
; thesetData
method is only available on IE.这是上面修改后的普通 JavaScript 解决方案,但支持更多浏览器(跨浏览器方法)
This is a vanilla javascript solution from a modified solution above but supports more browsers (cross browser method)
我测试过并且正在运行的 jQuery 最短版本是:
The shortest version for jQuery that I tested and is working is:
这是 jquery 中的一个插件来做到这一点
https://github.com/niklasvh/jquery.plugin.clipboard
来自项目自述文件
“此脚本在调用复制事件之前修改选择的内容,导致复制的选择与用户选择的内容不同。
这允许您将内容附加/添加到选择中,例如版权信息或其他内容。
根据 MIT 许可发布”
Here is a plugin in jquery to do that
https://github.com/niklasvh/jquery.plugin.clipboard
From the project readme
"This script modifies the contents of a selection prior to a copy event being called, resulting in the copied selection being different from what the user selected.
This allows you to append/prepend content to the selection, such as copyright information or other content.
Released under MIT License"
对答案进行改进,修改后恢复选择,以防止复制后随机选择。
Improving on the answer, restore selection after the alterations to prevent random selections after copy.
2018年的改进
Improvement for 2018
还有一个更短的解决方案:
Also a little shorter solution:
它是上述 2 个答案的汇编 + 与 Microsoft Edge 的兼容性。
我还在最后添加了原始选择的恢复,正如任何浏览器默认情况下所期望的那样。
It's a compilation of 2 answers above + compatibility with Microsoft Edge.
I've also added a restore of the original selection at the end, as it is expected by default in any browser.