Отключить оповещения JavaScript Gecko45 C#

Я хочу отключить все оповещения js. Этот код не работает 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);
    }
};

person Tolga29    schedule 25.05.2017    source источник
comment
Эээ... Это вообще Javascript? Я так не думаю. Вы можете попробовать переопределить alert чем-то вроде alert = function(){ }   -  person Jeremy Thille    schedule 25.05.2017
comment
@JeremyThille, если OP не отредактирует свой вопрос, это то, что они пытаются ....   -  person epascarello    schedule 25.05.2017
comment
Похоже, это дубликат stackoverflow.com/questions/40737542/   -  person Bartosz    schedule 27.05.2017


Ответы (2)


Если вы хотите отключить все оповещения, вам нужно реализовать собственный nsIPromptService2, в котором вы будете переопределять методы, вызываемые при появлении оповещения.

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

Имейте в виду, что вам также необходимо зарегистрироваться, позвонив после инициализации движка:

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

person Bartosz    schedule 26.05.2017

Вот полный класс, который я должен был добавить. Это единственное рабочее решение, которое я нашел для GeckoFX 45.

Xpcom.Initialize("Firefox64");
PromptFactory.PromptServiceCreator = () => new FilteredPromptService();

public class FilteredPromptService : nsIPromptService2, nsIPrompt
{   
    void nsIPromptService2.Alert(nsIDOMWindow aParent, string aDialogTitle, string aText)
    {

    }

    void nsIPromptService2.AlertCheck(nsIDOMWindow aParent, string aDialogTitle, string aText, string aCheckMsg, ref bool aCheckState)
    {

    }

    bool nsIPromptService2.Confirm(nsIDOMWindow aParent, string aDialogTitle, string aText)
    {
         throw new NotImplementedException();
    }

    bool nsIPromptService2.ConfirmCheck(nsIDOMWindow aParent, string aDialogTitle, string aText, string aCheckMsg, ref bool aCheckState)
    {
         throw new NotImplementedException();
    }

    int nsIPromptService2.ConfirmEx(nsIDOMWindow aParent, string aDialogTitle, string aText, uint aButtonFlags, string aButton0Title, string aButton1Title, string aButton2Title, string aCheckMsg, ref bool aCheckState)
    {
         throw new NotImplementedException();
    }

    bool nsIPromptService2.Prompt(nsIDOMWindow aParent, string aDialogTitle, string aText, ref string aValue, string aCheckMsg, ref bool aCheckState)
    {
         throw new NotImplementedException();
    }

    bool nsIPromptService2.PromptUsernameAndPassword(nsIDOMWindow aParent, string aDialogTitle, string aText, ref string aUsername, ref string aPassword, string aCheckMsg, ref bool aCheckState)
    {
         throw new NotImplementedException();   
    }

    bool nsIPromptService2.PromptPassword(nsIDOMWindow aParent, string aDialogTitle, string aText, ref string aPassword, string aCheckMsg, ref bool aCheckState)
    {
         throw new NotImplementedException();
    }

    bool nsIPromptService2.Select(nsIDOMWindow aParent, string aDialogTitle, string aText, uint aCount, IntPtr[] aSelectList, ref int aOutSelection)
    {
         throw new NotImplementedException();
    }

    bool nsIPromptService2.PromptAuth(nsIDOMWindow aParent, nsIChannel aChannel, uint level, nsIAuthInformation authInfo, string checkboxLabel, ref bool checkValue)
    {
         throw new NotImplementedException();
    }

    nsICancelable nsIPromptService2.AsyncPromptAuth(nsIDOMWindow aParent, nsIChannel aChannel, nsIAuthPromptCallback aCallback, nsISupports aContext, uint level, nsIAuthInformation authInfo, string checkboxLabel, ref bool checkValue)
    {
         throw new NotImplementedException();
    }

    void nsIPromptService.Alert(nsIDOMWindow aParent, string aDialogTitle, string aText)
    {

    }

    void nsIPromptService.AlertCheck(nsIDOMWindow aParent, string aDialogTitle, string aText, string aCheckMsg, ref bool aCheckState)
    {

    }

    bool nsIPromptService.Confirm(nsIDOMWindow aParent, string aDialogTitle, string aText)
    {
         throw new NotImplementedException();
    }

    bool nsIPromptService.ConfirmCheck(nsIDOMWindow aParent, string aDialogTitle, string aText, string aCheckMsg, ref bool aCheckState)
    {
         throw new NotImplementedException();
    }

    int nsIPromptService.ConfirmEx(nsIDOMWindow aParent, string aDialogTitle, string aText, uint aButtonFlags, string aButton0Title, string aButton1Title, string aButton2Title, string aCheckMsg, ref bool aCheckState)
    {
         throw new NotImplementedException();
    }

    bool nsIPromptService.Prompt(nsIDOMWindow aParent, string aDialogTitle, string aText, ref string aValue, string aCheckMsg, ref bool aCheckState)
    {
         throw new NotImplementedException();
    }

    bool nsIPromptService.PromptUsernameAndPassword(nsIDOMWindow aParent, string aDialogTitle, string aText, ref string aUsername, ref string aPassword, string aCheckMsg, ref bool aCheckState)
    {
         throw new NotImplementedException();
    }

    bool nsIPromptService.PromptPassword(nsIDOMWindow aParent, string aDialogTitle, string aText, ref string aPassword, string aCheckMsg, ref bool aCheckState)
    {
         throw new NotImplementedException();
    }

    bool nsIPromptService.Select(nsIDOMWindow aParent, string aDialogTitle, string aText, uint aCount, IntPtr[] aSelectList, ref int aOutSelection)
    {
         throw new NotImplementedException();
    }

    void nsIPrompt.Alert(string dialogTitle, string text)
    {

    }

    void nsIPrompt.AlertCheck(string dialogTitle, string text, string checkMsg, ref bool checkValue)
    {

    }

    bool nsIPrompt.Confirm(string dialogTitle, string text)
    {
        throw new NotImplementedException();
    }

    bool nsIPrompt.ConfirmCheck(string dialogTitle, string text, string checkMsg, ref bool checkValue)
    {
         throw new NotImplementedException();
    }

    int nsIPrompt.ConfirmEx(string dialogTitle, string text, uint buttonFlags, string button0Title, string button1Title, string button2Title, string checkMsg, ref bool checkValue)
    {
         throw new NotImplementedException();
    }

    bool nsIPrompt.Prompt(string dialogTitle, string text, ref string value, string checkMsg, ref bool checkValue)
    {
         throw new NotImplementedException();
    }

    bool nsIPrompt.PromptPassword(string dialogTitle, string text, ref string password, string checkMsg, ref bool checkValue)
    {
         throw new NotImplementedException();
    }

    bool nsIPrompt.PromptUsernameAndPassword(string dialogTitle, string text, ref string username, ref string password, string checkMsg, ref bool checkValue)
    {
         throw new NotImplementedException();
    }

    bool nsIPrompt.Select(string dialogTitle, string text, uint count, IntPtr[] selectList, ref int outSelection)
    {
         throw new NotImplementedException();
    }
}
person Pierpaolo Jimmy Angelini    schedule 14.05.2018