Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to allow users to copy text from a website, and the text be unformatted?

Tags:

html

css

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?

like image 415
Kyton Avatar asked Feb 04 '15 21:02

Kyton


2 Answers

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

like image 135
Scott Avatar answered Oct 08 '22 00:10

Scott


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)
});
like image 31
Kyton Avatar answered Oct 08 '22 00:10

Kyton