最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript: Hijack Copy? - Stack Overflow

programmeradmin0浏览0评论

I was just reading the Times online and I wanted to copy a bit of text from the article and IM it to a friend, but I noticed when I did so, it automatically appended the link back to the article in what I had copied.

This is not a feature of my IM client, so I assume this happened because of some javascript on Times website.

How would I acplish this if I wanted to implement it on my site? Basically, I would have to hijack the copy operation and append the URL of the article to the end of the copied content, right? Thoughts?

Here's the article I was reading, for reference: ,8599,1914857,00.html

I was just reading the Times online and I wanted to copy a bit of text from the article and IM it to a friend, but I noticed when I did so, it automatically appended the link back to the article in what I had copied.

This is not a feature of my IM client, so I assume this happened because of some javascript on Times website.

How would I acplish this if I wanted to implement it on my site? Basically, I would have to hijack the copy operation and append the URL of the article to the end of the copied content, right? Thoughts?

Here's the article I was reading, for reference: http://www.time./time/health/article/0,8599,1914857,00.html

Share Improve this question asked Jan 14, 2010 at 16:55 neezerneezer 20.6k33 gold badges130 silver badges222 bronze badges 1
  • Interesting that you should ask about this now. Slashdot just posted a story regarding Tynt's "service:" yro.slashdot/story/10/01/14/1818222/… – JasCav Commented Jan 14, 2010 at 19:26
Add a ment  | 

5 Answers 5

Reset to default 5

It's a breeze with jQuery (which your referenced site is using):

$("body").bind('copy', function(e) {
    // The user is copying something
});

You can use the jQuery Search & Share Plugin which does this exact thing whenever somebody copies more than 40 chars from your site: http://www.latentmotion./search-and-share/

The site that you referenced is apparently using a service called Tynt Insight to acplish this though.

They are using the free service Tynt. If you want to acplish the same thing, just use the same service.

What browser are you using (and what version)?

In some newer browsers, the user is either asked if a website can access the clipboard, or its simply not allowed. In other browsers (IE 6, for example), it is allowed, and websites can easily read from and write to your copy clipboard.

Here is the code (IE only)

clipboardData.setData("Text", "I just put this in the clipboard using JavaScript");

The "Copy & Paste Hijacker" jQuery plugin does exactly what you want and seems better suited for your purposes than Tynt or Search & Share: http://plugins.jquery./project/copypaste

You can easily format the copied content, specify max or min characters, and easily include the title of the page or the URL in the copied content exactly where you want.

I recently noticed this on another website and wrote a blog post on how it works. The jQuery example doesn't seem to actually modify what the user copies and pastes, it just adds a new context menu.

In short:

var content = document.getElementById("content");
content.addEventListener("copy", oncopy);

function oncopy() {
  var newEl = document.createElement("p");
  document.body.appendChild(newEl);
  newEl.innerHTML = "In your copy, messing with your text!";
  var selection = document.getSelection();
  var range = selection.getRangeAt(0);
  selection.selectAllChildren(newEl);
  setTimeout(function() {
    newEl.parentNode.removeChild(newEl);
    selection.removeAllRanges();
    selection.addRange(range);
  }, 0)
}

The setTimeout at the end is important as it doesn't seem to work if the last part is executed immediately.

This example replaces your selected text at the last minute with my chosen string. You can also grab the existing selection and append whatever you like to the end.

发布评论

评论列表(0)

  1. 暂无评论