如何从已加载到TWebBrowser组件中的页面更改JavaScript?
我试过这样的事情:
var
ElElem: IHTMLElement;
begin
newJSfunction := 'function onclick(){alert("ok");}';
ElElem := GetButtonFromBrowser;
ElElem.onclick := newJSfunction;
ElElem.click();
end;
但没有用。
答案 0 :(得分:0)
如果您询问,如何将脚本片段运行到当前的Web浏览器中,以下是解决方案:
function TMyWebBrowser.RunScript(const Fragment: string): Boolean;
var
D2Ptr: IHTMLDocument2;
Win2Ptr: IHTMLWindow2;
begin
Result := False;
if Supports(Document, IHTMLDocument2, D2Ptr) and
Supports(D2Ptr.parentWindow, IHTMLWindow2, Win2Ptr) then
try
Result := (Win2Ptr.execScript(Fragment, 'JavaScript') = S_OK);
except
// Failed to execute JS at runtime
end;
end;