Detect clickable link in Chrome

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

Re: Detect clickable link in Chrome

01 Jul 2020, 08:03

burque505 wrote:
01 Jul 2020, 07:09
@AHKStudent, this will get your data, just tried it. But it contains newlines, so you'll have to transform the data to get rid of them.

Code: Select all

selector := "'div.table-list-item'"
Regards,
burque505
:thumbup: works great, most cases I use a script to get data from there but sometimes I need something quick, for some results, this is great,
burque505
Posts: 1736
Joined: 22 Jan 2017, 19:37

Re: Detect clickable link in Chrome

01 Jul 2020, 10:13

:D Glad to hear it!
Regards,
burque505
samt
Posts: 62
Joined: 09 Sep 2022, 13:29

Re: Detect clickable link in Chrome

01 Mar 2023, 20:06

teadrinker wrote:
05 May 2020, 08:17
If using accsessible, it's easier to get acc-object of Chrome address bar and run javascript from it, which searches for a link with specified text and clicks it.

Code: Select all

SetBatchLines, -1

linkText := "05 May 2020, 06:43"

js =
(
(() => {
   const links = document.links;
   for (let i = 0; i < links.length; i++) {
      if (links[i].innerText == '%linkText%') {
         links[i].click();
         break
      }
   }
})();
)

$F1:: RunJsFromChromeAddressBar(js)

RunJsFromChromeAddressBar(js, exe := "chrome.exe") {
   static WM_GETOBJECT := 0x3D
        , ROLE_SYSTEM_TEXT := 0x2A
        , STATE_SYSTEM_FOCUSABLE := 0x100000
        , SELFLAG_TAKEFOCUS := 0x1
   if !AccAddrBar {
      window := "ahk_class Chrome_WidgetWin_1 ahk_exe " . exe
      SendMessage, WM_GETOBJECT, 0, 1, Chrome_RenderWidgetHostHWND1, % window
      AccChrome := AccObjectFromWindow( WinExist(window) )
      AccAddrBar := SearchElement(AccChrome, {Role: ROLE_SYSTEM_TEXT, State: STATE_SYSTEM_FOCUSABLE})
   }
   AccAddrBar.accValue(0) := "javascript:" . js
   AccAddrBar.accSelect(SELFLAG_TAKEFOCUS, 0)
   ControlSend,, {Enter}, % window, Chrome Legacy Window
}

SearchElement(parentElement, params)
{
   found := true
   for k, v in params {
      try {
         if (k = "ChildCount")
            (parentElement.accChildCount != v && found := false)
         else if (k = "State")
            (!(parentElement.accState(0) & v) && found := false)
         else
            (parentElement["acc" . k](0) != v && found := false)
      }
      catch 
         found := false
   } until !found
   if found
      Return parentElement
   
   for k, v in AccChildren(parentElement)
      if obj := SearchElement(v, params)
         Return obj
}

AccObjectFromWindow(hWnd, idObject = 0) {
   static IID_IDispatch   := "{00020400-0000-0000-C000-000000000046}"
        , IID_IAccessible := "{618736E0-3C3D-11CF-810C-00AA00389B71}"
        , OBJID_NATIVEOM  := 0xFFFFFFF0, VT_DISPATCH := 9, F_OWNVALUE := 1
        , h := DllCall("LoadLibrary", "Str", "oleacc", "Ptr")
        
   VarSetCapacity(IID, 16), idObject &= 0xFFFFFFFF
   DllCall("ole32\CLSIDFromString", "Str", idObject = OBJID_NATIVEOM ? IID_IDispatch : IID_IAccessible, "Ptr", &IID)
   if DllCall("oleacc\AccessibleObjectFromWindow", "Ptr", hWnd, "UInt", idObject, "Ptr", &IID, "PtrP", pAcc) = 0
      Return ComObject(VT_DISPATCH, pAcc, F_OWNVALUE)
}

