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

发布于 2024-10-09 19:43:16 字数 1130 浏览 7 评论 0原文

更新

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


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

copy("party in your clipboard!");

并将文本复制到我的剪贴板。我搜索过 SO 和 Google,似乎找不到任何相关信息。

  • 这些是特定于每个浏览器的吗?
  • 我在哪里可以找到有关的更多信息 这些 JavaScript 函数?

浏览器版本:

alt text alt text

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

function (object)
    {
        if (injectedScript._type(object) === "node") {
            var nodeId = InjectedScriptHost.pushNodePathToFrontend(object, false, false);
            InjectedScriptHost.copyNode(nodeId);
        } else
            InjectedScriptHost.copyText(object);
    }
  • 这段代码是什么意思?

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

alt text

替代文本

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鸩远一方 2024-10-16 19:43:16

我相信这些是预定义的 Firebug 控制台功能 - 至少 Firebug 似乎是这样。例如,如果您尝试调用 window.copy,您将收到有关函数未定义的警告,因此它绝对不是浏览器函数,并且不能在普通 JavaScript 文件中使用。经过一番尝试后,以下函数似乎也可以在 JavaScript 控制台中运行:

  • clear()
  • profile()

在 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);
}

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

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...
断爱 2024-10-16 19:43:16

现代响应

现在,您可以使用跨浏览器工作的新 API,您可以添加隐藏在 DOM 上的输入文本,选择它,然后运行 ​​document.execCommand('copy');

参考:
https://developer.chrome.com/blog/cut-and-copy -commands/

古老的响应时间

在这里你可以看到Chrome开发工具的参考复制命令:
https://developers.google.com/web/tools/ chrome-devtools/console/utilities#copy

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

Modern response

Nowadays you can use the new API that works cross browsers, you can add an input text hidden on the DOM, select it and then run document.execCommand('copy');

Reference:
https://developer.chrome.com/blog/cut-and-copy-commands/

Ancient response times

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文