Get HTML from an open website (v2) Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
WarlordAkamu67
Posts: 219
Joined: 21 Mar 2023, 06:52

Get HTML from an open website (v2)

Post by WarlordAkamu67 » 22 Mar 2023, 06:52

Hello World!

I have been using @teadrinker's script from version 1 located at: viewtopic.php?t=73571

Shared are the unedited results of putting it through the converter located here: https://github.com/mmikeww/AHK-v2-script-converter

Below the code are the errors that appeared during conversion. Following those are the errors raised when trying to run v2.

Code: Select all

; REMOVED: SetBatchLines, -1

js := "
(
var textArea = document.createElement("textarea");
textArea.value = new XMLSerializer().serializeToString(document);
textArea.style.position = 'fixed';
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
document.execCommand('copy');
textArea.parentNode.removeChild(textArea)
)"

$F1::
{ ; V1toV2: Added bracket
   A_Clipboard := ""
   RunJsFromChromeAddressBar(js)
   Errorlevel := !ClipWait(3)
   pg_Text := A_Clipboard
   FileAppend(pg_Text, "t_v1t.txt")
   MsgBox(pg_Text)
   Return
} ; Added bracket before function

RunJsFromChromeAddressBar(js, exe := "chrome.exe") {
   static WM_GETOBJECT := 0x3D        , ROLE_SYSTEM_TEXT := 0x2A        , STATE_SYSTEM_FOCUSABLE := 0x100000        , SELFLAG_TAKEFOCUS := 0x1

   window := "ahk_class Chrome_WidgetWin_1 ahk_exe " . exe
   WinActivate(window)
   ErrorLevel := 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)
}

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")

   VarSetStrCapacity(&IID, 16), idObject &= 0xFFFFFFFF ; V1toV2: if 'IID' is NOT a UTF-16 string, use 'IID := Buffer(16)'
   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 ComValue(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 := []
      varChildren := Buffer(cChildren*(8 + A_PtrSize*2), 0) ; V1toV2: if 'varChildren' is a UTF-16 string, use 'VarSetStrCapacity(&varChildren, cChildren*(8 + A_PtrSize*2))'
      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 ComValue(VT_DISPATCH, ComObjQuery(Acc, IAccessible), F_OWNVALUE)
}
Conversion Errors:
e0.PNG
e0.PNG (16.56 KiB) Viewed 450 times
e1.PNG
e1.PNG (74.12 KiB) Viewed 450 times
e2.PNG
e2.PNG (76.39 KiB) Viewed 450 times
[e3.PNG did not get attached]

Run Errors:
[re0.PNG did not get attached]

I have tried altering line 35 by removing the "(0)" preceding the ":= 'javascript:' . js". The script "runs" without any error. Upon trying to use it, however, more errors appear.

[re1.PNG did not get attached]
[re2.PNG did not get attached]
[re3.PNG did not get attached]

Help updating this to V2 would be greatly appreciated ^.^


[Mod edits: Removed the duplicate images of e2.PNG that appeared in place of the images that for some reason did not attach, as noted above.]

User avatar
WarlordAkamu67
Posts: 219
Joined: 21 Mar 2023, 06:52

Re: Get HTML from an open website (v2)

Post by WarlordAkamu67 » 22 Mar 2023, 07:57

I also found this, but I don't know if it helps. I don't know enough yet: viewtopic.php?f=82&t=102138

I do not see an "edit post".
Run Errors:
re0.PNG
re0.PNG (16.67 KiB) Viewed 429 times
re1.png
re1.png (16.83 KiB) Viewed 429 times
re2.png
re2.png (16.58 KiB) Viewed 429 times

Descolada
Posts: 1126
Joined: 23 Dec 2021, 02:30

Re: Get HTML from an open website (v2)  Topic is solved

Post by Descolada » 22 Mar 2023, 08:45

@WarlordAkamu67, I would recommend taking a look at an already converted Acc library such as this one. The search method shouldn't be too hard to convert.

Using search with Acc is usually rather slow though, you might get a better performance using UIA. Untested, but something like this should work:

Code: Select all

#include UIA.ahk ; requires UIA.ahk in the same folder as the script

RunJsFromChromeAddressBar(js, exe := "chrome.exe") {
    WinActivate(window := "ahk_class Chrome_WidgetWin_1 ahk_exe " exe)
    urlBarEl := UIA.ElementFromHandle(window).FindElement({Type:"Edit", i:-1})
    urlBarEl.SetFocus()
    urlBarEl.Value := "javascript:" js
    ControlSend("{Enter}", , window)
}

User avatar
WarlordAkamu67
Posts: 219
Joined: 21 Mar 2023, 06:52

Re: Get HTML from an open website (v2)

Post by WarlordAkamu67 » 23 Mar 2023, 12:22

Descolada wrote:
22 Mar 2023, 08:45
@WarlordAkamu67, I would recommend taking a look at an already converted Acc library such as this one. The search method shouldn't be too hard to convert.

Using search with Acc is usually rather slow though, you might get a better performance using UIA. Untested, but something like this should work:

Code: Select all

#include UIA.ahk ; requires UIA.ahk in the same folder as the script

RunJsFromChromeAddressBar(js, exe := "chrome.exe") {
    WinActivate(window := "ahk_class Chrome_WidgetWin_1 ahk_exe " exe)
    urlBarEl := UIA.ElementFromHandle(window).FindElement({Type:"Edit", i:-1})
    urlBarEl.SetFocus()
    urlBarEl.Value := "javascript:" js
    ControlSend("{Enter}", , window)
}
Working Code:

Code: Select all

#include UIA.ahk

RunJsFromChromeAddressBar(js, exe := "chrome.exe") {
    WinActivate(window := "ahk_class Chrome_WidgetWin_1 ahk_exe " exe)
    urlBarEl := UIA.ElementFromHandle(window).FindElement({Type:"Edit", i:-1})
    urlBarEl.SetFocus()
    urlBarEl.Value := "javascript:" js
    ControlSend("{Enter}", , window)
}

js := "
(
var textArea = document.createElement("textarea");
textArea.value = new XMLSerializer().serializeToString(document);
textArea.style.position = 'fixed';
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
document.execCommand('copy');
textArea.parentNode.removeChild(textArea)
)"

F1::
{
  RunJsFromChromeAddressBar(js)
  pg_Text := A_Clipboard
  FileAppend(pg_Text, A_Desktop "\the_Code.txt")
}

Post Reply

Return to “Ask for Help (v2)”