简体   繁体   中英

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.

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"

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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