要在 Delphi 代码中访问 detail 或 CustomEvent,您需要:
- 在您的事件侦听器中获取引用
IDOMEvent,
- 从中获取对
IDOMCustomEvent的引用,
- 使用后期绑定浏览
detail 属性。
您显然在第 1 步失败了。您是通过IDispatch 实现事件侦听器并将其作为IEventTarget.addEventListener 的第二个参数传递的好方法。此时,您希望在将事件分派给侦听器时根据文档接收一些参数:
listener [输入]
输入:IDispatch
与事件关联的事件处理函数。请注意,事件处理函数本身需要两个参数 - 第一个是事件目标(即调用事件处理函数的对象),第二个是 IDOMEvent 对象。
在注册事件侦听器并在 HTML 中引发事件后,您发现在方法 Invoke 中的参数 Params 中没有收到任何值:
您不是第一个面临此问题的人,寻找根本原因只会产生很少的结果:
基于您的侦听器需要实现IDispatchEx,文档未提及。你只需要实现它的InvokeEx 方法并忽略其余的。这是示例实现(我敢于将类重命名为TWebBrowserEventListener 以更好地表达其目的):
uses
System.SysUtils, Winapi.Windows, Winapi.ActiveX, MSHTML;
type
THandleEvent = procedure(const Target: IDispatch; const DOMEvent: IDOMEvent) of object;
TWebBrowserEventListener = class(TInterfacedObject, IDispatchEx)
private
FOnHandleEvent: THandleEvent;
{ IDispatch }
function GetTypeInfoCount(out Count: Integer): HResult; stdcall;
function GetTypeInfo(Index, LocaleID: Integer; out TypeInfo): HResult; stdcall;
function GetIDsOfNames(const IID: TGUID; Names: Pointer;
NameCount, LocaleID: Integer; DispIDs: Pointer): HResult; stdcall;
function Invoke(DispID: Integer; const IID: TGUID; LocaleID: Integer;
Flags: Word; var Params; VarResult, ExcepInfo, ArgErr: Pointer): HResult; stdcall;
{ IDispatchEx }
function GetDispID(const bstrName: TBSTR; const grfdex: DWORD;
out id: TDispID): HResult; stdcall;
function InvokeEx(const id: TDispID; const lcid: LCID; const wflags:
WORD; const pdp: PDispParams; out varRes: OleVariant; out pei:
TExcepInfo; const pspCaller: PServiceProvider): HResult; stdcall;
function DeleteMemberByName(const bstr: TBSTR;
const grfdex: DWORD): HResult; stdcall;
function DeleteMemberByDispID(const id: TDispID): HResult; stdcall;
function GetMemberProperties(const id: TDispID; const grfdexFetch:
DWORD; out grfdex: DWORD): HResult; stdcall;
function GetMemberName(const id: TDispID; out bstrName: TBSTR):
HResult; stdcall;
function GetNextDispID(const grfdex: DWORD; const id: TDispID;
out nid: TDispID): HResult; stdcall;
function GetNameSpaceParent(out unk: IUnknown): HResult; stdcall;
protected
procedure HandleEvent(const Target: IDispatch; const DOMEvent: IDOMEvent); virtual;
public
constructor Create(AOnHandleEvent: THandleEvent);
end;
constructor TWebBrowserEventListener.Create(AOnHandleEvent: THandleEvent);
begin
inherited Create;
FOnHandleEvent := AOnHandleEvent;
end;
function TWebBrowserEventListener.DeleteMemberByDispID(const id: TDispID): HResult;
begin
Result := E_NOTIMPL;
end;
function TWebBrowserEventListener.DeleteMemberByName(const bstr: TBSTR;
const grfdex: DWORD): HResult;
begin
Result := E_NOTIMPL;
end;
function TWebBrowserEventListener.GetDispID(const bstrName: TBSTR; const grfdex: DWORD;
out id: TDispID): HResult;
begin
Result := E_NOTIMPL;
end;
function TWebBrowserEventListener.GetIDsOfNames(const IID: TGUID; Names: Pointer;
NameCount, LocaleID: Integer; DispIDs: Pointer): HResult;
begin
Result := E_NOTIMPL;
end;
function TWebBrowserEventListener.GetMemberName(const id: TDispID;
out bstrName: TBSTR): HResult;
begin
Result := E_NOTIMPL;
end;
function TWebBrowserEventListener.GetMemberProperties(const id: TDispID;
const grfdexFetch: DWORD; out grfdex: DWORD): HResult;
begin
Result := E_NOTIMPL;
end;
function TWebBrowserEventListener.GetNameSpaceParent(out unk: IInterface): HResult;
begin
Result := E_NOTIMPL;
end;
function TWebBrowserEventListener.GetNextDispID(const grfdex: DWORD; const id: TDispID;
out nid: TDispID): HResult;
begin
Result := E_NOTIMPL;
end;
function TWebBrowserEventListener.GetTypeInfo(Index, LocaleID: Integer;
out TypeInfo): HResult;
begin
Result := E_NOTIMPL;
end;
function TWebBrowserEventListener.GetTypeInfoCount(out Count: Integer): HResult;
begin
Result := E_NOTIMPL;
end;
procedure TWebBrowserEventListener.HandleEvent(const Target: IDispatch;
const DOMEvent: IDOMEvent);
begin
if Assigned(FOnHandleEvent) then
FOnHandleEvent(Target, DOMEvent);
end;
function TWebBrowserEventListener.Invoke(DispID: Integer; const IID: TGUID;
LocaleID: Integer; Flags: Word; var Params; VarResult, ExcepInfo,
ArgErr: Pointer): HResult;
begin
Result := E_NOTIMPL;
end;
function TWebBrowserEventListener.InvokeEx(const id: TDispID; const lcid: LCID;
const wflags: WORD; const pdp: PDispParams; out varRes: OleVariant;
out pei: TExcepInfo; const pspCaller: PServiceProvider): HResult;
var
DOMEvent: IDOMEvent;
begin
if (id = DISPID_VALUE) and (pdp^.cArgs = 2) and (pdp^.rgvarg^[0].vt = varDispatch) and
(pdp^.rgvarg^[1].vt = varDispatch) and Supports(IDispatch(pdp^.rgvarg^[1].dispVal), IDOMEvent, DOMEvent) then
begin
HandleEvent(IDispatch(pdp^.rgvarg^[0].dispVal), DOMEvent);
Result := S_OK;
end
else
Result := E_NOTIMPL;
end;
要测试我是否将此 HTML 加载到 Web 浏览器控件中:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="x-ua-compatible" content="IE=edge">
<script>
/* Internet Explorer doesn't support CustomEvent() constructor. Polyfill for IE9+ from
https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent/CustomEvent */
(function () {
if ( typeof window.CustomEvent === "function" ) return false;
function CustomEvent ( event, params ) {
params = params || { bubbles: false, cancelable: false, detail: null };
var evt = document.createEvent( 'CustomEvent' );
evt.initCustomEvent( event, params.bubbles, params.cancelable, params.detail );
return evt;
}
window.CustomEvent = CustomEvent;
})();
function triggerPmweTest() {
var event = new CustomEvent('pmweTest', { detail: { dataPackage: document.getElementById('input-text').value } });
document.dispatchEvent(event);
}
</script>
</head>
<body>
<input id="input-text" type="text" value="hello Matt" />
<input id="input-checkbox" type="checkbox" />
<button id="button" onclick="triggerPmweTest()">Click</button>
</body>
</html>
这就是我注册监听器的方式:
procedure TForm1.WebBrowser1DocumentComplete(ASender: TObject;
const pDisp: IDispatch; const URL: OleVariant);
var
Target : IEventTarget;
Listener: IDispatchEx;
begin
Target := WebBrowser1.Document as IEventTarget;
Listener := TWebBrowserEventListener.Create(WebBrowserEvent);
Target.addEventListener('change', Listener, True);
Target.addEventListener('pmweTest', Listener, True);
end;
procedure TForm1.WebBrowserEvent(const Target: IDispatch;
const DOMEvent: IDOMEvent);
var
EventInfo: string;
DOMCustomEvent: IDOMCustomEvent;
begin
EventInfo := 'Type: ' + DOMEvent.type_ + #13#10'SrcElement: ';
if Assigned(DOMEvent.srcElement) then
begin
EventInfo := EventInfo + DOMEvent.srcElement.tagName;
if DOMEvent.srcElement.id <> '' then
EventInfo := EventInfo + '#' + DOMEvent.srcElement.id;
end
else
EventInfo := EventInfo + '#document';
if (DOMEvent.type_ = 'pmweTest') and Supports(DOMEvent, IDOMCustomEvent, DOMCustomEvent) then
EventInfo := EventInfo + #13#10'detail.dataPackage: ' + VarToStr(DOMCustomEvent.detail.dataPackage);
ShowMessage(EventInfo);
end;
上面的代码侦听<input> 元素上的change 事件以及通过单击按钮触发的自定义pmweTest 事件。两种类型都使用相同的侦听器。
当您更改文本字段的值并将焦点移出该字段时,它会显示:
类型:更改
源元素:INPUT#input-text
当你点击复选框时:
类型:更改
源元素:INPUT#input-checkbox
当你点击按钮时:
类型:pmweTest
源元素:#document
detail.dataPackage:你好,马特