是否可以在执行任何浏览器时运行代码?

时间:2016-01-25 06:39:09

标签: python vb.net batch-file vbscript

我是新来的&进入编程。 也许我的问题听起来很愚蠢 - 无论是非常简单还是根本不可能...... 无论如何,有可能睡眠"直到任何浏览器执行/活动? 我不介意它是vb,vbs,bat,python ...... 谢谢!

1 个答案:

答案 0 :(得分:1)

如果启动的程序数量(chrome.exe,firefox.exe,iexplore.exe等),此vbscript每15分钟检查一次是否发现任何内容,以便启动它。

If AppPrevInstance() Then   
     WScript.Echo "Instance already running"   
     WScript.Quit   
 Else   
     Do   
         Call Main(Array("%ProgramFiles%\Internet Explorer\iexplore.exe", "%ProgramFiles%\Mozilla Firefox\Firefox.exe", "%ProgramFiles%\Google\Chrome\Application\chrome.exe"))  ', 
         Call Pause(15)   
     Loop   
 End If   

 Sub Main(colProcessPaths)   
     Dim ProcessPath   

     For Each ProcessPath In colProcessPaths   
         CheckProcess(ProcessPath)   
     Next   
 End Sub   
  '*********************************************************************************************
 Sub CheckProcess(ProcessPath)   
     Dim ProcessName : ProcessName = StripProcPath(ProcessPath)   
     With GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")   
         With .ExecQuery("SELECT * FROM Win32_Process WHERE Commandline LIKE " &  CommandLineLike(ProcessName))   
             If .Count = 0 Then    
                 With CreateObject("WScript.Shell")  
                     WScript.Echo ProcessPath & vbCrLf & CommandLineLike(ProcessName) & vbCrLf & CommandLineLike(ProcessName) & vbCrLf 
                     .Run DblQuote(ProcessPath)   
                 End With   
             Else    
                 Exit Sub    
             End if   
         End With   
     End With   
 End Sub   
  '*********************************************************************************************
 Function AppPrevInstance()   
     With GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")   
         With .ExecQuery("SELECT * FROM Win32_Process WHERE CommandLine LIKE " & CommandLineLike(WScript.ScriptFullName) & " AND CommandLine LIKE '%WScript%' OR CommandLine LIKE '%cscript%'")   
             AppPrevInstance = (.Count > 1)   
         End With   
     End With   
 End Function   
  '*********************************************************************************************
 Sub Pause(Minutes)    
     Wscript.Sleep(Minutes*1000*60)    
 End Sub   
  '*********************************************************************************************
 Function StripProcPath(ProcessPath)   
     Dim arrStr : arrStr = Split(ProcessPath, "\")   
     StripProcPath = arrStr(UBound(arrStr))   
 End Function   
  '*********************************************************************************************
 Function CommandLineLike(ProcessPath)   
     ProcessPath = Replace(ProcessPath, "\", "\\")   
     CommandLineLike = "'%" & ProcessPath & "%'"   
 End Function
 '*********************************************************************************************
Function DblQuote(Str)
    DblQuote = Chr(34) & Str & Chr(34)
End Function
'**********************************************************************************************