如何使用AutoHotKey脚本获取当前浏览器的URL?

11

我想在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
}

3
已解决,但是“声望不足100点的用户在提问后8小时内不能回答自己的问题。您可以在5小时后自我回答。”所以我会在5小时后再回来报告..... :-( - masterdam79
2
五个小时后回来发表你的答案是个好主意。这可能会帮助其他人 :) - ardavis
6个回答

6

我使用的所有浏览器都支持使用 Alt+D 来聚焦并选择 URL。以下是我的 AHK 脚本,通过按下 Ctrl+Shift+D 可以在 Google Chrome、Firefox 和 Internet Explorer 中复制当前标签页。

#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
}

4

或者您可以使用F6

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

然后只需要复制粘贴即可。

对于较新的Firefox版本,这是Ctrl+L

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


这不是一个好的技巧。我正在干扰用户浏览器,这可能让用户认为是黑客行为。 - Himanshu sharma
我建议使用 Alt+D 而不是 F6。F6 键切换焦点,这意味着如果已经聚焦在 URL 栏上,您可能会意外取消选择它。 - Stevoisiak

4

对于Chrome浏览器,获取控件Chrome_OmniboxView1的文本,该控件是地址栏(截至当前版本Chrome 21.0.1180.83)。

以下代码将地址栏内容放入变量omniboxContents中:

ControlGetText omniboxContents, Chrome_OmniboxView1, Chrome

请注意,omniboxContents不一定包含正确的URL,因为如果URL以“http://”开头,则会省略它。因此,您将获得“www.google.com”而不是“http://www.google.com”,严格来说这不是一个正确的URL。这仅仅是因为Chrome在Omnibox中以这种方式显示地址。您需要添加额外的代码来从omnibox的内容中获取正确的URL。

这看起来现在已经过时了。 - Thierry Dalon

2

2

一种干净的方法是:

    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

1
您可以使用 Alt+D 快捷键激活地址栏,然后使用 Ctrl+C 快捷键复制 URL。
F1::
{
Send, !d
Sleep 50
Send, ^c
Sleep 50
Return
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接