;---------------------------------
; AHK利用地址栏实现网页自动化 By FeiYue
;
; 简单的GUI窗口用于测试,实际使用RunJs()函数
;---------------------------------
js= ;-- 这个JS代码实现浏览器跳转到下一页
(
var Re=new RegExp("(下|后)(一|\s)*(页|頁|章|篇|翻)","i");
var a=document.getElementsByTagName("A");
for (var i=0;i<a.length;i++)
{
if (Re.test(a[i].innerHTML))
{
if (a[i].href.substr(0,11).toLowerCase()=="javascript:")
a[i].click();
else
document.location.href=a[i].href;
break;
}
}
)
#NoEnv
Gui, +AlwaysOnTop +Resize
Gui, Font, s14 cGreen
Gui, Color, DDEEFF
Gui, Margin, 15, 15
Gui, Add, Edit, vmyedit w500 h300 -Wrap HScroll, %js%
Gui, Add, Button, vmybutton wp gRun Default, 运行JS代码
GuiControlGet, pos, Pos, mybutton
GuiControl, Focus, mybutton
Gui, Show,, 网页自动化测试平台
OnMessage(0x201, "LButton_Down")
LButton_Down()
{
if (A_GuiControl="")
SendMessage, 0xA1, 2
}
Menu, Tray, Click, 1
Menu, Tray, Add
Menu, Tray, Add, 打开测试平台
Menu, Tray, Default, 打开测试平台
打开测试平台:
Gui, Show
return
GuiSize:
if (ErrorLevel=1)
return
GuiControl, Move, myedit
, % "W" (A_GuiWidth-15*2) " H" (A_GuiHeight-15*3-posH)
GuiControl, Move, mybutton
, % "W" (A_GuiWidth-15*2) " Y" (A_GuiHeight-15-posH)
return
Run:
Gui, Hide
Gui, Show, NA
GuiControlGet, js,, myedit
RunJs(js, "ahk_class 360se6_Frame")
return
;-- 在脚本中调用 RunJs() 即可实现网页自动化
RunJs(js, window:="ahk_class 360se6_Frame")
{
static Acc, id
if (!Acc)
{
IfWinNotExist, % window
{
MsgBox, 4096,, 请先打开你的浏览器!, 3
Exit
}
WinActivate
WinWaitActive
Sleep, 100
SendInput {Alt Down}d{Alt Up}
Sleep, 50
;-- 利用输入光标快速找到地址栏的ACC
Acc := AccObjectFromWindow(id:=WinExist())
While IsObject(child := AccQuery(Acc.accFocus))
Acc := child
; MsgBox,4096,,% Format("0x{:X}",Acc.accRole(0)) "`n" Acc.accName(0),3
}
Try if (Acc.accRole(0)=0x2A and Acc.accName(0)="地址和搜索栏")
{
js:="javascript:try{" RegExReplace(js,"\s+"," ") "}catch(e){}void(0);"
Acc.accValue(0):=js
Acc.accSelect(1, 0)
;-- 其他内核的浏览器可能要修改 Chrome Legacy Window
ControlSend,, {Enter}, ahk_id %id%, Chrome Legacy Window
Sleep, 500
return
}
Acc:=""
}
;-- 下面两个函数提取自ACC库,感谢库的开发者
AccObjectFromWindow(hWnd, idObject = -4)
{
static h := DllCall("LoadLibrary", "Str", "oleacc", "Ptr")
if DllCall("oleacc\AccessibleObjectFromWindow"
, "Ptr", hWnd, "UInt", idObject&=0xFFFFFFFF, "Ptr", (VarSetCapacity(IID,16)
+NumPut(idObject==0xFFFFFFF0?0x0000000000020400:0x11CF3C3D618736E0,IID,"Int64")
+NumPut(idObject==0xFFFFFFF0?0x46000000000000C0:0x719B3800AA000C81,IID,8,"Int64"))*0
+&IID, "Ptr*", pacc)=0
return ComObjEnwrap(9,pacc,1)
}
AccQuery(Acc)
{
static IAccessible := "{618736e0-3c3d-11cf-810c-00aa00389b71}"
Try return ComObj(9, ComObjQuery(Acc, IAccessible), 1)
}
;
牛逼
牛人,谢谢