procedure TFMWebBrowser.RefreshHTMLElementTree; function _AddElementNode(AParent: TcxTreeListNode; AItem: IHTMLElement): TcxTreeListNode; var lCaption: string; lDataElement: TDataElement; begin lCaption := ''; if Trim(AItem.className) <> '' then lCaption := ' class=' + AItem.className; if lCaption = '' then lCaption := VarToStr(AItem.getAttribute('class', 0)); if Trim(AItem.id) <> '' then lCaption := ' id=' + AItem.id; lCaption := '<' + AItem.tagName + lCaption + '>'; Result := AParent.AddChild; Result.Values[0] := lCaption; lDataElement := TDataElement.Create; lDataElement.PElement := AItem; Result.Data := lDataElement; end; procedure _FullChildItem(AParent: TcxTreeListNode; AItem: IHTMLElement); var M, N: Integer; lChilds: IHTMLElementCollection; lItem: IHTMLElement; lThisFrame: IHTMLWindow2; lFrame: IDispatch; lItemNode: TcxTreeListNode; lurl, lsrc: string; begin lChilds := AItem.children as IHTMLElementCollection; for M := 0 to lChilds.Length - 1 do begin lItem := lChilds.Item(M, varEmpty) as IHTMLElement; lItemNode := _AddElementNode(AParent, lItem); if (lItem.tagName = 'IFRAME') or (lItem.tagName = 'FRAME') then begin lThisFrame := nil; //下面的代码也无法使 lItem 和 IHTMLWindow4 联系起来 for N := 0 to (lItem.document as IHTMLDocument2).frames.length - 1 do begin lFrame := (lItem.document as IHTMLDocument2).frames.item(N); lurl := (lFrame as IHTMLWindow4).frameElement.src; lsrc := lItem.getAttribute('src', 0); if CompareText(RightStr(lurl, Length(lsrc)), lsrc) = 0 then //比较src有时候不行,如果比较name有时候会重复 begin lThisFrame := lFrame as IHTMLWindow2; Break; end; end; if lThisFrame <> nil then begin lItemNode := _AddElementNode(lItemNode, lThisFrame.Document.body); _FullChildItem(lItemNode, lThisFrame.Document.body); end; end else begin if (lItem.children as IHTMLElementCollection).Length <> 0 then _FullChildItem(lItemNode, lItem); end; end; end; var lItemNode: TcxTreeListNode; begin ctlElementTree.Clear; lItemNode := _AddElementNode(ctlElementTree.Root, (WebBrowser.Document as IHTMLDocument2).body); _FullChildItem(lItemNode, (WebBrowser.Document as IHTMLDocument2).body); end;
procedure TFMWebBrowser.RefreshHTMLElementTree;
function
_AddElementNode(AParent: TcxTreeListNode; AItem: IHTMLElement): TcxTreeListNode;
var
lCaption: string;
lDataElement: TDataElement;
begin
lCaption := '';
if Trim(AItem.className) <> '' then lCaption := ' class=' + AItem.className;
if lCaption = '' then lCaption := VarToStr(AItem.getAttribute('class', 0));
if Trim(AItem.id) <> '' then lCaption := ' id=' + AItem.id;
lCaption := '<' + AItem.tagName + lCaption + '>';
Result := AParent.AddChild;
Result.Values[0] := lCaption;
lDataElement := TDataElement.Create;
lDataElement.PElement := AItem;
Result.Data := lDataElement;
end;
procedure
_FullChildItem(AParent: TcxTreeListNode; AItem: IHTMLElement);
var
M, N: Integer;
lChilds: IHTMLElementCollection;
lItem: IHTMLElement;
lThisFrame: IHTMLWindow2;
lFrame: IDispatch;
lItemNode: TcxTreeListNode;
lurl, lsrc: string;
begin
lChilds := AItem.children as IHTMLElementCollection;
for M := 0 to lChilds.Length - 1 do
begin
lItem := lChilds.Item(M, varEmpty) as IHTMLElement;
lItemNode := _AddElementNode(AParent, lItem);
if (lItem.tagName = 'IFRAME') or (lItem.tagName = 'FRAME') then
begin
lThisFrame := nil; //下面的代码也无法使 lItem 和 IHTMLWindow4 联系起来
for N := 0 to (lItem.document as IHTMLDocument2).frames.length - 1 do
begin
lFrame := (lItem.document as IHTMLDocument2).frames.item(N);
lurl := (lFrame as IHTMLWindow4).frameElement.src;
lsrc := lItem.getAttribute('src', 0);
if CompareText(RightStr(lurl, Length(lsrc)), lsrc) = 0 then //比较src有时候不行,如果比较name有时候会重复
begin
lThisFrame := lFrame as IHTMLWindow2;
Break;
end;
end;
if lThisFrame <> nil then
begin
lItemNode := _AddElementNode(lItemNode, lThisFrame.Document.body);
_FullChildItem(lItemNode, lThisFrame.Document.body);
end;
end else
begin
if (lItem.children as IHTMLElementCollection).Length <> 0 then
_FullChildItem(lItemNode, lItem);
end;
end;
end;
var
lItemNode: TcxTreeListNode;
begin
ctlElementTree.Clear;
lItemNode := _AddElementNode(ctlElementTree.Root, (WebBrowser.Document as IHTMLDocument2).body);
_FullChildItem(lItemNode, (WebBrowser.Document as IHTMLDocument2).body);
end;