0

I created a spreadsheet to keep track of all the videos uploaded by the YouTubers I follow. Then, I created a script to be executed from the console that lists all the videos of that user. I store the list in a variable, and then I log it, select it, and copy it to the clipboard, but I'd like to copy it automatically every time I run the script. The problem is that the text is not inside an element (like a div, or textarea), so I can't use either window.navigator.clipboard or document.execCommand('copy'). Is there a way to do that?.

Thanks & greets from Argentina (Hope it is from England someday).

IDsign4U (Marcelo Miguel Bazan).

This is the code I use (open the videos tab in any channel and try it):

console.clear();
console.log("Título + Duración + Estado + URL en Subscripciones (sin número de orden)");
var domains = "";
var i = "";
var text = "";
var title = "";
var duration = "";
var hours = "";
var link = "";
var video = "";
var textDuration = "";
var hoursCheck = "";
var finalDuration = "";
var finalTitle = "";
domains = document.getElementsByTagName('ytd-grid-video-renderer')
for (i = 0; i < domains.length; i++)
{title = domains[i].getElementsByTagName('h3');
duration = domains[i].getElementsByTagName('span');
link = domains[i].getElementsByTagName('a');
textDuration = duration[0].innerText.trim();
hoursCheck = "";
hoursCheck = textDuration.length > 5 ? "0": "00:";
finalDuration = hoursCheck + textDuration + "\t" + "P" + "\t";
finalTitle = title[0].innerText + "\t";
url = "https://www.youtube.com" + link[0].attributes['href'].value;
video = video + finalTitle + finalDuration + url + "\n";}
console.log(video);
IDsign4U
  • 1
  • 1
  • Does this answer your question? [How do I copy to the clipboard in JavaScript?](https://stackoverflow.com/questions/400212/how-do-i-copy-to-the-clipboard-in-javascript) – SuperStormer Jan 09 '21 at 00:04

3 Answers3

0

Why shouldn't you be able to use navigator.clipboard? It works fine while providing variables to copy to clipboard.

document.getElementById("copy").addEventListener("click", async () => {
  const text = "Text copied to the clipboard"
  await navigator.clipboard.write(text)
})
<button id="copy">
  Copy to clipboard
</button>
niklhs
  • 166
  • 9
0

When you say "from the console", do you mean the browser console? If so, there's a built-in global copy function (not window.copy, just copy).

Zac Anger
  • 6,983
  • 2
  • 15
  • 42
  • Zac: Yes, I do all from the console (check the code I added to my original post). I tried copy, but this is what it says: copy(video) <== video is the name of the variable which content I want to copy. VM222:1 Uncaught TypeError: copy is not a function at :1:1 – IDsign4U Jan 09 '21 at 12:16
  • Does this solve that problem? https://stackoverflow.com/questions/62212958/devtools-console-copy-is-not-a-function-while-on-youtube – Zac Anger Jan 09 '21 at 16:28
-1

Yes!! Thank you Zac (and wOxxOm and Jannis Ioannou) for the answer. It's just a matter of deleting the, for me, weird DOM element with the ID 'copy' to be able to use the copy function in the console.

Thanks & greets from Argentina (Hope it is from England someday).

IDsign4U (Marcelo Miguel Bazan).

IDsign4U
  • 1
  • 1