Search code examples
javascriptc#geckogeckofxalerts

Disable JavaScript Alerts Gecko45 C#


I want to disable all js alert.This code not working gecko 45

geckoWebBrowser1.JavascriptError += (sender, error) => {
    GeckoWebBrowser browser = geckoWebBrowser1;
    string text = "window.alert = function(){};";
    using (AutoJSContext context = new AutoJSContext(browser.Window.JSContext)) {
        string result;
        //toolStripLabel1.Text = "was is loaded?";

        context.EvaluateScript(text, (nsISupports)browser.Window.DomWindow, out result);
    }
};

Solution

  • If you want to disable all alerts, then you need to implement your own nsIPromptService2 where you will override the methods called when an alert happens.

    public class FilteredPromptService : nsIPromptService2, nsIPrompt
    {
            public void Alert(string dialogTitle, string text)
            {
               //do nothing, 
            }
            //and so on for other alerts/prompts
     }
    

    Bear in mind you also need to register in by calling after you initialize the engine:

    PromptFactory.PromptServiceCreator = () => new FilteredPromptService();