1
Good morning, I understand very little javascript but I would like to create a script for tampermonkey where every word I click inside the desired element is copied, for example
<html>
<head>
<title>Página Teste</title>
<meta charset="utf-8">
</head>
<body>
<p id="copiar">Aqui vai o texto que eu quero copiar</p>
</body>
</html>
If I click any word of this element it would be copied, clicking on the word "text" will automatically give a Ctrl+c to it
Using Devtools from Chrome I did the following to test ( I understand that the code below doesn’t have much to do with what I need, but since I don’t understand much of the language this is how I was doing to test the results I need )
let copiar = document.getElementById('copiar');
let words = copiar.textContent.split(" ");
function myFunction(item) {
let txt = document.createElement("h2");
txt.innerHTML = item
$("body").append(txt);
words.forEach(myFunction);
It works fine for the part of taking the full text of id='copy' and separating each word and storing them individually in an array ( maybe it’s not necessary to store ), but I have no idea how to make an "onclick" function for every word that when I click on it automatically copy to the Clipboard so I need to give only Ctrl+v. Can anyone help me please?
Thank you!
Thank you for the reply, searching yesterday I got some progress using the following function http://jsfiddle.net/p89jm1yb/, it worked more or less the way I want, apart from the fact that I have to double click to make the copy of the word, but when I tried to move to the tampermonkey it didn’t work on the page I need to use. Gist with the code used in the tampemonkey https://gist.github.com/edufgimenez/ffa9ed45185c34655b68dc427e339e6b I don’t know much about java but I’ll look at your code and see what I can make of it. Thank you
– edufgimenez