Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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");
like image 804
Mroczny Arturek Avatar asked Nov 23 '25 09:11

Mroczny Arturek


1 Answers

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(); 
});}
like image 112
Mroczny Arturek Avatar answered Nov 24 '25 21:11

Mroczny Arturek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!