在 Chrome 和 Firefox 中秘密复制到剪贴板 JavaScript 功能?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4559180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 12:56:54  来源:igfitidea点击:

Secret copy to clipboard JavaScript function in Chrome and Firefox?

javascriptfirefoxgoogle-chromecopyclipboard

提问by Mike Grace

Update

更新

Looks like browsers are starting to support copy natively in JS

看起来浏览器开始支持在 JS 中本地复制



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

在 Mac 上的 Chrome 和 Firefox 的控制台窗口中,我可以执行

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.

并且文本被复制到我的剪贴板。我已经搜索过 SO 和 Google,但似乎找不到任何关于此的内容。

  • Are these specific to each browser?
  • Where can I find more information on these JavaScript functions?
  • 这些是特定于每个浏览器的吗?
  • 在哪里可以找到有关这些 JavaScript 函数的更多信息?

Browser versions:

浏览器版本:

alt textalt text

替代文字替代文字

JavaScript returned from Chrome console when executing 'copy'

执行“复制”时从 Chrome 控制台返回的 JavaScript

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

这是在禁用所有 Chrome 扩展程序的情况下在 Chrome 控制台中执行复制功能的 2 个屏幕截图

alt text

替代文字

alt text

替代文字

采纳答案by Yi Jiang

I believe these are predefined Firebug console functions - at least that seems to be the case for Firebug. If you try calling window.copyfor 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:

我相信这些是预定义的 Firebug 控制台功能 - 至少 Firebug 似乎是这种情况。window.copy例如,如果您尝试调用,则会收到有关未定义函数的警告,因此它绝对不是浏览器函数,不能在普通 JavaScript 文件中使用。以下函数似乎也可以在 JavaScript 控制台中运行,但稍微玩了一下之后:

  • clear()
  • profile()
  • clear()
  • profile()


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

在 Chrome 控制台中运行这些会揭示 Webkit 控制台中这些函数背后的来源:

> 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 sourcealso defines a list of functions:

Firebug 源也定义了一个函数列表:

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...

回答by morhook

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

这里可以看到Chrome开发工具的参考复制命令: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).

你不应该在真正的 JS 跨浏览器上使用这个命令(只是为了在控制台上调试)。