179

Update

Looks like browsers are starting to support copy natively in JS


In the console windows of both Chrome and Firefox on Mac I can execute

copy("party in your clipboard!");

and the text gets copied to my clipboard. I have searched SO and Google and can't seem to find anything on this.

  • Are these specific to each browser?
  • Where can I find more information on these JavaScript functions?

Browser versions:

alt text alt text

JavaScript returned from Chrome console when executing 'copy'

function (object)
    {
        if (injectedScript._type(object) === "node") {
            var nodeId = InjectedScriptHost.pushNodePathToFrontend(object, false, false);
            InjectedScriptHost.copyNode(nodeId);
        } else
            InjectedScriptHost.copyText(object);
    }
  • What does this code mean?

Here are 2 screenshots of executing copy function in Chrome console with all chrome extensions disabled

alt text

alt text

Mike Grace
  • 16,636
  • 8
  • 59
  • 79
  • Which Firefox console are you using, Error Console? – Matthew Flaschen Dec 30 '10 at 00:56
  • 3
    I'm no C++ programmer, but I found `void InjectedScriptHost::copyText(const String& text)` in http://opensource.apple.com/source/WebCore/WebCore-7533.18.1/inspector/InjectedScriptHost.cpp – Kai Dec 30 '10 at 01:36
  • @Kai, Hmmmmmm... looks like maybe it is built in to Apple's open source webkit then? – Mike Grace Dec 30 '10 at 02:03
  • Unfortunately this command doesn't work from a Chrome extension, so it must be specific to the developer tools console in WebKit. – atomicules Mar 23 '11 at 11:11
  • confirm this also works with Safari. Nice find! – bizi Nov 01 '13 at 17:59
  • It looks like this works without Firebug as well. :D https://developer.mozilla.org/en-US/docs/Tools/Web_Console/Helpers – Stefnotch Apr 02 '16 at 15:52

2 Answers2

73

I believe these are predefined Firebug console functions - at least that seems to be the case for Firebug. If you try calling window.copy for instance, you'll get a warning about function not defined, so it's definitely not a browser function, and cannot be used in normal JavaScript files. The following functions also seems to work in the JavaScript console, after playing around with it a bit:

  • clear()
  • profile()

Running these in the Chrome console reveals the source behind these functions in the Webkit console:

> profile
function ()
{
return console.profile.apply(console, arguments)
}

> clear
function ()
{
InjectedScriptHost.clearConsoleMessages();
}

> copy
function (object)
{
if (injectedScript._type(object) === "node")
object = object.outerHTML;
InjectedScriptHost.copyText(object);
}

While the Firebug source also defines a list of functions:

this.clear = function()  // no web page interaction
{
    Firebug.Console.clear(context);
};

this.inspect = function(obj, panelName)  // no web page interaction
{
    Firebug.chrome.select(obj, panelName);
};

this.keys = function(o)
{
    return FBL.keys(o);  // the object is from the page, unwrapped
};

this.values = function(o)
{
    return FBL.values(o); // the object is from the page, unwrapped
};

// etc...
Yi Jiang
  • 49,435
  • 16
  • 136
  • 136
  • 2
    Seems that this doesn't work in Chrome 44 when using node-inspector debugging console. `Clipboard is not enabled in hosted mode. Please inspect using chrome://inspect` – jcollum Jul 28 '15 at 19:41
  • 1
    Nice. This works in Chrome Version 61.0.3163.100 (Official Build) (64-bit)! – Shanimal Oct 14 '17 at 05:44
  • 2
    On my Chrome developer tool looks like it's working and also works `window.copy('content')` (on Firefox only with `copy('content')` works) – morhook Jul 11 '18 at 21:33
  • I'm on the new Edge and copy works as well. – code Jan 13 '22 at 01:16
3

Here you can see the reference copy command of Chrome Dev tools: https://developers.google.com/web/tools/chrome-devtools/console/utilities#copy

You shouldn't use this commands on real JS cross-browsers (just for debugging on the console so-to-speak).

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
morhook
  • 685
  • 7
  • 19