static void Main(string[] args) { PathType pathType = PathType.None; string parsedPath = null; var screenBounds = Screen.PrimaryScreen.Bounds; GImgClicks gimgClicks = null; foreach (var supported in GImgClicksSupported) { if (supported.ScreenResolution.Width == screenBounds.Width && supported.ScreenResolution.Height == screenBounds.Height) { gimgClicks = supported; break; } } if (gimgClicks == null) { MessageBox.Show("Error: Screen resolution not supported.", "GoSearch"); return; } Utility.RunAsSTAThread(() => { if (Clipboard.ContainsText()) { var s = Clipboard.GetText(); if (s.StartsWith("http://") || s.StartsWith("https://")) { pathType = PathType.WebUrl; return; } else if (File.Exists(s)) { pathType = PathType.LocalFile; return; } } // Note The WPF version of Clipboard.GetImage() is buggy if (Clipboard.ContainsImage()) { var image = Clipboard.GetImage(); parsedPath = Path.Combine(Path.GetTempPath(), "temp-gs.png"); pathType = PathType.LocalFile; image.Save(parsedPath, System.Drawing.Imaging.ImageFormat.Png); return; } if (Clipboard.ContainsFileDropList()) { var fs = Clipboard.GetFileDropList(); parsedPath = fs[0]; pathType = PathType.LocalFile; } }); if (pathType == PathType.None) { MessageBox.Show("Error: No valid path copied.", "GoSearch"); return; } var w = new ControlledWindow { ProcessPath = @"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe", Arguments = @"-inprivate http://images.google.com" }; void SleepAndCheck(int sleepMs = 500) { Thread.Sleep(500); if (w.Process.HasExited) { throw new TargetWindowExited(); } } if (!w.CreateNew(x => x.StartsWith("Google Images"))) { MessageBox.Show("Error: Failed to create new browser window.", "GoSearch"); return; } try { if (!w.Process.HasExited && w.WindowRect.HasValue) { if (!w.WaitUntilTitleStartsWith("Google Images")) { MessageBox.Show("Error: Failed to load Google Images site.", "GoSearch"); return; } if (w.Process.HasExited) { return; } //var bounds = System.Windows.Forms.Screen.PrimaryScreen.Bounds; //const int StandardResolutionWidth = 1920; //const int StandardResolutionHeight = 1080; //const int StandardClickX = 1200; //const int StandardClickY = 550; //if (bounds.Width == StandardResolutionHeight && bounds.Height == StandardResolutionWidth) //{ // Input.MouseClick(1200, 550); //} //else //{ // var x = StandardClickX * bounds.Width / StandardResolutionWidth; // var y = StandardClickY * bounds.Height / StandardResolutionHeight; // Input.MouseClick(x, y); //} Input.MouseClick(gimgClicks.CameraButton.X, gimgClicks.CameraButton.Y); SleepAndCheck(); if (w.Process.HasExited) { return; } if (pathType == PathType.WebUrl) { Debug.Assert(parsedPath == null); CtrlV(); } else if (pathType == PathType.LocalFile) { Input.MouseClick(gimgClicks.UploadImageTab.X, gimgClicks.UploadImageTab.Y); SleepAndCheck(); Input.MouseClick(gimgClicks.ChooseFileButton.X, gimgClicks.ChooseFileButton.Y); SleepAndCheck(); if (parsedPath != null) { Input.TypeStr("\"" + parsedPath + "\"\n"); } else { CtrlV(); } } } } catch (TargetWindowExited) { MessageBox.Show("Error: Search window lost.", "GoSearch"); } }
/// <summary> /// /// </summary> /// <param name="args"></param> /// <remarks> /// References: /// 1. https://stackoverflow.com/questions/2251578/how-do-i-get-the-selected-text-from-the-focused-window-using-native-win32-api /// /// </remarks> static void Main() { // http://www.pingshu8.com/down_95241.html // http://www.pingshu8.com/down_96003.html const string MsEdgePath = @"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"; const string FdmPath = @"C:\Program Files\FreeDownloadManager.ORG\Free Download Manager\fdm.exe"; const int ItemStartIndex = 0; const int ItemTotalCount = 763; const int MaxDownloadPageAttempts = 10; Input.ForceCurrentWindowBeforeSend = true; var fdm = new ControlledWindow { ProcessPath = FdmPath }; fdm.SwitchToOnlyOne(true, x => x == "Free Download Manager"); new ControlledWindow { ProcessPath = MsEdgePath, }.SwitchToOnlyOne(true); for (var i = ItemStartIndex; i < ItemTotalCount; i++) { var url = $"http://www.pingshu8.com/down_{95241 + i}.html"; Console.WriteLine($"Retrieving item {i}..."); var entryPage = new ControlledWindow { ProcessPath = MsEdgePath, Arguments = url }; entryPage.CreateNew(x => x.Contains("在线收听")); ControlledWindow downloadPage = null; Thread.Sleep(100); var attempts = 0; for (; downloadPage == null && attempts < MaxDownloadPageAttempts; attempts++) { downloadPage = ControlledWindow.Capture(ControlledWindow.ProcessPathToName(MsEdgePath), () => { Input.MouseClick(410, 420); Thread.Sleep(100); }); } if (downloadPage == null) { Console.WriteLine($"Error: unable to retrieve file download page after {attempts} attempts."); break; } else if (attempts > 1) { Console.WriteLine($"Info: {attempts} attempts attempted retriving file download page."); } downloadPage.SwitchToThisWindow(); Input.SendKeys( new[] { new Input.Key { ScanCode = ScanCodeShort.LMENU }, new Input.Key { ScanCode = ScanCodeShort.KEY_D }, new Input.Key { ScanCode = ScanCodeShort.KEY_D, Up = true }, new Input.Key { ScanCode = ScanCodeShort.LMENU, Up = true } }); Utility.RunAsSTAThread(() => { Clipboard.Clear(); }); Thread.Sleep(200); while (true) { Input.SendKeys(new[] { new Input.Key { ScanCode = ScanCodeShort.LCONTROL }, new Input.Key { ScanCode = ScanCodeShort.KEY_C }, new Input.Key { ScanCode = ScanCodeShort.KEY_C, Up = true }, new Input.Key { ScanCode = ScanCodeShort.LCONTROL, Up = true } }); Thread.Sleep(200); string text = ""; Utility.RunAsSTAThread(() => { text = Clipboard.GetText(); }); if (text.Contains("down01.pingshu88.com")) { downloadPage.SoftCloseWindow(); Thread.Sleep(200); fdm.SwitchToThisWindow(); Thread.Sleep(100); Input.MouseClick(40, 40); Thread.Sleep(100); Input.SendKeys(new[] { new Input.Key { ScanCode = ScanCodeShort.RETURN }, new Input.Key { ScanCode = ScanCodeShort.RETURN, Up = true } }); Input.SendKeys(new[] { new Input.Key { ScanCode = ScanCodeShort.RETURN }, new Input.Key { ScanCode = ScanCodeShort.RETURN, Up = true } }); break; } } Thread.Sleep(250); entryPage.SoftCloseWindow(); Console.WriteLine($"Item {i} retrieved."); Thread.Sleep(1000); } }