Amir Beraha
Amir Beraha

Reputation: 41

copy specific text to copy another text in javascript

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.

Upvotes: 1

Views: 85

Answers (4)

ali zarei
ali zarei

Reputation: 1241

 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();
});

Upvotes: 1

Vadym Shashkov
Vadym Shashkov

Reputation: 118

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

Upvotes: 0

CrayonViolent
CrayonViolent

Reputation: 32532

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>');
  }

});

Upvotes: 1

ChaseLouisPowers
ChaseLouisPowers

Reputation: 755

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);
    }

Upvotes: 0

Related Questions