如何在Microsoft Edge中获取当前URL?

时间:2016-02-29 02:18:49

标签: delphi microsoft-edge geturl

自Microsoft Edge正式发布以来,Ialready已经看到了几个关于如何从Microsoft Edge获取活动URL的示例,主要是在C#.NET代码中。

但是直到现在还没有看到没有人为此做过Delphi版本。现在我正好知道是否有人已经这样做了。我需要的很多。

我的最后一次尝试是:

function GetEdgeActiveTabURL(Wnd: HWnd; Param: LParam): Bool; stdcall;
var
  urls: TStrings;
  hWndMainWindow, hWndTab: HWND;
  Buffer : array[0..255] of Char;
  res : boolean;
begin
  res := true;
  urls := TStrings(Param);
  SendMessage(Wnd, WM_GETTEXT, Length(Buffer), integer(@Buffer[0]));
  hWndMainWindow := FindWindow('ApplicationFrameWindow', Buffer);
  application.ProcessMessages;
  if hWndMainWindow <> 0 then
  begin
    hWndTab := FindWindowEx(hWndMainWindow, 0, 'Internet Explorer_Server', nil);
    if hWndTab <> 0 then
    begin
      SendMessage(hWndTab, WM_GETTEXT, Length(Buffer), integer(@Buffer));
      urls.Add(Buffer);
      res := false;
    end;
  end;
  Result := res;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
  Urls: TStringList;
begin
  Urls := TStringList.Create;
  try
    EnumWindows(@GetEdgeActiveTabURL, LParam(Urls));
    Memo1.Lines.AddStrings(Urls);
  finally
    FreeAndNil(Urls);
  end;

end;

编辑:

好的,所以,这就是我现在所拥有的一切,但这只显示了我的控制权。如何获得价值?

 uses
     ActiveX, UIAutomationClient_TLB {http://pastebin.com/a0b5J0FM};

     private
        { Private declarations }
        procedure FindItems(Recurse: Boolean);

    procedure TForm1.FindItems(Recurse: Boolean);
    var
      UIAuto: TCUIAutomation;
      condition: IUIAutomationCondition;
      collection: IUIAutomationElementArray;
      Length: Integer;
      Count: Integer;
      itemElement: IUIAutomationElement;
      retVal: Integer;
      val: WideString;

      ExpandCollapsePattern: IUIAutomationExpandCollapsePattern;
      FElement: IUIAutomationElement;
    begin
      UIAuto := TCUIAutomation.Create(nil);

      UIAuto.CreateTrueCondition(condition);

      UIAuto.ElementFromHandle(GetForegroundWindow, FElement);

      FElement.FindAll(TreeScope_Descendants, condition, collection);

      collection.Get_Length(length);

      for Count := 0 to length - 1 do
      begin
        collection.GetElement(Count, itemElement);
        itemElement.Get_CurrentControlType(retVal);

        if (retVal = UIA_EditControlTypeId) then
        begin
          ItemElement.Get_CurrentName(val);
          TThread.Synchronize(nil,
            procedure
            begin
              mmo1.lines.Add(val);
            end);

          itemElement.GetCurrentPattern(UIA_ExpandCollapsePatternId, IInterface(ExpandCollapsePattern));
          if Assigned(ExpandCollapsePattern) then
          begin
            ExpandCollapsePattern.Expand;
            if Recurse = True then
              FindItems(False);
          end;
        end;
      end;
      UIAuto.Free;
    end;

    procedure TForm1.tmr1Timer(Sender: TObject);
    begin
     TThread.CreateAnonymousThread(procedure begin CoInitializeEx(nil, 2); FindItems(true); CoUninitialize; end).Start;
     end;

0 个答案:

没有答案