Chrome 和 Firefox 中的秘密复制到剪贴板 JavaScript 功能?
更新
看起来像浏览器开始支持 JS 本地复制
在 Mac 上的 Chrome 和 Firefox 的控制台窗口中,我可以执行
copy("party in your clipboard!");
并将文本复制到我的剪贴板。我搜索过 SO 和 Google,似乎找不到任何相关信息。
- 这些是特定于每个浏览器的吗?
- 我在哪里可以找到有关的更多信息 这些 JavaScript 函数?
浏览器版本:
执行“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 个屏幕截图
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:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信这些是预定义的 Firebug 控制台功能 - 至少 Firebug 似乎是这样。例如,如果您尝试调用
window.copy
,您将收到有关函数未定义的警告,因此它绝对不是浏览器函数,并且不能在普通 JavaScript 文件中使用。经过一番尝试后,以下函数似乎也可以在 JavaScript 控制台中运行:clear()
profile()
在 Chrome 控制台中运行这些函数可以揭示背后的源代码Webkit 控制台中的这些功能:
而
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:
While the Firebug source also defines a list of functions:
现代响应
现在,您可以使用跨浏览器工作的新 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).