简体   繁体   English

如何将javascript变量文本复制到剪贴板?

[英]How to copy javascript variable text to clipboard?

I'm using CefSharp to build a web browser and I'm integrating some javascript on button click, etc.我正在使用 CefSharp 来构建一个网络浏览器,我正在集成一些 javascript 按钮点击等。

I am trying to get the element by class name and then save its text to the clipboard.我正在尝试按类名获取元素,然后将其文本保存到剪贴板。

It successfully finds an element and the text, but I can't set it to the clipboard no matter what.它成功找到了一个元素和文本,但无论如何我都无法将其设置到剪贴板。

Here is the code这是代码

private void button3_Click(object sender, EventArgs e)
    {
        CurBrowser.GetMainFrame().ExecuteJavaScriptAsync("var elems1 = document.getElementsByClassName('question-text')");
        CurBrowser.GetMainFrame().ExecuteJavaScriptAsync("textt = elems1[elems1.length - 1].innerText");
        CurBrowser.GetMainFrame().ExecuteJavaScriptAsync("navigator.clipboard.writeText(textt);");
}

It says that the document is not focused, but I tried to focus it using它说文档没有聚焦,但我尝试使用它聚焦

textt.focus();

or或者

elems1.focus():

it returns "textt.focus(); is not a function" or "elems1.focus(): is not a function"它返回“textt.focus(); is not a function”或“elems1.focus(): is not a function”

Try running everything in on尝试运行所有内容

CurBrowser.GetMainFrame().ExecuteJavaScriptAsync("const element = document.createElement('textarea'); element.value = document.getElementsByClassName('question-text')[0].innerText; document.body.appendChild(element); element.select(); document.execCommand('copy'); document.body.removeChild(element);");

If it still doesn't say its in focus, try adding the focus within that too.如果它仍然没有说它处于焦点,请尝试在其中添加焦点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM