This code (courtesy of this page), does its job well when used inside a plain html file. In particular, it successfully copies selected text to clipboard.
function copySelectionText(){
var copysuccess
try {
copysuccess = document.execCommand("copy")
} catch(e) {
console.log(e)
copysuccess = false
}
return copysuccess
}
document.addEventListener('mouseup', function(){
var copysuccess = copySelectionText()
}, false)
However, I couldn't make it work when used inside a tampermonkey custom script inside this:
(function() {
// 'use strict';
// Your code here...
})();
Disclaimers:
- This is my first time to write a tampermonkey script.
- I intend to use this on a particular website a friend of mine owns and not abusively on any other website.
- Using vanilla javascript (no libraries) would be a major plus.
Question: How do I make this function work inside a tampermonkey custom script?
I followed the instructions in the tampermonkey/greasemonkey documentation.
Console shows "copysuccess is not defined". console.log(e)
also produces nothing.