Cefsharp - Paste Clipboard text into input at onclick event

Asked

Viewed 87 times

-1

I have a input within a Iframe on a page .html in https with the event onclick calling a function:

<input type="text" value="" id="TXT_PROTOCOLO" placeholder="Clique para colar o protocolo" onclick="pasteClipboard(this)" />

Being the function pasteClipboard:

function pasteClipboard(e)
{
    navigator.clipboard.readText().then(text =>
    {
        console.log('Clipboard text: ', text);
        e.value = text;
    })
    .catch(err =>
    {
        console.error(err);
    });
}

The above code works normally on Chrome, but not on Cefsharp. I tried to modify/add some properties of the type variable BrowserSettings in the creation of the type variable ChromiumWebBrowser, i and..:

chromeBrowser = new ChromiumWebBrowser(ConfigurationManager.AppSettings.Get("startupUrl"))
{
    // Adicionando outras configurações ao browser
    BrowserSettings = new BrowserSettings
    {
        JavascriptAccessClipboard = CefState.Enabled,
        JavascriptDomPaste = CefState.Enabled
    }
};

I thought enabling JavascriptAccessClipboard and JavascriptDomPaste for the browser to solve the problem (it seems self-explanatory), but is always expelled in the console one DOMException:

DOMException

I’ve had problems enabling access to the microphone, even using the flag enable-media-stream and I was only able to solve it when I put the executable in the list of trusted applications of Kaspersky Endpoint Security for Windows. So I believe it has nothing to do with the antivirus or maybe I should use another way to paste the text from clipboard in the input.

  • have any information on Exception ? already tested on other computer without Kaspersky ?

  • @Rovannlinhalis I could only find out that the antivirus was blocking the microphone when I tested it on a test machine. The behavior persisted even on this machine without Kaspersky. Already the expelled Exception is always without details (I will edit the post with an image)

1 answer

0


I found an alternative to the problem from of that answer. The idea is to pass the content of clipboard from C# to Javascript using the class Clipboard.

I created a method similar to the link above:

public string GetClipboardText()
{
    string clipboardData = string.Empty;
    Thread staThread = new Thread( delegate () { clipboardData = Clipboard.GetText(TextDataFormat.Text); });

    staThread.SetApartmentState(ApartmentState.STA);
    staThread.Start();
    staThread.Join();

    return clipboardData.Trim();
}

Simply return Clipboard.GetText(TextDataFormat.Text) or Clipboard.GetText() does not work, always returned a string empty. The alternative was to place this call in a threadand then return the value.

And in Javascript:

function pasteClipboard(e)
{
    cefSession.getClipboardText().then(r => e.value = r);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.