int IObjectWithSite.SetSite(object site) { try { if (_prefs == null) _prefs = new Db(this); // Only need to check for install once per run. if (!_installChecked) { CheckInstall(); var u = new Thread(CheckUpdate); u.SetApartmentState(ApartmentState.STA); u.Start(); var s = new Thread(CheckScriptUpdate); s.SetApartmentState(ApartmentState.STA); s.Start(); } if (_link == null) { _link = new ScriptmonkeyLinkManager(_prefs.Settings.ScriptmonkeyLinkUrl, _prefs.Settings.ScriptmonkeyLinkPollDelay); _link.OnReceiveEvent += OnReceiveLinkEvent; } } catch (Exception ex) { if (LogAndCheckDebugger(ex, "SetSite:Setup")) throw; } _site = site; _normalLoad = true; _refresh = false; if (!Application.RenderWithVisualStyles) Application.EnableVisualStyles(); if (site != null) { try { var serviceProv = (IServiceProvider) _site; var guidIWebBrowserApp = Marshal.GenerateGuidForType(typeof (IWebBrowserApp)); var guidIWebBrowser2 = Marshal.GenerateGuidForType(typeof (IWebBrowser2)); IntPtr intPtr; serviceProv.QueryService(ref guidIWebBrowserApp, ref guidIWebBrowser2, out intPtr); _browser = Marshal.GetObjectForIUnknown(intPtr) as IWebBrowser2; } catch (Exception ex) { if (LogAndCheckDebugger(ex, "SetSite:TrySetBrowserObject")) throw; } if (_browser == null) return 0; // Ensure the handlers are not already added ((DWebBrowserEvents2_Event)_browser).DocumentComplete -= Run; ((DWebBrowserEvents2_Event)_browser).BeforeNavigate2 -= BeforeNavigate; ((DWebBrowserEvents2_Event)_browser).NavigateComplete2 -= NavigateComplete2; ((DWebBrowserEvents2_Event)_browser).DownloadComplete -= DownloadComplete; ((DWebBrowserEvents2_Event)_browser).DocumentComplete += Run; ((DWebBrowserEvents2_Event)_browser).BeforeNavigate2 += BeforeNavigate; if (_prefs != null && _prefs.Settings.RunOnPageRefresh) { ((DWebBrowserEvents2_Event)_browser).NavigateComplete2 += NavigateComplete2; ((DWebBrowserEvents2_Event)_browser).DownloadComplete += DownloadComplete; } } else { // No site. Remove handlers if (_browser == null) return 0; ((DWebBrowserEvents2_Event) _browser).DocumentComplete -= Run; ((DWebBrowserEvents2_Event) _browser).BeforeNavigate2 -= BeforeNavigate; if (_prefs.Settings.RunOnPageRefresh) { ((DWebBrowserEvents2_Event) _browser).NavigateComplete2 -= NavigateComplete2; ((DWebBrowserEvents2_Event) _browser).DownloadComplete -= DownloadComplete; } _browser = null; } return 0; }
/// <summary> /// Event handler for Scriptmonkey Link /// </summary> /// <param name="action">The action to perform</param> private void OnReceiveLinkEvent(string action) { if (action == "refresh" && _prefs.Settings.AllowedScriptmonkeyLinkCommands.refresh) _prefs.ReloadData(); else if (action == "disableLink" && _prefs.Settings.AllowedScriptmonkeyLinkCommands.disableLink) { _link.Dispose(); _link = null; } else if (action == "testLink" && _prefs.Settings.AllowedScriptmonkeyLinkCommands.testLink) LogAndCheckDebugger(null, "Scriptmonkey Link alert: " + (_currentUrl ?? "")); else if (action == "verify" && _prefs.Settings.AllowedScriptmonkeyLinkCommands.verify) _link.Verify(_currentUrl.ToString()); else if (action == "refreshCache" && _prefs.Settings.AllowedScriptmonkeyLinkCommands.refreshCache) _scriptCache.Clear(); }