document.execCommand vs range.insertNode

Having problems with building or using the CefSharp .NET binding? Ask your CEF-related questions here. Please ask general usage questions on StackOverflow.

Moderator: amaitland

document.execCommand vs range.insertNode

Postby andla71 » Fri Aug 26, 2022 2:30 pm

On some websites, I have a problem.
sometimes document.execCommand is the only thing that works but it is Deprecated. Any ideas?

Inserting HTML or Text into contenteditable elements
Code: Select all
//Much more reliable
function insertHTMLAtCursor2(text)
{
    let selection = window.getSelection();
    let range = selection.getRangeAt(0);
    range.deleteContents();

    document.execCommand('insertHTML', false, text);
    //range.pasteHTML(text);
    //range.insertNode(node);

                for (let position = 0; position != text.length; position++)
    {
        selection.modify('move', 'right', 'character');
    };
        }

//Not that reliable
function insertHTMLAtCursor(text)
{
    let selection = window.getSelection();
    let range = selection.getRangeAt(0);
    range.deleteContents();

    let node  = range.createContextualFragment(text);
    range.insertNode(node);

    for(let position = 0; position != text.length; position++)
    {
        selection.modify('move', 'right', 'character');
    };
}

andla71
Techie
 
Posts: 28
Joined: Sat Oct 31, 2020 12:19 pm

Re: document.execCommand vs range.insertNode

Postby magreenblatt » Fri Aug 26, 2022 2:58 pm

Does it behave the same in Google Chrome?
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: document.execCommand vs range.insertNode

Postby andla71 » Fri Aug 26, 2022 3:04 pm

On some websites only one function work.
What I mean is that sometimes only insertHTMLAtCursor2() works compared to insertHTMLAtCursor()

Chromium browser of course.
andla71
Techie
 
Posts: 28
Joined: Sat Oct 31, 2020 12:19 pm

Re: document.execCommand vs range.insertNode

Postby magreenblatt » Fri Aug 26, 2022 3:16 pm

This is not a general-purpose web development forum. I suggest you seek somewhere more appropriate for general questions, like stackoverflow.
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: document.execCommand vs range.insertNode

Postby andla71 » Fri Aug 26, 2022 8:50 pm

They would be confused when I inject code to automate tasks.
andla71
Techie
 
Posts: 28
Joined: Sat Oct 31, 2020 12:19 pm

Re: document.execCommand vs range.insertNode

Postby amaitland » Sun Aug 28, 2022 3:21 pm

Run your code in Chrome using DevTools console. Confirm you see the same behaviour in Chrome.
Maintainer of the CefSharp project.
amaitland
Virtuoso
 
Posts: 1292
Joined: Wed Jan 14, 2015 2:35 am


Return to CefSharp Forum

Who is online

Users browsing this forum: Google [Bot] and 125 guests