Malcev Добрый день.
Почему то оно не работает?
В хроме чего то изменилось?
Мой код (весь тестовый файл, у меня открыто одно окно хрома не от админа, в нем с десяток вкладок):
F2::
{
ROLE_SYSTEM_PAGETABLIST := 0x0000003c
STATE_SYSTEM_SELECTED := 0x00000002
ChromeAccObj := GetAccObjFromChrome( WinExist("ahk_class Chrome_WidgetWin_1 ahk_exe chrome.exe") )
AccTabList := SearchElement(ChromeAccObj, {Role: ROLE_SYSTEM_PAGETABLIST})
AccTabs := AccTabList.accChild(1)
tabsCount := activeTabNumber := 0
Loop % AccTabs.accChildCount()
{
if (AccTabs.accChild(A_Index).accRole(0) = 37) ; ROLE_SYSTEM_PAGETAB
{
if (activeTabNumber = 0) and (AccTabs.accChild(A_Index).accState(0) & STATE_SYSTEM_SELECTED)
activeTabNumber := A_Index
tabsCount++
}
}
MsgBox, % "Всего вкладок: " . tabsCount . "`n"
. "Активная вкладка: " . activeTabNumber
Return
}
GetAccObjFromChrome(hWnd) {
static WM_GETOBJECT := 0x3D, AccArray := []
if !AccArray[hWnd] {
try SendMessage, WM_GETOBJECT, 0, 1, Chrome_RenderWidgetHostHWND1, ahk_id %hWnd%
if !AccChrome := AccObjectFromWindow(hWnd)
throw "Failed to get accessible object from hWnd " . Format("{:#x}", hWnd)
AccArray[hWnd] := AccChrome
}
Return AccArray[hWnd]
}
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, AccObject := 0
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
AccObject := ComObject(VT_DISPATCH, pAcc, F_OWNVALUE)
Return AccObject
}
SearchElement(parentElement, params)
{ ; params — object like {Role: ROLE_SYSTEM_PUSHBUTTON := 0x2B, Name: "5"}
found := true
for k, v in params {
try {
if (k = "State")
(!(parentElement.accState(0) & v) && found := false)
else if (k ~= "^(Name|Value)$")
(!(parentElement["acc" . k](0) ~= v) && found := false)
else if (k = "ChildCount")
(parentElement["acc" . k] != 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
}
AccChildren(Acc) {
if ComObjType(Acc, "Name") != "IAccessible" {
ErrorLevel := "Invalid IAccessible Object"
Return
}
try childCount := Acc.accChildCount
if !childCount
Return
Children := []
VarSetCapacity(varChildren, childCount*(8 + A_PtrSize*2), 0)
res := DllCall("oleacc\AccessibleChildren", "Ptr", ComObjValue(Acc), "Int", 0
, "Int", childCount, "Ptr", &varChildren, "IntP", childCount)
if (res != 0) {
ErrorLevel := "winapi AccessibleChildren failed"
Return
}
Loop % childCount {
offset := (A_Index - 1)*(A_PtrSize*2 + 8)
_child := NumGet(varChildren, offset + 8)
if NumGet(varChildren, offset) != (VT_DISPATCH := 9)
child := _child
else
child := AccQuery(_child), ObjRelease(_child)
Children.Push(child)
}
Return 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)
}
При нажатии F2 стабильно выдает "всего вкладок 0, активная вкладка 0", пробовал запускать скрипт и от админа, и без.
PS и можно ли отсюда как то вынуть id вкладок?