Пример #1
0
        /// <summary>
        /// Register a listener for a custom jscrip-initiated MessageEvent
        /// https://developer.mozilla.org/en/DOM/document.createEvent
        /// http://help.dottoro.com/ljknkjqd.php
        /// </summary>
        /// <param name="eventName"></param>
        /// <param name="action"></param>
        /// <example>AddMessageEventListener("callMe", (message=>MessageBox.Show(message)));</example>
        public void AddMessageEventListener(string eventName, Action <string> action, bool useCapture)
        {
            nsIDOMEventTarget target = Xpcom.QueryInterface <nsIDOMEventTarget>(Xpcom.QueryInterface <nsIDOMWindow>(_webBrowser.Instance.GetContentDOMWindowAttribute()).GetWindowRootAttribute());

            if (target != null)
            {
                // the argc parameter is the number of optionial argumetns we are passing.
                // (useCapture and wantsUntrusted are specified as optional so we always pass 2 when calling interface from C#)
                target.AddEventListener(new nsAString(eventName), this, /*Review*/ useCapture, true, 2);
                _messageEventListeners.Add(eventName, action);
            }
        }
        protected override void OnHandleCreated(EventArgs e)
        {
#if GTK
            if (Xpcom.IsMono)
            {
                base.OnHandleCreated(e);
                m_wrapper.Init();
            }
#endif
            if (!this.DesignMode)
            {
                Xpcom.Initialize();
                WindowCreator.Register();
#if !GTK
                LauncherDialogFactory.Register();
#endif

                WebBrowser      = Xpcom.CreateInstance <nsIWebBrowser>("@mozilla.org/embedding/browser/nsWebBrowser;1");
                WebBrowserFocus = (nsIWebBrowserFocus)WebBrowser;
                BaseWindow      = (nsIBaseWindow)WebBrowser;

                WebNav = (nsIWebNavigation)WebBrowser;

                WebBrowser.SetContainerWindowAttribute(this);
#if GTK
                if (Xpcom.IsMono)
                {
                    BaseWindow.InitWindow(m_wrapper.BrowserWindow.Handle, IntPtr.Zero, 0, 0, this.Width, this.Height);
                }
                else
#endif
                BaseWindow.InitWindow(this.Handle, IntPtr.Zero, 0, 0, this.Width, this.Height);


                RecordNewJsContext();
                BaseWindow.Create();

                Guid nsIWebProgressListenerGUID = typeof(nsIWebProgressListener).GUID;
                WebBrowser.AddWebBrowserListener(this.GetWeakReference(), ref nsIWebProgressListenerGUID);

                if (UseHttpActivityObserver)
                {
                    ObserverService.AddObserver(this, ObserverNotifications.HttpRequests.HttpOnModifyRequest, false);

                    nsIHttpActivityDistributor activityDistributor = Xpcom.GetService <nsIHttpActivityDistributor>("@mozilla.org/network/http-activity-distributor;1");
                    activityDistributor = Xpcom.QueryInterface <nsIHttpActivityDistributor>(activityDistributor);
                    activityDistributor.AddObserver(this);
                }

                // var domEventListener = new GeckoDOMEventListener(this);

                _target = Xpcom.QueryInterface <nsIDOMWindow>(WebBrowser.GetContentDOMWindowAttribute()).GetWindowRootAttribute();

                _target.AddEventListener(new nsAString("submit"), this, true, true, 2);
                _target.AddEventListener(new nsAString("keydown"), this, true, true, 2);
                _target.AddEventListener(new nsAString("keyup"), this, true, true, 2);
                _target.AddEventListener(new nsAString("keypress"), this, true, true, 2);
                _target.AddEventListener(new nsAString("mousemove"), this, true, true, 2);
                _target.AddEventListener(new nsAString("mouseover"), this, true, true, 2);
                _target.AddEventListener(new nsAString("mouseout"), this, true, true, 2);
                _target.AddEventListener(new nsAString("mousedown"), this, true, true, 2);
                _target.AddEventListener(new nsAString("mouseup"), this, true, true, 2);
                _target.AddEventListener(new nsAString("click"), this, true, true, 2);
                _target.AddEventListener(new nsAString("dblclick"), this, true, true, 2);
                _target.AddEventListener(new nsAString("compositionstart"), this, true, true, 2);
                _target.AddEventListener(new nsAString("compositionend"), this, true, true, 2);
                _target.AddEventListener(new nsAString("contextmenu"), this, true, true, 2);
                _target.AddEventListener(new nsAString("DOMMouseScroll"), this, true, true, 2);
                _target.AddEventListener(new nsAString("focus"), this, true, true, 2);
                _target.AddEventListener(new nsAString("blur"), this, true, true, 2);
                // Load event added here rather than DOMDocument as DOMDocument recreated when navigating
                // ths losing attached listener.
                _target.AddEventListener(new nsAString("load"), this, true, true, 2);
                _target.AddEventListener(new nsAString("change"), this, true, true, 2);

                // history
                {
                    var sessionHistory = WebNav.GetSessionHistoryAttribute();
                    if (sessionHistory != null)
                    {
                        sessionHistory.AddSHistoryListener(this);
                    }
                }

                BaseWindow.SetVisibilityAttribute(true);

                // this fix prevents the browser from crashing if the first page loaded is invalid (missing file, invalid URL, etc)
                if (Document != null)
                {
                    // only for html documents
                    Document.Cookie = "";
                }
            }

            base.OnHandleCreated(e);
        }