Mroczny Arturek
Mroczny Arturek

Reputation: 419

How to change color of copied text

I have little problem with copying text. On my website color of the font is set to white:

body {
    color: #FFF;
}

Exmaple:

enter image description here

When I copy for example "List of programs" and I want paste it to Word, Lync text is white. Is it possible to add some styles/js which change this color to black in external programs? I know in word is paste option "Keep text only" but what with Lync?

@Update

That javascript works almost I expected. Problem is with IE. Any idea?(tested on chrome 45.0.2454.101)

 (function (container, defaultColor, copyColor) {
    selectedText = window.getSelection();

    $(container).keydown(function (e) {
        e = e || window.event;
        var key = e.which || e.keyCode;
        var ctrl = e.ctrlKey ? e.ctrlKey : ((key === 17) ? true : false);

        if (key == 67 && ctrl) {
            var range = getRange(selectedText);
            changeColor(range, selectedText, copyColor);
        }
    }).keyup(function (e) {
        var range = getRange(selectedText);

        if (range) {
            selectedText.removeAllRanges();
            selectedText.addRange(range);
        }
        changeColor(range, selectedText, defaultColor);
    });

    function getRange(text) {
        if (text.rangeCount && text.getRangeAt) {
            return text.getRangeAt(0);
        }
        return null;
    }

    function changeColor(range, selectedText, color) {
        document.designMode = "on";

        if (range) {
            selectedText.removeAllRanges();
            selectedText.addRange(range);
        }
        document.execCommand("ForeColor", false, color);
        document.designMode = "off";
    }
})("body", "white", "black");

Upvotes: 3

Views: 4159

Answers (6)

Mroczny Arturek
Mroczny Arturek

Reputation: 419

This code solved my problem.

window.onload = function () {
document.addEventListener('copy', function (e) {
    selectedText = window.getSelection().toString();
    if (window.clipboardData) {
        window.clipboardData.setData("Text", selectedText);
    } else {
        e.clipboardData.setData('text/plain', selectedText);
    }

    e.preventDefault(); 
});}

Upvotes: 3

Rashy
Rashy

Reputation: 911

You can reset text while selecting as follows:

p.reset_selection {
  color: #FFF;
  background-color: #31727E;
  padding: 15px;
}
p.reset_selection::-moz-selection {
  background-color: #FFF;
  color: #000;
}
p.reset_selection::selection {
  background-color: #FFF;
  color: #000;
}
<p class="reset_selection">List of programs</p>

Or you can paste text in Lync using Ctrl+Shift+V

Or you can try PureText using which you can configure a hotkey like Win+V to paste text only without any styling.

Upvotes: 1

Meena patel
Meena patel

Reputation: 149

::-moz-selection {
   background-color: #FFA;
   color: #000;
}

/* Works in Safari */

::selection {
   background-color: #FFA;
   color: #000;
}

Upvotes: 0

Basha Khan
Basha Khan

Reputation: 21

You can copy paste the link from anywhere, then in your Microsoft word select the text that you have pasted and then right-click -> paste options -> merge formatting. I think this would solve your problem.

Upvotes: 2

Neville
Neville

Reputation: 528

I believe you are looking for ::selection. You can use it to set the color of the text or the background of the selected area (default is blue in chrome and some other browsers :-) )

Upvotes: 0

Prashant Kapoor
Prashant Kapoor

Reputation: 36

To get color of copied text as black, you have to add following css to your code:

::-moz-selection { /* For Firefox */
    color: #000;
}

::selection {
    color: #000;
}

It will change the color of the text you are selecting to #000 i.e., black and the text then you copied have black color.

Upvotes: 0

Related Questions