SHARE
TWEET

RunJsFromChromeAddressBar

teadrinker Jan 24th, 2020 622 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; change YouTube speed
  2.  
  3. SetBatchLines, -1
  4. speed := 2
  5. js := "document.querySelector('video').playbackRate = " . speed  ; could be currentTime (sec), volume (0.5), muted (boolean)
  6. RunJsFromChromeAddressBar(js)
  7.  
  8. RunJsFromChromeAddressBar(js, exe := "chrome.exe") {
  9.    static WM_GETOBJECT := 0x3D
  10.         , ROLE_SYSTEM_TEXT := 0x2A
  11.         , STATE_SYSTEM_FOCUSABLE := 0x100000
  12.         , SELFLAG_TAKEFOCUS := 0x1
  13.    
  14.    window := "ahk_class Chrome_WidgetWin_1 ahk_exe " . exe
  15.    SendMessage, WM_GETOBJECT, 0, 1, Chrome_RenderWidgetHostHWND1, % window
  16.    AccChrome := AccObjectFromWindow( WinExist(window) )
  17.    AccAddrBar := SearchElement(AccChrome, {Role: ROLE_SYSTEM_TEXT, State: STATE_SYSTEM_FOCUSABLE})
  18.    AccAddrBar.accValue(0) := "javascript:" . js
  19.    AccAddrBar.accSelect(SELFLAG_TAKEFOCUS, 0)
  20.    ControlSend,, {Enter}, % window
  21. }
  22.  
  23. SearchElement(parentElement, params)
  24. {
  25.    found := true
  26.    for k, v in params {
  27.       try {
  28.          if (k = "ChildCount")
  29.             (parentElement.accChildCount != v && found := false)
  30.          else if (k = "State")
  31.             (!(parentElement.accState(0) & v) && found := false)
  32.          else
  33.             (parentElement["acc" . k](0) != v && found := false)
  34.       }
  35.       catch
  36.          found := false
  37.    } until !found
  38.    if found
  39.       Return parentElement
  40.    
  41.    for k, v in AccChildren(parentElement)
  42.       if obj := SearchElement(v, params)
  43.          Return obj
  44. }
  45.  
  46. AccObjectFromWindow(hWnd, idObject = 0) {
  47.    static IID_IDispatch   := "{00020400-0000-0000-C000-000000000046}"
  48.         , IID_IAccessible := "{618736E0-3C3D-11CF-810C-00AA00389B71}"
  49.         , OBJID_NATIVEOM  := 0xFFFFFFF0, VT_DISPATCH := 9, F_OWNVALUE := 1
  50.         , h := DllCall("LoadLibrary", "Str", "oleacc", "Ptr")
  51.        
  52.    VarSetCapacity(IID, 16), idObject &= 0xFFFFFFFF
  53.    DllCall("ole32\CLSIDFromString", "Str", idObject = OBJID_NATIVEOM ? IID_IDispatch : IID_IAccessible, "Ptr", &IID)
  54.    if DllCall("oleacc\AccessibleObjectFromWindow", "Ptr", hWnd, "UInt", idObject, "Ptr", &IID, "PtrP", pAcc) = 0
  55.       Return ComObject(VT_DISPATCH, pAcc, F_OWNVALUE)
  56. }
  57.  
  58. AccChildren(Acc) {
  59.    static VT_DISPATCH := 9
  60.    Loop 1  {
  61.       if ComObjType(Acc, "Name") != "IAccessible"  {
  62.          error := "Invalid IAccessible Object"
  63.          break
  64.       }
  65.       try cChildren := Acc.accChildCount
  66.       catch
  67.          Return ""
  68.       Children := []
  69.       VarSetCapacity(varChildren, cChildren*(8 + A_PtrSize*2), 0)
  70.       res := DllCall("oleacc\AccessibleChildren", "Ptr", ComObjValue(Acc), "Int", 0
  71.                                                 , "Int", cChildren, "Ptr", &varChildren, "IntP", cChildren)
  72.       if (res != 0) {
  73.          error := "AccessibleChildren DllCall Failed"
  74.          break
  75.       }
  76.       Loop % cChildren  {
  77.          i := (A_Index - 1)*(A_PtrSize*2 + 8)
  78.          child := NumGet(varChildren, i + 8)
  79.          Children.Push( (b := NumGet(varChildren, i) = VT_DISPATCH) ? AccQuery(child) : child )
  80.          ( b && ObjRelease(child) )
  81.       }
  82.    }
  83.    if error
  84.       ErrorLevel := error
  85.    else
  86.       Return Children.MaxIndex() ? Children : ""
  87. }
  88.  
  89. AccQuery(Acc) {
  90.    static IAccessible := "{618736e0-3c3d-11cf-810c-00aa00389b71}", VT_DISPATCH := 9, F_OWNVALUE := 1
  91.    try Return ComObject(VT_DISPATCH, ComObjQuery(Acc, IAccessible), F_OWNVALUE)
  92. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top