2011-06-08 337 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 
相關問題