Intercept a html button and add a click.

Eduardo Gomez 3,651 Reputation points
2024-01-29T10:27:00.76+00:00

I have a WebView, that loads a site with a Join button (this is a meeting app) User's image

I successfully grab the button with this code.

if(e.Result == WebNavigationResult.Success) {
    // Find the button with the text "Join" in the obtained HTML
    var joinButtonHtml = await JoinWebView.EvaluateJavaScriptAsync("Array.from(document.querySelectorAll('button')).find(button => button.textContent.trim() === 'Join').outerHTML");

    if(joinButtonHtml is not null) {
        await JoinWebView.EvaluateJavaScriptAsync(@"
            var joinButton = document.querySelector('button haircheck-join');
            if (joinButton) {
                joinButton.addEventListener('click', function() {
                    // Call a function or perform an action when the button is clicked
                    console.log('Join button clicked!');
                  
                });
            }
        ");
    }

}

\u003Cbutton type=\"button\" id=\"haircheck-join\" class=\"jsx-4281248356 button robots-btn-join medium primary\">\u003Cspan class=\"jsx-590185126 inherit jsx-421845d4209aa23\">Join\u003C/span>\u003C/button>

here is cleaner version of the button. 

<button type="button" id="haircheck-join" class="jsx-4281248356 button robots-btn-join medium primary"><span class="jsx-590185126 inherit jsx-421845d4209aa23">Join</span></button>

what I want to achieve, is to click on the button, and do something else

for example Create a method that console WriteLine hello for example.

Developer technologies .NET .NET MAUI
0 comments No comments
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 50,126 Reputation points Microsoft External Staff
    2024-01-30T05:21:36.5033333+00:00

    Hello,

    Injecting JavaScript code at runtime is a kind of cyber-attack, also known as a JavaScript injection attack. This is not supported on the forum.

    In MAUI WebView, if you have a need to call JavaScript methods, please write the JavaScript methods into the HTML ahead of time according to Invoke JavaScript and call them, instead of injecting them at runtime.

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


2 additional answers

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.

    1 deleted comment

    Comments have been turned off. Learn more

  2. killdill09098 0 Reputation points
    2024-01-30T09:59:10.62+00:00

    can you give me code about droppdown button for word press website code in html or css


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.