【问题标题】:Disable JavaScript Alerts Gecko45 C#禁用 JavaScript 警报 Gecko45 C#
【发布时间】:2017-05-25 18:32:16
【问题描述】:

我想禁用所有 js 警报。此代码不起作用壁虎 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);
    }
};

【问题讨论】:

  • 呃……这甚至是 Javascript 吗?我不这么认为。您可以尝试用 alert = function(){ } 之类的东西覆盖 alert
  • @JeremyThille 除非 OP 编辑​​了他们的问题,这就是他们正在尝试的......

标签: javascript c# gecko geckofx alerts


【解决方案1】:

如果您想禁用所有警报,则需要实现自己的 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();

【讨论】:

    【解决方案2】:

    这是我必须添加的完整课程。这是我为 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();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-03
      • 1970-01-01
      • 1970-01-01
      • 2018-06-10
      • 1970-01-01
      相关资源
      最近更新 更多