private void UpdateExplorerWindows()
		{
			if (_explorerWindows != null)
				return;

			var shellWindows = new ShellWindows();

			_explorerWindows = shellWindows.Cast<InternetExplorer>().ToArray();
		}
Ejemplo n.º 2
0
        private void UpdateExplorerWindows()
        {
            if (_explorerWindows != null)
            {
                return;
            }

            var shellWindows = new ShellWindows();

            _explorerWindows = shellWindows.Cast <InternetExplorer>().ToArray();
        }
Ejemplo n.º 3
0
        private void NavigateIEShell(string url, string title)
        {
            try
            {
                _logger.Trace("navigate the url. URL: " + url);

                var newBrowser = (SHDocVw.WebBrowser)(new SHDocVw.InternetExplorer());

                Uri uri = new Uri(url);
                SHDocVw.ShellWindows shellWindows = new ShellWindows();

                if (shellWindows != null && ieProcessID != 0 && shellWindows.Cast <SHDocVw.IWebBrowser2>().Any(x => x.HWND == ieProcessID))
                {
                    _logger.Trace("The exist window found to the machine");

                    var windows = shellWindows.Cast <SHDocVw.IWebBrowser2>().SingleOrDefault(x => x.HWND == ieProcessID);

                    if (windows.Document != null)
                    {
                        _logger.Trace("Going to retrieve the document from the browser.");

                        HTMLDocument htmlDoc = (HTMLDocument)windows.Document;

                        _logger.Trace("The document has been retrieved from the browser window.");
                        _logger.Trace("Going to inject the script to navigate the page.");

                        //IHTMLElementCollection nodes = htmlDoc.getElementsByTagName("script");
                        //if (nodes != null && nodes.length > 0)
                        //{
                        //    _logger.Trace("The script tag found in the document.");
                        //    foreach (IHTMLScriptElement scriptElement in nodes)
                        //    {
                        //        scriptElement.text = scriptElement.text + "window.location='" + url + "';";
                        //        _logger.Trace("Script injected successfully.");
                        //        break;
                        //    }
                        //}
                        //else
                        //{
                        //_logger.Trace("The script tag not found in the document.");

                        var scriptErrorSuppressed = (IHTMLScriptElement)htmlDoc.createElement("SCRIPT");
                        scriptErrorSuppressed.type = "text/javascript";
                        //scriptErrorSuppressed.text = "window.location='" + url + "';";
                        scriptErrorSuppressed.text = " window.open('" + url + "', '_self','" + title + "');";

                        IHTMLElementCollection headElement = htmlDoc.getElementsByTagName("head");
                        foreach (IHTMLElement elem in headElement)
                        {
                            var head = (HTMLHeadElement)elem;
                            head.appendChild((IHTMLDOMNode)scriptErrorSuppressed);
                            _logger.Trace("Script injected successfully.");
                            break;
                        }
                        //}
                    }
                    else
                    {
                        _logger.Warn("The document is null to the window");
                    }

                    //_logger.Trace("Going to navigate the URL in the old window - " + windows.HWND);

                    //windows.Navigate(url);

                    //_logger.Trace("The URL navigated in the old window.");
                }
                else
                {
                    _logger.Trace("The existing window is not found. So going to open new window.");

                    var newBrowser = (SHDocVw.WebBrowser)(new SHDocVw.InternetExplorer());
                    //newBrowser.Name = "HimmsMainWindow";

                    _logger.Trace("Going to navigate the URL in the new window.");

                    newBrowser.Navigate(url);
                    var inptr = new IntPtr(newBrowser.HWND);
                    ieProcessID = newBrowser.HWND;
                    ShowWindow(inptr, 9);

                    _logger.Trace("The URL navigated in the new window.");
                }
            }
            catch (Exception generalException)
            {
                _logger.Error("Error occurred as " + generalException.Message);
                _logger.Trace("Error Trace : " + generalException.StackTrace);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        ///     エクスプローラのウインドウをクリーンします。
        /// </summary>
        /// <returns>クローズしたウインドウの名前リスト</returns>
        public void Clean()
        {
            var isUpdated    = false;
            var cleanedInfos = new List <ExplorerCleanedInfo>();

            var closedExplorerHandleList = _explorerDic.Keys.ToList();

            _log.Debug("ShellWindows[{0}]", _shellWindows.Count);

            var userApps = IsShowApplication
                ? Process.GetProcesses()
                           .Where(x => x.MainWindowHandle != IntPtr.Zero)
                           .Select(x => new UserApplication(x))
                           .Where(x => !x.IsExplorer && !x.IsUnknown)
                : Enumerable.Empty <UserApplication>();

            var ies = _shellWindows.Cast <InternetExplorer>().Concat(userApps);

            foreach (var ie in ies)
            {
                if (ie.FullName == null)
                {
                    continue;
                }
                var handle = 0;
                try
                {
                    handle = ie.HWND;
                }
                catch (COMException comex)
                {
                    // IEのコンポーネントを使用しているアプリを拾った場合、COMExceptionが出る場合があるので出た場合はそのプロセスはスキップする。
                    _log.Debug(comex, "COM Error!! FullName:{0}", ie.FullName);
                }
                if (handle == 0)
                {
                    continue;
                }
                if (!_explorerDic.Keys.Contains(handle))
                {
                    var explorer = new Explorer(ie);
                    if (_appConfig.IsKeepPin)
                    {
                        explorer.PinLocationChanged += (sender, pinexp) =>
                        {
                            // ピン留めでパスが変わったらピン留めのパスを開く(キープ)
                            OpenExplorer(pinexp, true);
                            // ピンキープのためにキーを保存
                            _pinedRestoreHashSet.Add(pinexp.LocationKey);
                        };

                        // ピン復元
                        if (_pinedRestoreHashSet.Contains(explorer.LocationKey))
                        {
                            explorer.IsPined = true;
                            _pinedRestoreHashSet.Remove(explorer.LocationKey);
                        }
                    }

                    RegistExplorer(explorer);
                }
                else
                {
                    closedExplorerHandleList.Remove(handle);

                    // よくわからないけどここでKeyNotFoundが出る場合があるのでTryGetする。
                    Explorer explorer;
                    if (_explorerDic.TryGetValue(handle, out explorer))
                    {
                        bool updated;
                        if (_appConfig.IsKeepPin)
                        {
                            updated = explorer.UpdateWithKeepPin(ie);
                        }
                        else
                        {
                            updated = explorer.Update(ie);
                        }
                        if (updated)
                        {
                            isUpdated = true;
                        }
                    }
                }
            }

            // すでに終了されたものがあればディクショナリから削除
            foreach (var closedHandle in closedExplorerHandleList)
            {
                Explorer explorer;
                if (_explorerDic.TryGetValue(closedHandle, out explorer))
                {
                    CloseExplorer(explorer, ExplorerCloseReason.Terminated);
                    _explorerDic.Remove(closedHandle);
                }
            }

            var duplicateExplorers = _explorerDic.Where(x => x.Value.IsExplorer)
                                     .GroupBy(g => g.Value.LocationKey)
                                     .Select(g => new { Explorer = g, Count = g.Count() })
                                     .Where(x => x.Count > 1).ToArray(); // Count > 1はいらないけど、旧バージョンからの互換のためにしばらく残す

            // 同じパスのがあれば一番新しいもの以外は終了させる
            foreach (var duplicateExplorer in duplicateExplorers)
            {
                var closeTargets = duplicateExplorer.Explorer
                                   .Where(
                    x =>
                    x.Value.LastUpdateDateTime !=
                    duplicateExplorer.Explorer.Max(m => m.Value.LastUpdateDateTime))
                                   .Select(x => x.Value)
                                   .ToArray();
                foreach (var closeTarget in closeTargets)
                {
                    var closeReason = ExplorerCloseReason.Duplication;
                    if (CloseExplorer(closeTarget, closeReason))
                    {
                        cleanedInfos.Add(new ExplorerCleanedInfo(closeTarget.LocationName, closeReason));
                    }
                }
            }

            // 期限切れのものがあれば閉じる
            if (_appConfig.IsAutoCloseUnused && _appConfig.ExpireInterval != TimeSpan.Zero)
            {
                _expireDateTime = DateTime.Now.Subtract(_appConfig.ExpireInterval);
                _log.Debug("Expire Datetime {0}", _expireDateTime);

                var explorers = _explorerDic.Values.Where(x => x.IsExplorer).ToArray();
                foreach (var expireExplorer in explorers.Where(x => x.LastUpdateDateTime < _expireDateTime))
                {
                    _log.Debug("Expire Explorer {0}", expireExplorer);
                    var closeReason = ExplorerCloseReason.Expiration;
                    if (CloseExplorer(expireExplorer, closeReason))
                    {
                        cleanedInfos.Add(new ExplorerCleanedInfo(expireExplorer.LocationName, closeReason));
                    }
                }
            }

            // 表示用データ更新
            var updatedView = UpdateView();

            if (updatedView)
            {
                isUpdated = true;
            }

            Save();

            if (WindowCount > _maxWindowCount)
            {
                _maxWindowCount = WindowCount;
            }
            _totalCloseWindowCount += cleanedInfos.Count;

            OnCleaned(cleanedInfos, isUpdated);
        }
Ejemplo n.º 5
0
        public static void Main(string[] args)
        {
            ShellWindows allBrowsers = new ShellWindows();

            Console.WriteLine($"IE Processes: {allBrowsers.Count}");

            var nternetExplorers = allBrowsers
                                   .Cast <InternetExplorer>()
                                   .ToArray();

            var changingBrowsers =
                nternetExplorers
                .Select((ie, index) =>
            {
//                    return Task.Run(() =>
//                    {
//                        ie.Navigate("https://redmine.x-code.pl");
//                        HTMLDocument ieDocument = ie.Document;
//
//                        var scriptTag = (IHTMLScriptElement)ieDocument.createElement("script");
//
//                        scriptTag.type = "text/javascript";
//                        scriptTag.src =
//                            @"function test(str){ alert('you clicked' + str);}";
//
//                        var head = (IHTMLElement)((IHTMLElementCollection)ieDocument.all.tags("head")).item(null, 0);
//
//                        ((HTMLHeadElement) head).appendChild((IHTMLDOMNode) scriptTag);
//
//                        ieDocument.parentWindow.execScript("test('siema eniu');");
//                    });

                return(StartSTATask(() =>
                {
                    ie.Navigate("https://google.pl");
                    HTMLDocument ieDocument = ie.Document;

                    var scriptTag = (IHTMLScriptElement)ieDocument.createElement("script");

                    scriptTag.type = "text/javascript";
                    scriptTag.src =
                        @"function test(str){ alert('you clicked' + str);}";

                    var head = (IHTMLElement)((IHTMLElementCollection)ieDocument.all.tags("head")).item(null, 0);

                    ((HTMLHeadElement)head).appendChild((IHTMLDOMNode)scriptTag);

                    ieDocument.parentWindow.execScript("test('siema eniu');");
                }));
            })
                .ToArray();

            Task.WaitAll(changingBrowsers);

            foreach (var nternetExplorer in nternetExplorers)
            {
                nternetExplorer.Visible = true;
            }

            Console.WriteLine();
        }