Reputation: 41
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
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
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);
}
});
Upvotes: 0
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
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