get windows browser url

This commit is contained in:
antelle 2016-04-24 19:57:01 +03:00
parent 1857244d1e
commit 9028232664
3 changed files with 60 additions and 6 deletions

View File

@ -55,6 +55,8 @@
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Windows.Forms" />
<Reference Include="UIAutomationClient" />
<Reference Include="UIAutomationTypes" />
</ItemGroup>
<ItemGroup>
<Compile Include="InputCommands\CopyPasteCommand.cs" />

View File

@ -8,6 +8,7 @@ namespace KeeWebHelper
[STAThread]
static void Main(string[] args)
{
var sw = System.Diagnostics.Stopwatch.StartNew();
Console.InputEncoding = Encoding.UTF8;
Console.OutputEncoding = Encoding.UTF8;
if (args.Length > 0 && args[0] == "--window-info")
@ -16,8 +17,6 @@ namespace KeeWebHelper
return;
}
var printTime = args.Length > 0 && args[0] == "--print-time";
var sw = System.Diagnostics.Stopwatch.StartNew();
Console.WriteLine("Start: " + sw.ElapsedMilliseconds);
while (true)
{
var cmd = InputParser.Next();
@ -36,8 +35,7 @@ namespace KeeWebHelper
static void GetWindowInfo()
{
var windowInfo = WindowHelper.GetActiveWindowInfo();
Console.WriteLine(windowInfo.Title);
Console.WriteLine(windowInfo.Url);
Console.WriteLine("{0}\n{1}", windowInfo.Title, windowInfo.Url);
}
}
}

View File

@ -1,6 +1,8 @@
using System;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Windows.Automation;
namespace KeeWebHelper
{
@ -12,18 +14,70 @@ namespace KeeWebHelper
[DllImport("user32.dll")]
static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
[DllImport("user32.dll", SetLastError = true)]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint processId);
public static WindowInfo GetActiveWindowInfo()
{
const int nChars = 2048;
StringBuilder Buff = new StringBuilder(nChars);
IntPtr handle = GetForegroundWindow();
IntPtr hwnd = GetForegroundWindow();
var result = new WindowInfo();
if (GetWindowText(handle, Buff, nChars) > 0)
if (GetWindowText(hwnd, Buff, nChars) > 0)
{
result.Title = Buff.ToString();
}
try
{
result.Url = GetUrl(hwnd);
}
catch { }
return result;
}
static string GetUrl(IntPtr hwnd)
{
uint pid = 0;
GetWindowThreadProcessId(hwnd, out pid);
if (pid <= 0)
{
return null;
}
var process = Process.GetProcessById((int)pid);
return GetUrlAcc(process);
}
static string GetUrlAcc(Process process)
{
var el = AutomationElement.FromHandle(process.MainWindowHandle);
if (el == null)
{
return null;
}
if (process.ProcessName.IndexOf("chrome", StringComparison.OrdinalIgnoreCase) >= 0)
{
var elChr = el.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "Google Chrome"));
if (elChr != null)
{
el = elChr;
}
}
var controlCondition = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit);
var valueCondition = new PropertyCondition(AutomationElement.IsValuePatternAvailableProperty, true);
var condition = new AndCondition(controlCondition, valueCondition);
AutomationElement urlEl = el.FindFirst(TreeScope.Descendants, condition);
if (urlEl == null)
{
return null;
}
var val = (ValuePattern)urlEl.GetCurrentPattern(ValuePattern.Pattern);
return val.Current.Value;
}
}
}