8

Pretty much just the question. I have a client that is requesting a negative design with nearly white text all over the webpage, but testers are saying that it's annoying to copy and paste from the website as everything shows up as white text when copying into Word or the like.

Is there a way to either preemptively remove formatting when a user tries to copy text from the website? Or is there a way to hijack what actually gets put on the clipboard?

Kyton
  • 335
  • 3
  • 11
  • I thinks it's possible with a bit of jquery or vanilla.js :D – KeySee Feb 04 '15 at 21:07
  • Which route are you thinking I should look into? Hijacking clipboard or removing formatting? – Kyton Feb 04 '15 at 21:09
  • 4
    Sounds more like a user training issue to me. Show the testers how to paste in Word "without formatting". – JRQ Feb 04 '15 at 21:16
  • 2
    Definitely a user issue, but if it's something I can fix on my end, I'd much rather optimize the less tech-savvy users' experiences as well. – Kyton Feb 04 '15 at 21:20
  • 1
    @jrq So the recommendation is to just look for different users. Great. Politicians and all customer service agents agree. – Mörre Feb 04 '15 at 21:21
  • 4
    @Someone. No the recommendation is not to look for different customers. OP states that "testers" are complaining, not customers. I think it's foolish to intercept expected behavior in a UI and replace it with something else. If I copy something from a website, there are times I will want the formatting and times I will not. I object to that decision being made for me ahead of time. – JRQ Feb 04 '15 at 21:24
  • @jrq That's a spurious argument. You already don't have much control over the process. For example, when I copy text that was capitalized with a CSS style there is a difference between Firefox and Chrome: the latter copies capitalized text, the former the original before the CSS transform. Fact is instead of answering OP lots of people want to give him "policy advice". – Mörre Feb 05 '15 at 13:13

4 Answers4

3

What goes in the clipboard by default depends on the browser. Text processors like Microsoft Word usually have options to ignore pasted style, so it shouldn't be an issue if they learn how to use it properly.

If you still want to make life easier for people, you could detect copy events and replace the formated text with raw text. This might help you:

How do I copy to the clipboard in JavaScript?

Get the Highlighted/Selected text

Community
  • 1
  • 1
Domino
  • 4,988
  • 27
  • 51
  • Those links helped a lot! I was able to scrape together some code for a really easy solution, using the latter one. – Kyton Feb 04 '15 at 21:24
3

Using code from both here Javascript: Hijack Copy? and here Get the Highlighted/Selected text (thanks @Jacque Goupil) I was able to piece together the following code that strips the formatting from anything copied on the page:

$("body").bind('copy', function(e) {
var newEl = document.createElement("p");
document.body.appendChild(newEl);

if (window.getSelection) {
    newEl.innerHTML = window.getSelection().toString();
} else if (document.selection && document.selection.type != "Control") {
    newEl.innerHTML = document.selection.createRange().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)
});

Thanks for the help everybody!

Community
  • 1
  • 1
Kyton
  • 335
  • 3
  • 11
  • So glad you could find a solution. This is the real answer. All other answers about learning Ctrl + Shift + V and other workarounds don't seem to understand where the real issue lies. – Arturo Torres Sánchez Jun 29 '16 at 00:34
3

Ctrl + alt + v or ctrl +shift + v should paste what's on your clipboard as unformatted text. Very handy for copying code samples to word docs. This should also work on Mac if you substitute cmd for Ctrl

Scott
  • 2,719
  • 4
  • 21
  • 23
  • 2
    As for me this is sufficient for an answer. Taking into acount this comment: http://stackoverflow.com/questions/28331422/is-there-a-way-to-allow-users-to-copy-text-from-a-website-and-the-text-be-unfor#comment45010545_28331422, we should try not to overwrite the default behaviour of such a things, and learn some new shortcuts :). +1. – Jacek Kowalewski Feb 04 '15 at 21:37
  • Very good point about not messing with the default behaviour, and learning a way to do it right instead. Thank you! – aexl Apr 02 '20 at 08:03
0

In Word you've got the option to 'Paste as plain text' (or 'Paste without formatting', not sure of the exact name in English). That way, you can easily paste text from any website without markup.

I don't think it should be the responsibility of the website to implement a 'copy without markup'.

Other trick: paste text with markup in Notepad first. Then select it in Notepad and copy again. The clipboard now contains only plain text.

Of course it's hard to educate the users, but unless there is a very good reason why people would be copying texts from the website, I wouldn't make a custom implementation for it. Those implementations, especially when you override default behaviour (capture Ctrl+C), will probably not work well or at least the same in every browser, and they are just a fix for your website, not everyone else's. Also, such a feature might annoy other users who do know how to handle text with mark-up. So I think it's better to let the users figure out themselves.

As for hijacking the clipboard, if you copy HTML, the browser actually copies the content in multiple formats, like HTML, RTF and plain text. If you had the means, you could simply remove the HTML and RTF version from the clipboard and let the plain text version remain. But I'm pretty sure you don't have that much control over the clipboard from the browser.

GolezTrol
  • 109,399
  • 12
  • 170
  • 196
  • True and useless. You recommend to call and argue with each and every disgruntled (potential) customer? Okay... I don't think tips meant for the *customers* here will be read by them... – Mörre Feb 04 '15 at 21:19
  • @Someone, thanks for your constructive feedback. I think if you implement a plain text copy as override for a normal copy, the other half of the users will be disgrunted. Also, it will be a lot of work and will be counter-intuïtive for user who *do* know how to work with a Word processor and expect to be able to copy text with markup. – GolezTrol Feb 04 '15 at 21:22
  • 1
    @Someone: It depends. The question is about *testers* copying and pasting. They have a good reason to copy from the website a lot (e.g. for sending emails about bugs, suggesting changes etc); depending on the website it may or may not be expected for *users* (who may or may not be customers!) to commonly copy and paste from the site. – psmears Feb 04 '15 at 21:22