从网站复制/粘贴时编辑剪贴板数据
我现在见过一些网站,如果您突出显示一篇文章的文本,复制它,然后粘贴,他们可以向其中添加更多文本。
尝试复制并粘贴 http://belfasttelegraph.co.uk/ 上的文章中的一段文本,您就会发现你会明白我的意思 - 他们在粘贴的文本中添加了指向原始文章的链接。
这是怎么做到的?我假设这里有一些 javascript 在工作
I have seen a few sites now where if you highlight text of an article, copy it, and then paste in they can add more text to it.
Try copying and pasting a section of text from an article at http://belfasttelegraph.co.uk/ and you'll see what I mean - they add a link to the original article in the pasted text.
How is this done? I'm assuming there is some javascript at work here
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是一个很好的效果,您可以看到使用 Firebug(在 Firefox 中)复制时触发的脚本。
启动Firebug并加载页面,选择clear(因为页面使用了大量ajax,很快就有100个请求)。然后选择“全部”选项卡并尝试复制。您将看到对 1x1 像素图像的请求,但如果按 + 按钮查看详细信息,您将在“params”选项卡中看到此 GET 请求将您请求的文本作为“content”参数传递,其中包含一些内容将用于操作剪贴板 DOM 的 xpath 信息:
正如 @Crimson 指出的,有一些方法可以操作剪贴板, 就像使用Flash和图像的zeroclipboard。
我认为这就是通过使用图像获取请求来更改剪贴板来完成该技术的方法。
This is a good effect, you can see the scripting that is fired on copy using Firebug (in Firefox).
Start up Firebug and load the page, choose clear (because the page uses a lot of ajax there are very quickly 100 requests). Then choose the 'All' tab and try to copy. You will see a request for a 1x1 pixel image but if you press on the + button to look at the details, you will see in the 'params' tab that this GET request passes your requested text as the 'content' parameter, with some xpath information that will be used to manipulate the clipboard DOM:
As @Crimson pointed out there are methods to manipulate the clipboard, like zeroclipboard which use Flash and an image.
I reckon that is how the technique is done by using the image get request to change the clipboard.
您会注意到,只有当您使用组合键 [Ctrl+C] 时才会发生这种情况,而当您突出显示文本并从右键单击菜单中选择复制时则不会发生这种情况。
他们只是捕获 [Ctrl+C] 按键。
此外,要将数据添加到剪贴板,请查看这篇文章:
如何在 JavaScript 中复制到剪贴板?
You will notice that this happens only when you use the key combination [Ctrl+C] and not if you highlight text and chose copy from the right-click menu.
They are simply trapping the [Ctrl+C] keystroke.
Further, to add data to the clipboard, take a look at this post:
How do I copy to the clipboard in JavaScript?
我最近注意到网站上出现了这种“剪贴板劫持”的情况。 thefutoncritic.com、cracked.com...如果您使用 Adblock,只需进入“手动条目”列表并添加 *post-copypaste.js* 即可。这应该可以防止网站将其广告添加到您的剪贴板。
I've noticed lately an influx of this "clipboard hijacking" on websites. thefutoncritic.com, cracked.com... If you use Adblock just go into the "manual entries" list and add *post-copypaste.js* to it. This should prevent the sites from adding their ads to your clipboard.
其他网站使用的另一种解决方案是使用 jQuery 和“复制”/“剪切”事件:
这里有一些示例:
http://www.mkyong.com/jquery/how-to-detect-copy-paste-and-cut-behavior-with-jquery/" mkyong.com/jquery/how-to-detect-copy-paste-and-cut-behavior-with-jquery/
An other solution used by other websites is to use jQuery and the 'copy' / 'cut' event :
Some examples here :
http://www.mkyong.com/jquery/how-to-detect-copy-paste-and-cut-behavior-with-jquery/
我访问的新闻网站使用此功能将“来源”附加到复制的选择中:
A news site i visit use this function to append a "source" to the copied selection: