![]() Process procsChrome = Process.GetProcessesByName("chrome") Method to get the URL of the active window: private static string GetUrlInternal() Getting the current tab's URL from Google Chrome using C# If (listHandle.IsAllocated) listHandle.Free() Ĭonsole.WriteLine("Press any key to continue.") GCHandle listHandle = GCHandle.Alloc(pointers) ĮnumWindows(enumCallback, GCHandle.ToIntPtr(listHandle)) Win32Callback enumCallback = new Win32Callback(EnumWindow) StringBuilder sb = new StringBuilder(length + 1) Int length = GetWindowTextLength(handle) Private static string GetTitle(IntPtr handle) Internal static extern int GetWindowTextLength(IntPtr hwnd) Public static extern int GetWindowText(IntPtr windowHandle, StringBuilder stringBuilder, int nMaxCount) List pointers = GCHandle.FromIntPtr(pointer).Target as List Private static bool EnumWindow(IntPtr handle, IntPtr pointer) Protected static extern bool EnumWindows(Win32Callback enumProc, IntPtr lParam) Public delegate bool Win32Callback(IntPtr hwnd, IntPtr lParam) ![]() Get the title of the currently active window: class Program If (Processes.Length d.MainWindowHandle != IntPtr.Zero)) Process Processes = Process.GetProcessesByName("chrome") Public DateTime VisitedTime Ĭheck if the browser is running: static bool CheckIfBrowserIsRunning() HistoryItem Class: public class HistoryItem So in this way, as long as the page does not switch, we will treat it as active. Moreover, as cooldadtx said, if a user opens a page for a long time without any action, we cannot tell whether the user is active or not, maybe he is reading a difficult paper. If it changes, record the current time, which is the start time of the current page and the end time of the previous page.Īt the same time, query whether the current page already exists in the list, if it already exists, modify the VisitedTime of that item.īecause the user may switch pages during the two polls of the Timer, the time obtained by this method is not completely accurate, just relatively close. Use Timer to detect whether the browser is open, record the currently active page, add it to a List, and then use another Timer to detect whether the currently active page has changed. I have a rough idea, we can use a service.
0 Comments
Leave a Reply. |