1

I want to make it like when i copy something i'll get something else on the clipboard.

document.addEventListener('copy', function(e){
  if(e.clipboardData.getData('Text').toString()==="@Trolluminati") 
  e.clipboardData.setData ("Text", "<@325608201175433216>");
  e.preventDefault();
});

Idk, how to make it so it will change the last copy of this specific text to the other one.

Right now it just keeps the same copied text and does not change it.

Amir Beraha
  • 41
  • 1
  • 8

4 Answers4

1

 document.addEventListener('copy', function(e){
console.log(0);
if( window.getSelection().toString().trim()==="@Trolluminati") 
  e.clipboardData.setData ("Text", "<@325608201175433216>");
else
  e.clipboardData.setData ("Text", window.getSelection().toString().trim());
e.preventDefault();
});

try this

 document.addEventListener('copy', function(e){
console.log(0);
if( window.getSelection().toString().trim()==="@Trolluminati") 
  e.clipboardData.setData ("Text", "<@325608201175433216>");
else
  e.clipboardData.setData ("Text", window.getSelection().toString().trim());
e.preventDefault();
});
ali zarei
  • 1,212
  • 11
  • 17
1
document.addEventListener('copy', function(e){
  var selectedText  = window.getSelection().toString();
  if (selectedText == '@Trolluminati') {
      e.preventDefault();
      clipboardData = e.clipboardData || window.clipboardData || e.originalEvent.clipboardData;
      clipboardData.setData('text', '<@325608201175433216>');
  }

});
CrayonViolent
  • 32,111
  • 5
  • 56
  • 79
  • @AmirBeraha well then I feel there's more to your question you aren't showing and your problem is elsewhere – CrayonViolent Dec 07 '17 at 22:02
  • well, i want it just when copying (using ctrl c) on @Trolluminati it will allow pasting (using ctrl v) <@325608201175433216> maybe i forgot to mention that, oops – Amir Beraha Dec 07 '17 at 22:05
0

Here's some code that might be helpful

if (document.queryCommandSupported('copy')) {
      var textArea = document.createElement("textarea");
      textArea.style.position = 'fixed';
      textArea.style.top = '0';
      textArea.style.left = '0';
      textArea.style.width = '2em';
      textArea.style.height = '2em';
      textArea.style.padding = '0';
      textArea.style.border = 'none';
      textArea.style.outline = 'none';
      textArea.style.boxShadow = 'none';
      textArea.style.background = 'transparent';
      textArea.value = INSERT WHATEVER YOU WANT TO COPY HERE
      document.body.appendChild(textArea);
      textArea.select();
      try {
        var successful = document.execCommand('copy');
        var msg = successful ? 'successful' : 'unsuccessful';
        console.log('Copying text command was ' + msg);
      } catch (err) {
        console.log('Oops, unable to copy');
      }
      document.body.removeChild(textArea);
    }
  • i meant that i want to select text with my mouse (not an inputed text) and when i copy a specific text, in that case "@Trolluminati" the clipboard will add "<@325608201175433216>" – Amir Beraha Dec 07 '17 at 21:52
  • this is a way of creating a hidden text area that you copy from behind the scenes.. try replacing the INSERT WHATEVER YOU WANT HERE with <@325608201175433216> – ProfessorManhattan Dec 07 '17 at 22:00
0

I have created fiddle with the next code. It works for me in Chrome.

document.addEventListener('copy', function (event) {
  event.preventDefault();

  var copytext = window.getSelection() + '<@325608201175433216>';

  if (event.clipboardData) {
    event.clipboardData.setData('Text', copytext);
  }
});

jsfiddle

Vadym Shashkov
  • 118
  • 2
  • 14