2011-06-08 345 views
7

我正在寻找一种方法在AutoHotkey中将当前访问的URL放入一个变量中。如何使用AutoHotKey脚本获取当前的浏览器URL?

这个AHK的目标是跟踪我一天中在做什么来更好地记录我的小时数。我有另一个系统用来记录我的工作,但有时我忘记了当我侧重跟踪时使用它。

loop 
{ 
    ; Get current Window ID & Name 
    WinGet, active_id, ID, A 
    WinGet, process_name, ProcessName, A 

    ; Only do anything if any other windows was activated 
    if(active_id = PrevActiveId) 
    { 
     ; Do nothing 
    } 
    else 
    { 
     ; Format the time-stamp. 
     current=%A_DD%/%A_MM%/%A_YYYY%, %A_Hour%:%A_Min% 

     ; Write this data to the log.txt file. 
     fileappend, %current% - %process_name%`n, log.txt 

     ; Get the URL if process_name = "chrome.exe" 
     if(process_name = "chrome.exe") 
     { 
      ; Put URL in log file 
      ; fileappend, %current% - %current_url%`n, log.txt 
     } 
    } 

    PrevActiveId = %active_id% 
    Sleep, 100 
} 
+1

解决了它,但“声誉低于100的用户在询问后8小时内无法回答自己的问题,您可以在5个小时内自行回答。”所以会在5小时内回复..... :-( – masterdam79 2011-06-08 09:28:51

+1

好主意在5小时内回来并发布您的答案。它可以帮助其他人:) – ardavis 2011-06-15 01:08:23

回答

0

使用WinGetTitle函数,因为大多数浏览器在标题中设置当前URL。

loop 
{ 
    ; Get current Window ID & Name 
    WinGet, active_id, ID, A 
    WinGet, process_name, ProcessName, A 
    WinGetTitle, this_title, ahk_id %active_id% 

    ; Format the time-stamp. 
    current=%A_DD%/%A_MM%/%A_YYYY%, %A_Hour%:%A_Min% 

    ; Only do anything if any other windows was activated 
    if(active_id = PrevActiveId) 
    { 
     if process_name contains chrome.exe,firefox.exe,iexplore.exe,flock.exe,k-meleon.exe,javaw.exe 
     { 
      ; Write titles to the log.txt file. 
      fileappend, %current% - %process_name% - %this_title%`n, log.txt 
     } 
    } 
    else 
    { 
     ; Write titles to the log.txt file. 
     fileappend, %current% - %process_name% - %this_title%`n, log.txt 
    } 

    Sleep, 1000 
    PrevActiveId = %active_id% 

} 
2

或者你可以使用F6

当按F6时,大多数浏览器都会显示地址栏并为您选择整个URL。

然后它只是复制粘贴的问题。

对于较新的Firefox版本,其按Ctrl +大号虽然

为此,您可以检查窗口标题。

+0

这不是一个好的窍门。我正在干涉用户浏览器,哪个用户可以认为是缺少的东西。 – 2016-05-30 05:25:40

+0

我推荐使用** Alt + D **而不是F6。 F6键切换焦点,这意味着如果URL栏已处于焦点状态,您可能会意外取消选择。 – 2017-09-22 18:46:29

3

对于Chrome,获取控件Chrome_OmniboxView1的文本,Chrome_OmniboxView1是多功能框(与当前版本的Chrome,21.0.1180.83一样)。

这段代码放在网址列的内容到变量omniboxContents:

ControlGetText omniboxContents, Chrome_OmniboxView1, Chrome 

注意omniboxContents不一定包含一个正确的URL,因为“HTTP://”的URL是否开始冷落与“http://”。因此,取代“http://www.google.com”,您将获得“www.google.com”,严格来说这不是一个正确的网址。这仅仅是因为Chrome在多功能框中显示了这种地址。您需要添加额外的代码才能从多功能框的内容中获取正确的网址。

3

我用过的所有浏览器都支持Alt + D重点介绍并选择url。以下是通过按Ctrl键++d复制在谷歌Chrome,Firefox和Internet Explorer中的当前选项卡我的AHK脚本..

#IfWinActive ahk_class MozillaUIWindowClass ; Mozilla Firefox 3.x 
    ^+d::GenericDuplicateTab() ; (Control+Shift+D) 
#IfWinActive 

#IfWinActive ahk_class MozillaWindowClass ; Firefox 4, 5, 6, 7, 8+ (?) 
    ^+d::GenericDuplicateTab() ; (Control+Shift+D) 
#IfWinActive 

#IfWinActive ahk_class Chrome_WidgetWin_1 ; Chromium and Chrome 19+ 
    ^+d::GenericDuplicateTab() ; (Control+Shift+D) 
#IfWinActive 

#IfWinActive ahk_class Chrome_WidgetWin_1 ; Chrome 18 and less 
    ^+d::GenericDuplicateTab() ; (Control+Shift+D) 
#IfWinActive 

#IfWinActive ahk_class IEFrame 
    ^+d::InternetExplorerDuplicateTab() ; (Control+Shift+D) 
#IfWinActive 

GenericDuplicateTab() 
{ 
    ; Wait for both Control and Shift to be released. 
    KeyWait Control 
    KeyWait Shift 

    BackupClipbrd := Clipboard 
    Sleep 50 

    Send !d ; Select the url textbox 
    Sleep 150 

    Send ^x ; Copy the url 
    ClipWait 0.1 
    If ERRORLEVEL 
    { 
    Clipboard := BackupClipbrd 
    Return 
    } 

    Send ^t ; Open a new tab 
    Sleep 50 

    Send ^v ; Paste the url into the new tab's url textbox 
    Sleep 50 
    Send {Enter} 

    Clipboard := BackupClipbrd 
} 

InternetExplorerDuplicateTab() 
{ 
    ; Wait for both Control and Shift to be released. 
    KeyWait Control 
    KeyWait Shift 

    Send ^k ; Call IE's shortcut to duplicate tab (Control+K) 
    Sleep 100 

    Send ^{TAB} ; Switch to that tab 
} 
1

一个干净的方式做到这一点:

GroupAdd, WebBrowsers, ahk_class MozillaWindowClass 
    GroupAdd, WebBrowsers, ahk_class IEFrame 
    GroupAdd, WebBrowsers, ahk_class Chrome_WidgetWin_0 
    GroupAdd, WebBrowsers, ahk_class Chrome_WidgetWin_1 
    GroupAdd, WebBrowsers, ahk_class OperaWindowClass 
    GroupAdd, WebBrowsers, ahk_class {1C03B488-D53B-4a81-97F8-754559640193} 
    ; etc. 

    #IfWinActive, ahk_group WebBrowsers 
    { 
     ^+d:: 
     ; [Instructions...] 
     return 
    }#If 
相关问题