【问题标题】:What is the best way to get the internet explorer current url as a string将 Internet Explorer 当前网址作为字符串获取的最佳方法是什么
【发布时间】:2014-04-10 20:50:17
【问题描述】:

我正在寻找一种从当前 Internet Explorer URL 以字符串形式返回 url 的方法。

这种方法使用 dde。它非常快并且运行良好,除了它返回一个非常长的字符串,分为两部分,都带有引号。

uses
  ddeman;

function GetURL(Service: string): string;
var
  ClDDE: TDDEClientConv;
  temp: PAnsiChar;
begin
  Result := '';
  ClDDE := TDDEClientConv.Create(nil);
  with ClDDE do
  begin
    SetLink(Service, 'WWW_GetWindowInfo');
    temp := RequestData('0xFFFFFFFF');
    Result := StrPas(temp);
    StrDispose(temp);
    CloseLink;
  end;
  ClDDE.Free;
end;

例如,这会返回: "http://core2.staticworld.net/images/article/2014/01/counterfeit_android_apps1-100227383-medium.jpg","http://core2.staticworld.net/images/article/2014/01/counterfeit_android_apps1-100227383-medium.jpg"

但我只在第一个逗号之前寻找不带引号的第一部分: http://core2.staticworld.net/images/article/2014/01/counterfeit_android_apps1-100227383-medium.jpg

对另一种方法或如何解析字符串以生成不带引号且仅显示字符串第一部分的结果有任何建议吗?

【问题讨论】:

  • 为什么不能只提取逗号前的部分,然后自己删除引号?
  • 我想知道当背景中有多个带有多个标签的ie窗口时,您如何定义当前 url。
  • 每当我看到一个逗号分隔的双引号字符串时,我的脑海中就会出现TStringList.DelimitedText :o)

标签: delphi delphi-xe4


【解决方案1】:

您可以自己非常简单地解析字符串:

// Extract string up to position of the ,
Temp := Copy(Temp, 1, Pos(',', Temp) - 1);
// Remove double-quotes from resulting string
Temp := StringReplace(temp, '"', '', [rfReplaceAll]);

这是一个示例控制台应用程序,它在您提供的示例响应上使用上述逻辑,并将最终内容输出到控制台:

program Project2;

{$APPTYPE CONSOLE}

uses
  SysUtils;

const
  LinkStr = '"http://core2.staticworld.net/images/article/2014/01/counterfeit_android_apps1-100227383-medium.jpg","http://core2.staticworld.net/images/article/2014/01/counterfeit_android_apps1-100227383-medium.jpg"';

var
  Temp: string;

begin
  Temp := Copy(LinkStr, 1, Pos(',', LinkStr) - 1);
  Temp := StringReplace(Temp, '"', '', [rfReplaceAll]);
  Writeln('After: ' + Temp);
  ReadLn;
end.

输出:

【讨论】:

  • @Ken... 引号被删除了,但不幸的是整个字符串被返回,而不是逗号之前的字符串的第一部分。
  • @Bill:这不是真的。请参阅我编辑的答案,它使用您提供的 exact 字符串(我复制并粘贴了它),并仅输出第一部分(直到逗号)并删除了引号。
  • 很高兴知道是否还有其他方法可以替代使用 dde。感谢肯的帮助。
  • @Bill:你要求要么/要么。我对第一个没有答案,但解析很容易。 :-) 正如你所说,它“非常快并且运行良好”,并且解析代码也非常快并且运行良好......?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-08
  • 1970-01-01
  • 2010-09-08
  • 1970-01-01
  • 2021-04-16
  • 1970-01-01
相关资源
最近更新 更多