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?
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
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)
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With