Archived Boards > WinBatch Script Exchange

Retrieve Current URL from Chrome - .NET

(1/3) > >>

JTaylor:
Here is code for retrieving the Current URL from Chrome.   I used Deana's excellent example of compiling C# code in-memory so I could create a Method for this purpose.

Jim


--- Code: Winbatch ---
;************************************************************************************************************
;**  This retrieves the Current URL from Chrome Version 35.
;**  I know it won't work with 32 (it will with a minor tweak) due to changes somewhere along the way to 35.
;**  Guessing it will break again soon but offers an approach that could be modified as versions change.
;************************************************************************************************************

  If !AppExist("chrome.exe",0, 0) Then
    Display(2,"Note","Chrome is not running.")
    Exit
  EndIf

  GoSub Load_Routines
  Load_GetMyURL()
  url_save = ""

  While 1
    url   = sha1.GetMyURL()
    If url != url_save Then Display(2,"URL",url)
    url_save = url
    TimeDelay(3)
  EndWhile

Exit


:LOAD_ROUTINES

#DefineSubRoutine Load_GetMyURL()

;***************************************************************************
;**  Run C# in memory using WinBatch - Use Namespace.Class defined in CSharpeSource
;**
;** Purpose:  Run C# in memory using WinBatch - Using Namespace.Class defined in CSharpeSource
;** Inputs:
;** Outputs: Results in message
;**
;** Developer: Deana Falk 2014.04.14
;***************************************************************************

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Load assemblies into the WinBatch process.
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; mscorlib assembly is automatically loaded by WinBatch when the CLR is loaded.
; ObjectClrOption ('use','mscorlib, version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')

