16

I know that I can use copy() function on the console to add content to the clipboard.

When I am on any other page copy('test') works.

When I am on youtube I get:

Uncaught TypeError: copy is not a function

How can I fix this. E.g. Is there a way to prevent a site from overriding specific functions using devtools?

Strangely on firefox copy() works on youtube, so could this be a chrome bug?

Jannis Ioannou
  • 1,692
  • 1
  • 14
  • 17
  • 3
    Run `copy` in console and you'll see it's a DOM element with id `copy`. Once you remove this element you can use the built-in API. – wOxxOm Jun 05 '20 at 10:39
  • 1
    @wOxxOm Thank you for the feedback. indeed `document.querySelector('#copy').remove()` works. I wonder why does this happen? Are ids supposed to be accessible as variables? I try to access a differrent id by its name but I cannot. And why this does not work on firefox? – Jannis Ioannou Jun 05 '20 at 11:15
  • 2
    1) see [Do DOM tree elements with ids become global variables?](https://stackoverflow.com/q/3434278), 2) dunno, but google is known to serve different sites to non-chrome browsers. – wOxxOm Jun 05 '20 at 11:25
  • Is there anyway to reinstate `copy`? – Jikku Jose Sep 27 '20 at 06:42

1 Answers1

14

The issue is that there is an element on that page with id="copy". If you type copy in the console, you should get a element printed out like this:

$ copy
<g id="copy"></g>

So, you'll have to remove that element before using the copy function:

document.querySelector('#copy').remove();

Running it in console again should show it's a function:

$ copy
ƒ copy() { [native code] }

Then use copy() as normal, e.g. copy(myVariable)

dǝɥɔS ʇoıןןƎ
  • 1,674
  • 5
  • 19
  • 42