AccChildren(Acc) {
   static VT_DISPATCH := 9
   Loop 1  {
      if ComObjType(Acc, "Name") != "IAccessible"  {
         error := "Invalid IAccessible Object"
         break
      }
      try cChildren := Acc.accChildCount
      catch
         Return ""
      Children := []
      VarSetCapacity(varChildren, cChildren*(8 + A_PtrSize*2), 0)
      res := DllCall("oleacc\AccessibleChildren", "Ptr", ComObjValue(Acc), "Int", 0
                                                , "Int", cChildren, "Ptr", &varChildren, "IntP", cChildren)
      if (res != 0) {
         error := "AccessibleChildren DllCall Failed"
         break
      }
      Loop % cChildren  {
         i := (A_Index - 1)*(A_PtrSize*2 + 8)
         child := NumGet(varChildren, i + 8)
         Children.Push( (b := NumGet(varChildren, i) = VT_DISPATCH) ? AccQuery(child) : child )
         ( b && ObjRelease(child) )
      }
   }
   if error
      ErrorLevel := error
   else
      Return Children.MaxIndex() ? Children : ""
}

AccQuery(Acc) {
   static IAccessible := "{618736e0-3c3d-11cf-810c-00aa00389b71}", VT_DISPATCH := 9, F_OWNVALUE := 1
   try Return ComObject(VT_DISPATCH, ComObjQuery(Acc, IAccessible), F_OWNVALUE)
}
Is this supposed to work for non-emails too, like random websites? I cannot get it to click the link that contians specific word. The URL just flashes. Using latest chrome.
teadrinker
Posts: 4391
Joined: 29 Mar 2015, 09:41
Contact:

Re: Detect clickable link in Chrome

01 Mar 2023, 20:26

Try replacing

Code: Select all

links[i].click();
with

Code: Select all

window.location = links[i].href;
samt
Posts: 62
Joined: 09 Sep 2022, 13:29

Re: Detect clickable link in Chrome

01 Mar 2023, 20:36

teadrinker wrote:
01 Mar 2023, 20:26
Try replacing

Code: Select all

links[i].click();
with

Code: Select all

window.location = links[i].href;
I actually got it to work with the original, and the replacement too.
I know why it wasn't working, I think: It seems to only work when that particular string only occurs once in current viewing window. The words I was looking for existed twice so it didn't word. So if the word/phrase exists twice or more in that window, the script wont click on it. I guess it gets confused? Anyway to fix this? Like, make it click the first instance that word link that it finds?
Last edited by samt on 01 Mar 2023, 20:44, edited 1 time in total.
teadrinker
Posts: 4391
Joined: 29 Mar 2015, 09:41
Contact:

Re: Detect clickable link in Chrome

01 Mar 2023, 20:42

samt wrote: if the word/phrase exists twice or more in that window, the script wont click on it
No, in this case the script should click on the first link it finds.
samt
Posts: 62
Joined: 09 Sep 2022, 13:29

Re: Detect clickable link in Chrome

01 Mar 2023, 20:53

teadrinker wrote:
01 Mar 2023, 20:42
samt wrote: if the word/phrase exists twice or more in that window, the script wont click on it
No, in this case the script should click on the first link it finds.
Interesting. Not sure why it doesn't work for me if there's 2 of the same words. Trying it this AHK forum and it doesn't click anything if that word/phrase appears twice? (they lead to same URL)
teadrinker
Posts: 4391
Joined: 29 Mar 2015, 09:41
Contact:

Re: Detect clickable link in Chrome

01 Mar 2023, 21:09

For me this correctly works on this page: linkText := "samt"
samt
Posts: 62
Joined: 09 Sep 2022, 13:29

Re: Detect clickable link in Chrome

01 Mar 2023, 21:20

teadrinker wrote:
01 Mar 2023, 21:09
For me this correctly works on this page: linkText := "samt"
You're right, that works. I noticed it seems to lead to a different link than when you click on it manually.
Try this:

Code: Select all

linkText := "Re: Detect clickable link"
It all leads to one link and doesn't work
teadrinker
Posts: 4391
Joined: 29 Mar 2015, 09:41
Contact:

Re: Detect clickable link in Chrome

01 Mar 2023, 21:38

The complete text is "Re: Detect clickable link in Chrome".
samt
Posts: 62
Joined: 09 Sep 2022, 13:29

Re: Detect clickable link in Chrome

01 Mar 2023, 22:09

teadrinker wrote:
01 Mar 2023, 21:38
The complete text is "Re: Detect clickable link in Chrome".
It seems to work. I tried it on some other sites but it doesn't seem to work..
Anyways, I'll pause trying for now. Not sure what's going on, but got some other stuff to do. Thanks for the help!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], doanmvu and 173 guests