; ObjectClrOption("version", "v2.0.50727")
; ObjectClrOption("use","System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
  ObjectClrOption ( 'use', 'System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Compiles the c# code in Memory
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  objCSharp = ObjectClrNew('Microsoft.CSharp.CSharpCodeProvider')
  objParams = ObjectClrNew('System.CodeDom.Compiler.CompilerParameters')
  objParams.GenerateInMemory = ObjectType( "VT_BOOL", 1 ) ;TRUE
 
  objParams.ReferencedAssemblies.Add("System.dll")
  objParams.ReferencedAssemblies.Add("C:\Windows\Microsoft.NET\assembly\GAC_MSIL\UIAutomationTypes\v4.0_4.0.0.0__31bf3856ad364e35\UIAutomationTypes.dll")
  objParams.ReferencedAssemblies.Add("C:\Windows\Microsoft.NET\assembly\GAC_MSIL\UIAutomationClient\v4.0_4.0.0.0__31bf3856ad364e35\UIAutomationClient.dll")

;Make sure the cSharp code includes both namepspace and class that can be later used by ObjectCLRNew
  cSharpSource =               `namespace URLEngine {`:@LF
  cSharpSource = cSharpSource :`using System;`:@LF
  cSharpSource = cSharpSource :`using System.Text;`:@LF
  cSharpSource = cSharpSource :`using System.Threading;`:@LF
  cSharpSource = cSharpSource :`using System.Runtime.InteropServices;`:@LF
  cSharpSource = cSharpSource :`using System.Text.RegularExpressions;`:@LF
  cSharpSource = cSharpSource :`using System.Collections.Generic;`:@LF
  cSharpSource = cSharpSource :`using System.Windows.Automation;`:@LF
  cSharpSource = cSharpSource :`using System.Diagnostics;  `:@LF

  cSharpSource = cSharpSource :`public class MyURLEngine {`:@LF


  cSharpSource = cSharpSource :`public string GetMyURL() `:@LF
  cSharpSource = cSharpSource :`  { `:@LF
  cSharpSource = cSharpSource :`    `:@LF

  cSharpSource = cSharpSource :`  Process[] procsChrome = Process.GetProcessesByName("chrome");  `:@LF

  cSharpSource = cSharpSource :`  int myint = procsChrome.Length;  `:@LF
  cSharpSource = cSharpSource :`  string mystring = myint.ToString();  `:@LF
; cSharpSource = cSharpSource :`  return mystring;  `:@LF
   
  cSharpSource = cSharpSource :`  foreach (Process chrome in procsChrome) {  `:@LF
  cSharpSource = cSharpSource :`  if (chrome.MainWindowHandle == IntPtr.Zero) {  `:@LF
  cSharpSource = cSharpSource :`    continue;  `:@LF
  cSharpSource = cSharpSource :`  } `:@LF
  cSharpSource = cSharpSource :`    `:@LF
  cSharpSource = cSharpSource :`  AutomationElement elm = AutomationElement.FromHandle(chrome.MainWindowHandle);  `:@LF
  cSharpSource = cSharpSource :`    `:@LF
  cSharpSource = cSharpSource :`  AutomationElement elmUrlBar = null;  `:@LF
  cSharpSource = cSharpSource :`  try {  `:@LF
  cSharpSource = cSharpSource :`        var elm1 = elm.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "Google Chrome"));  `:@LF
  cSharpSource = cSharpSource :`        if (elm1 == null) { continue; } // not the right chrome.exe  `:@LF
  cSharpSource = cSharpSource :`        var elm2 = TreeWalker.RawViewWalker.GetLastChild(elm1); // I don't know a Condition for this for finding :(  `:@LF
  cSharpSource = cSharpSource :`        var elm3 = elm2.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, ""));  `:@LF
  cSharpSource = cSharpSource :`        var elm4 = TreeWalker.RawViewWalker.GetNextSibling(elm3); // I don't know a Condition for this for finding :(  `:@LF
  cSharpSource = cSharpSource :`        var elm7 = elm4.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.ToolBar));  `:@LF
  cSharpSource = cSharpSource :`        elmUrlBar = elm7.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Custom));  `:@LF
  cSharpSource = cSharpSource :`  } catch {  `:@LF
  cSharpSource = cSharpSource :`             continue;  `:@LF
  cSharpSource = cSharpSource :`  } `:@LF
  cSharpSource = cSharpSource :`    `:@LF
  cSharpSource = cSharpSource :`  if (elmUrlBar == null) {  `:@LF
  cSharpSource = cSharpSource :`    continue;  `:@LF
  cSharpSource = cSharpSource :`  }  `:@LF
  cSharpSource = cSharpSource :`    `:@LF
  cSharpSource = cSharpSource :`     if ((bool)elmUrlBar.GetCurrentPropertyValue(AutomationElement.HasKeyboardFocusProperty)) {  `:@LF
  cSharpSource = cSharpSource :`     continue;  `:@LF
  cSharpSource = cSharpSource :`  } `:@LF
  cSharpSource = cSharpSource :`    `:@LF
  cSharpSource = cSharpSource :`  AutomationPattern[] patterns = elmUrlBar.GetSupportedPatterns();  `:@LF
  cSharpSource = cSharpSource :`  if (patterns.Length == 1) {  `:@LF
  cSharpSource = cSharpSource :`  try {  `:@LF
  cSharpSource = cSharpSource :`        string ret = ((ValuePattern)elmUrlBar.GetCurrentPattern(patterns[0])).Current.Value;  `:@LF
  cSharpSource = cSharpSource :`        return ret;  `:@LF
  cSharpSource = cSharpSource :`  } catch { }  `:@LF
  cSharpSource = cSharpSource :`        continue;  `:@LF
  cSharpSource = cSharpSource :`  }  `:@LF
  cSharpSource = cSharpSource :`  }  `:@LF

  cSharpSource = cSharpSource :` return "";  `:@LF

  cSharpSource = cSharpSource :`     } `:@LF
 
  cSharpSource = cSharpSource :` }`:@LF
  cSharpSource = cSharpSource :`}`:@LF

  objResult = objCSharp.CompileAssemblyFromSource(objParams,cSharpSource)

  ;Compiler Output
  If objResult.Output.Count > 0 Then
    strOutput = ''
    For x = 0 to objResult.Output.Count-1
       If strOutput == "" Then
         strOutput = objResult.Output.Item(x)
       Else
         strOutput = strOutput:@LF:objResult.Output.Item(x)
       EndIf
    Next
    Pause('Compiler Output',strOutput)
  Endif
 
  ; Compiler Errors
  If objResult.Errors.Count > 0 Then
    strErrors = ''
    ForEach ce In objResult.Errors
      ;Pause("Error", ce.ToString())
      If strErrors == "" Then
        strErrors = ce.ToString()
      Else
        strErrors = strErrors:@LF:ce.ToString()
      EndIf
    Next
    Pause('Compiler Errors',strErrors)
    Exit
  EndIf

  sha1 = ObjectClrNew( 'URLEngine.MyURLEngine' )

#EndSubRoutine

Return
 

Deana:
Hmm. I just tested on my Windows 7 system with Google Chrome (35.0.1916.153) running and the code you posted. It always seems to return: Chrome is Not Running.

What Windows platform and chrome version are you using?

JTaylor:
I'm on Windows 7 and Chrome Version 32.0.1700.107.

I'll update and see what happens.   At least you didn't get an error and for me that is quite the accomplishment  ;)

Jim

kdmoyers:
I'm on win7 and Chrome Version 35.0.1916.114 m
I get "Chrome is Not Running" but it definitely is.
Then it loops till you terminate it.
-Kirby

JTaylor:
Yeah...I see that on version 35 as well.   Bummer...had it working but guess something changed on that front between versions.  I'll get it figured out...unless someone beats me to it...I won't be offended if that happens  :)

Jim

Navigation

[0] Message Index

[#] Next page

Go to full version