自上次显示以来,检测Delphi TWebBrowser网页已更改的最佳方法是什么?

Bri*_*ost 5 delphi tdatetime twebbrowser

我想使用Deplhi TWebBrowser在表单中显示“新闻”页面。新闻页面是一个简单的HTML页面,我们会不时将其上传到我们的网站,并且可能会通过各种工具输出。显示屏很好,但是我想在我的应用程序中知道自上次显示以来它是否已更改,因此理想情况下,我想获取其修改日期/时间或大小/校验和。精度并不重要,并且理想情况下不应依赖可能会失败的属性,因为使用了“简单”工具来编辑HTML文件,例如NotePad。在网上检查有几个文档修改过的java调用,但是我真的不知道从哪里开始。我浏览了Delphi的Winapi中的许多调用。WinInet单元,我看到我可以使用HTTP来获取文件以对其进行检查,但这似乎就像用大锤将核桃开裂一样。我也看不到任何文件日期时间功能,这使我觉得我缺少明显的东西。我正在使用Delphi XE5。我应该朝哪个方向看?感谢您的指导。

Bri*_*ost 1

感谢 kobik、David 和 TLama 的建议和指点,我意识到我实际上确实需要一把大锤,并且我终于想出了这个解决方案(我可能不是第一个,也不是最后一个!)。我必须读取文件内容,因为这似乎是检测更改的更好方法。下面的代码很少从 TTimer 调用“CheckForWebNewsOnTimer”,并使用 Indy 读取新闻页面,对其内容生成 MD5 哈希值,并将其与注册表中存储的先前哈希值进行比较。如果内容发生变化,或者120天过去了,就会弹出该页面。代码有问题,例如,对页面上链接图像的更改可能不会触发更改,但嘿,它唯一的新闻,文本几乎总是会发生变化。

function StreamToMD5HashHex( AStream : TStream ) : string;
// Creates an MD5 hash hex of this stream
var
  idmd5 : TIdHashMessageDigest5;
begin
  idmd5 := TIdHashMessageDigest5.Create;
  try
    result := idmd5.HashStreamAsHex( AStream );
  finally
    idmd5.Free;
  end;
end;



function HTTPToMD5HashHex( const AURL : string ) : string;
var
  HTTP : TidHTTP;
  ST : TMemoryStream;
begin
  HTTP := TidHTTP.Create( nil );
  try
    ST := TMemoryStream.Create;
    try
      HTTP.Get( AURL, ST );
      Result := StreamToMD5HashHex( ST );
    finally
      ST.Free;
    end;
  finally
    HTTP.Free;
  end;
end;




function ShouldShowNews( const ANewHash : string; AShowAfterDays : integer ) : boolean;
const
  Section = 'NewsPrompt';
  IDHash  = 'LastHash';
  IDLastDayNum = 'LastDayNum';
var
  sLastHash : string;
  iLastPromptDay : integer;
begin


  // Check hash
  sLastHash := ReadRegKeyUserStr( Section, IDHash, '' );
  Result := not SameText( sLastHash, ANewHash );
  if not Result then
    begin
    // Check elapsed days
    iLastPromptDay := ReadRegKeyUserInt( Section, IDLastDayNum, 0 );
    Result := Round( Now ) - iLastPromptDay > AShowAfterDays;
    end;

  if Result then
    begin
    // Save params for checking next time.
    WriteRegKeyUserStr( Section, IDHash, ANewHash );
    WriteRegKeyUserInt( Section, IDLastDayNum, Round(Now) );
    end;
end;





procedure CheckForWebNewsOnTimer;
var
  sHashHex, S : string;
begin
  try
    S := GetNewsURL; // < my news address
    sHashHex := HTTPToMD5HashHex( S );
    If ShouldShowNews( sHashHex, 120 {days default} ) then
      begin
      WebBrowserDlg( S );
      end;

  except
    // .. ignore or save as info
  end;
end;
Run Code Online (Sandbox Code Playgroud)