Get active URL in web view?

I've been building a mini preview browser (similar to Arc Mini) but I need to be able to grab the current URL in the floating menu's web view item.

{BTT_OPENED_URL} isn't enough, because I want it to grab even new links opened within the web view, after the initially opened URL.

I assume this is possible with some javascript call? But not sure what the variables would be called...

you could use the "execute java script in webview item" action:

let url = window.location.href;
set_string_variable({variableName: "webviewURL", to: url});

I'm trying to run this in a CAG when my floating menu is hovered and focused. Top level trigger runs but seems like js isn't creating my variable and the only thing I'm getting on HUD is {chatgptURL}, literally.

[
  {
    "BTTActionCategory" : 0,
    "BTTLastUpdatedAt" : 1741966677.421303,
    "BTTTriggerType" : 0,
    "BTTTriggerClass" : "BTTTriggerTypeKeyboardShortcut",
    "BTTUUID" : "847CC77B-B6DC-41B6-ACC1-7784093A9B24",
    "BTTPredefinedActionType" : 366,
    "BTTPredefinedActionName" : "Empty Placeholder",
    "BTTAdditionalConfiguration" : "1048584",
    "BTTKeyboardShortcutKeyboardType" : 1825,
    "BTTTriggerOnDown" : 1,
    "BTTLayoutIndependentChar" : "o",
    "BTTEnabled" : 1,
    "BTTEnabled2" : 1,
    "BTTShortcutKeyCode" : 31,
    "BTTShortcutModifierKeys" : 1048576,
    "BTTOrder" : 1,
    "BTTAutoAdaptToKeyboardLayout" : 0,
    "BTTAdditionalActions" : [
      {
        "BTTActionCategory" : 0,
        "BTTLastUpdatedAt" : 1741967090.1337051,
        "BTTTriggerParentUUID" : "847CC77B-B6DC-41B6-ACC1-7784093A9B24",
        "BTTIsPureAction" : true,
        "BTTTriggerClass" : "BTTTriggerTypeKeyboardShortcut",
        "BTTUUID" : "4E6DF45B-DB50-4FA7-9974-997D15FF61FF",
        "BTTPredefinedActionType" : 398,
        "BTTPredefinedActionName" : "Execute JavaScript In Web View Menu Item",
        "BTTAdditionalActionData" : {
          "BTTMenuActionMenuName" : "ChatGPT",
          "BTTMenuActionMenuItemContentJS" : "let url = window.location.href\n\nset_string_variable({variableName: \"chatgptURL\", to: url})",
          "BTTMenuActionMenuItemName" : "Webview",
          "BTTMenuActionMenuItemUUID" : ""
        },
        "BTTKeyboardShortcutKeyboardType" : 0,
        "BTTEnabled" : 1,
        "BTTEnabled2" : 1,
        "BTTShortcutKeyCode" : -1,
        "BTTOrder" : 0,
        "BTTAutoAdaptToKeyboardLayout" : 0
      },
      {
        "BTTActionCategory" : 0,
        "BTTLastUpdatedAt" : 1741966697.659848,
        "BTTTriggerParentUUID" : "847CC77B-B6DC-41B6-ACC1-7784093A9B24",
        "BTTIsPureAction" : true,
        "BTTTriggerClass" : "BTTTriggerTypeKeyboardShortcut",
        "BTTUUID" : "6207BEB9-F4BD-4521-BB0C-C9038643B18C",
        "BTTPredefinedActionType" : 254,
        "BTTPredefinedActionName" : "Show HUD Overlay",
        "BTTHUDActionConfiguration" : "{\"BTTActionHUDBlur\":true,\"BTTActionHUDBackground\":\"0.000000, 0.000000, 0.000000, 0.000000\",\"BTTIconConfigImageHeight\":100,\"BTTActionHUDPosition\":0,\"BTTActionHUDDetail\":\"\",\"BTTActionHUDDuration\":0.89999997615814209,\"BTTActionHUDDisplayToUse\":0,\"BTTIconConfigImageWidth\":100,\"BTTActionHUDSlideDirection\":0,\"BTTActionHUDHideWhenOtherHUDAppears\":false,\"BTTActionHUDWidth\":220,\"BTTActionHUDAttributedTitle\":\"{\\\\rtf1\\\\ansi\\\\ansicpg1252\\\\cocoartf2821\\n\\\\cocoatextscaling0\\\\cocoaplatform0{\\\\fonttbl\\\\f0\\\\fnil\\\\fcharset0 SFPro-Bold;}\\n{\\\\colortbl;\\\\red255\\\\green255\\\\blue255;\\\\red0\\\\green0\\\\blue0;}\\n{\\\\*\\\\expandedcolortbl;;\\\\cssrgb\\\\c0\\\\c0\\\\c0\\\\c84706\\\\cname labelColor;}\\n\\\\pard\\\\tx560\\\\tx1120\\\\tx1680\\\\tx2240\\\\tx2800\\\\tx3360\\\\tx3920\\\\tx4480\\\\tx5040\\\\tx5600\\\\tx6160\\\\tx6720\\\\pardirnatural\\\\qc\\\\partightenfactor0\\n\\n\\\\f0\\\\b\\\\fs80 \\\\cf2 \\\\{chatgptURL\\\\}}\",\"BTTActionHUDBorderWidth\":0,\"BTTActionHUDTitle\":\"\",\"BTTActionHUDHeight\":220}",
        "BTTKeyboardShortcutKeyboardType" : 0,
        "BTTEnabled" : 1,
        "BTTEnabled2" : 1,
        "BTTShortcutKeyCode" : -1,
        "BTTOrder" : 1,
        "BTTAutoAdaptToKeyboardLayout" : 0
      },
      {
        "BTTActionCategory" : 0,
        "BTTLastUpdatedAt" : 1741967134.7040892,
        "BTTTriggerParentUUID" : "847CC77B-B6DC-41B6-ACC1-7784093A9B24",
        "BTTIsPureAction" : true,
        "BTTTriggerClass" : "BTTTriggerTypeKeyboardShortcut",
        "BTTUUID" : "53C1FFC6-7D60-4DF5-B184-7AB7533A9FAF",
        "BTTPredefinedActionType" : 59,
        "BTTPredefinedActionName" : "Open URL  or  Open URL With Selection",
        "BTTOpenURL" : "{chatgptURL}",
        "BTTOpenURLBrowser" : "Safari",
        "BTTKeyboardShortcutKeyboardType" : 0,
        "BTTEnabled" : 1,
        "BTTEnabled2" : 1,
        "BTTShortcutKeyCode" : -1,
        "BTTOrder" : 2,
        "BTTAutoAdaptToKeyboardLayout" : 0
      }
    ]
  }
]

I haven't tried it, but it looks like you are missing a semicolon here (I know JS has some automagic semicolon addition, but I never trust that):

          "BTTMenuActionMenuItemContentJS" : "let url = window.location.href\n\nset_string_variable({variableName: \"chatgptURL\", to: url})",
let url = window.location.href;
// maybe also add an await to be sure the next action is executed after the variable has been set
await set_string_variable({variableName: "chatgptURL", to: url});

Added! Still the same. :confused:
If I set the variable manually outside js just for the test, it works good.

Ah with the await I added in the example the script would also need to be wrapped in a async function:

(async () => {
let url = window.location.href;
// maybe also add an await to be sure the next action is executed after the variable has been set
set_string_variable({variableName: "chatgptURL", to: url});
})();

I just tried it here, it seems to work ok with my test webview

Make sure this option is not checked for the webview item:

1 Like

Awesome, works!!!