【问题标题】:How can I get the current browser URL with an AutoHotKey script?如何使用 AutoHotKey 脚本获取当前浏览器 URL?
【发布时间】:2011-09-10 15:36:13
【问题描述】:

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

【问题讨论】:

  • 已解决,但“声望低于 100 的用户在提问后 8 小时内无法回答自己的问题。您可以在 5 小时内自行回答。”所以将在 5 小时内报告...... :-(
  • 5 小时后回来发布您的答案的好主意。它可以帮助其他人:)

标签: browser autohotkey


【解决方案1】:

我用过的所有浏览器都支持 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
}

【讨论】:

    【解决方案2】:

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

    这段代码将多功能框的内容放入变量omniboxContents:

    ControlGetText omniboxContents, Chrome_OmniboxView1, Chrome
    

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

    【讨论】:

    • 这看起来已经过时了。
    【解决方案3】:

    或者你可以使用F6。

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

    然后就是复制粘贴了。

    对于较新的 Firefox 版本,它的 Ctrl+L。

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

    【讨论】:

    • 这不是一个好技巧。我正在干扰用户浏览器,用户认为这是一种黑客行为。
    • 我建议使用 Alt+D 而不是 F6。 F6 键切换焦点,这意味着如果 URL 栏已经处于焦点位置,您可能会意外取消选择它。
    【解决方案4】:

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

        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
    

    【讨论】:

      【解决方案5】:

      也可以不依赖剪贴板,直接检查构成浏览器窗口的实际组件。更多信息:http://www.autohotkey.com/board/topic/111114-get-the-url-of-the-current-active-browser-tab/。这种方法更有效,但依赖于使用 OS API。

      【讨论】:

        【解决方案6】:

        您可以使用 Alt+D 快捷键激活地址栏,然后使用 Ctrl+C 复制网址kbd> 快捷方式

        F1::
        {
        Send, !d
        Sleep 50
        Send, ^c
        Sleep 50
        Return
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-08-09
          • 2011-01-08
          • 2011-01-18
          • 1970-01-01
          • 1970-01-01
          • 2015-08-26
          • 1970-01-01
          • 2014-05-07
          相关资源
          最近更新 更多