5

ID を使用して TChromium インスタンスから現在の Web ドキュメントに埋め込まれた特定の DOM ノードを取得するには、ICefDomDocument.getElementById() を使用します。しかし、NAME 属性で要素を見つけるにはどうすればよいでしょうか。Javascript には document.getElementsByName() メソッドがあり、TWebBrowser (IE をラップする) にも同様の呼び出しがありますが、TChromium でこれを行う方法がわかりません。NAME 属性を持ち、ID 属性を持たない DOM 要素を見つける必要があります。私はceflibユニットを検索しましたが、それを行うものは何も見当たりませんでした。

副質問。だれかが TChromium の「レシピ」スタイルのサイトまたはドキュメントへのリンクを持っていれば、それを使用できます。

更新: 回答を待っている間に、getElementsbyName() を実行するための次のコードを思いつきました。DOM ツリー全体をスキャンするよりも高速な方法が必要です。コードに何か問題がある場合は、お知らせください。

type
    TDynamicCefDomNodeArray = array of ICefDomNode;


// Given a Chromium document interface reference and a NAME attribute to search for,
//  return an array of all DOM nodes whose NAME attribute matches the desired.
function getElementsByName(ADocument: ICefDomDocument; theName: string): TDynamicCefDomNodeArray;

    // Get all the elements with a particular NAME attribute value and return
    //  an array of them.
    procedure getElementsByName1(intfParentNode: ICefDomNode; theName: string; var aryResults: TDynamicCefDomNodeArray);
    var
        oldLen: integer;
        intfChildNode: ICefDomNode;
        theNameAttr: string;
    begin
        Result := nil;
        intfChildNode := nil;

        if Assigned(intfParentNode) then
        begin
            // Attributes are case insensitive.
            theNameAttr := intfParentNode.GetElementAttribute('name');

            if AnsiSameText(theNameAttr, theName) then
            begin
                // Name attribute match.  Add it to the results array.
                oldLen := Length(aryResults);
                SetLength(aryResults, oldLen + 1);
                aryResults[oldLen] := intfParentNode;
            end; // if AnsiSameText(intfParentNode.Name, theName) then

            // Does the parent node have children?
            if intfParentNode.HasChildren then
            begin
                intfChildNode := intfParentNode.FirstChild;

                // Scan them.
                while Assigned(intfChildNode) do
                begin
                    getElementsByName1(intfChildNode, theName, aryResults);

                    if Assigned(intfChildNode) then
                        intfChildNode := intfChildNode.NextSibling;
                end;
            end; // if intfParentNode.HasChildren then
        end; // if Assigned(intfParentNode) then
    end;

    // ---------------------------------------------------------------

var
    intfCefDomNode: ICefDomNode;
begin
    intfCefDomNode := nil;
    Result := nil;

    if Assigned(ADocument) then
    begin
        // Check the header.
        intfCefDomNode := ADocument.Document;

        if Assigned(intfCefDomNode) then
        begin
            // Check the parent.
            getElementsByName1(intfCefDomNode, theName, Result);
        end; // if Assigned(intfCefDomNode) then
    end; // if Assigned(ADocoument) then
end;

// ---------------------------------------------------------------
4

1 に答える 1

3

現時点では、 JavaScriptgetElementsByNameや MSHTMLgetElementsByNameに組み込まれている機能Chromium Embeddedも、その Delphi ラッパーもありません。これを解決するには、すべての DOM 要素を繰り返し処理します。たとえば、次のような独自の DOM ビジター クラスを作成します。

VisitDomこの手順は非同期であるため、すぐに (実際には DOM ビジターが を終了する前にvisit) 戻り、実行時の DOM のスナップショットで動作することに注意してください。

type
  TElementNameVisitor = class(TCefDomVisitorOwn)
  private
    FName: string;
  protected
    procedure visit(const document: ICefDomDocument); override;
  public
    constructor Create(const AName: string); reintroduce;
  end;

procedure ProcessElementsByName(const AFrame: ICefFrame; const AName: string);
var
  Visitor: TElementNameVisitor;
begin
  if Assigned(AFrame) then
  begin
    Visitor := TElementNameVisitor.Create(AName);
    AFrame.VisitDom(Visitor);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ProcessElementsByName(Chromium1.Browser.MainFrame, 'NameAttributeValue');
end;

{ TDOMElementNameVisitor }

constructor TElementNameVisitor.Create(const AName: string);
begin
  inherited Create;
  FName := AName;
end;

procedure TElementNameVisitor.visit(const document: ICefDomDocument);

  procedure ProcessNode(ANode: ICefDomNode);
  var
    Node: ICefDomNode;
  begin
    if Assigned(ANode) then
    begin
      Node := ANode.FirstChild;
      while Assigned(Node) do
      begin
        if Node.GetElementAttribute('name') = FName then
        begin
          // do what you need with the Node here
          ShowMessage(Node.GetElementAttribute('value'));
        end;
        ProcessNode(Node);
        Node := Node.NextSibling;
      end;
    end;
  end;

begin
  ProcessNode(document.Body);
end;
于 2012-05-20T12:47:24.133 に答える