Я хочу сделать это, когда я что-то копирую, я получу что-то еще в буфере обмена.
document.addEventListener('copy', function(e){
if(e.clipboardData.getData('Text').toString()==="@Trolluminati")
e.clipboardData.setData ("Text", "<@325608201175433216>");
e.preventDefault();
});
Idk, как сделать так, чтобы он изменил последнюю копию этого конкретного текста на другую.
Сейчас он просто сохраняет один и тот же скопированный текст и не меняет его.
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>');
}
});
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();
});
попробуй это
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();
});
Я создал скрипку со следующим кодом. Это работает для меня в Chrome.
document.addEventListener('copy', function (event) {
event.preventDefault();
var copytext = window.getSelection() + '<@325608201175433216>';
if (event.clipboardData) {
event.clipboardData.setData('Text', copytext);
}
});
Вот какой код, который может быть полезен
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);
}