如何在 TWebBrowser 中显示相对路径图像?

WeG*_*ars 2 html delphi twebbrowser

我在 DesignMode (Doc.DesignMode := 'On') 中使用 TWebBrowser 来编写 HTML 文档。TWebBrowser 中没有加载文档(磁盘上的 HTML 文件)。我直接在 TWebBrowser 中从零创建文档。html 代码将从 TWebBrowser 中提取并保存为 c:/MyProjects/SomeHtmlName.html。

问题是它不会显示我插入的图像,如果它们有相对路径。

更确切地说,如果我将此代码粘贴到 WebBrowser 中,它将立即显示图像:

<IMG src="file:///c:/MyProjects/resources/R.PNG">
Run Code Online (Sandbox Code Playgroud)

但是,如果我输入:

<IMG border=0 src="resources\R.PNG">
<IMG border=0 src="resources/R.PNG">   <---- preferred so it will work on Linux
Run Code Online (Sandbox Code Playgroud)

它将显示图像占位符而不是实际图像。

我需要相对路径,因此如果我更改根路径或将其上传到 FTP,网站仍然可以工作。


procedure TForm1.Button1Click(Sender: TObject);
begin
  LoadDummyPage;
  SetHtmlCode('<img src="resources/test_img.PNG">');
  Memo1.Text:= GetHtmlCode;
end;



function TForm1.LoadDummyPage: Boolean;
const FileName: string= 'c:\MyProject\_ONLINE WEB SITE\dummy.html';
begin
  if not Assigned(wbBrowser.Document)
  then wbBrowser.Navigate('about:blank');

  Result := FileExists(FileName);
  if Result
  then wbBrowser.Navigate('file://' + FileName)
  else Caption:= 'file not found';
end;



procedure TForm1.SetHtmlCode(const HTMLCode: string);
var
  Doc: Variant;
begin
  if not Assigned(wbBrowser.Document)
  then wbBrowser.Navigate('about:blank');

  Doc := wbBrowser.Document;
  Doc.Write(HTMLCode);
  Doc.Close;
  Doc.DesignMode := 'On';

  WHILE wbBrowser.ReadyState < READYSTATE_INTERACTIVE
   DO Application.ProcessMessages;

  Doc.body.style.fontFamily := 'Arial';
  Doc.Close;
end;


function TForm1.GetHtmlCode: string;             { Get the HTML code from the browser }
var
  Doc: IHTMLDocument2;
  BodyElement: IHTMLElement;
begin
  if Assigned(wbBrowser.Document) and (wbBrowser.Document.QueryInterface(IHTMLDocument2, Doc) = S_OK) then
  begin
    BodyElement := Doc.body;
    if Assigned(BodyElement) then
      Result := BodyElement.innerHTML;
  end;

  if Result > ''
  then Result := StringReplace(Result, '="about:', '="', [rfReplaceAll, rfIgnoreCase]);  { Fix the 'How stop TWebBrowser from adding 'file:///' in front of my links' bug }
end;
Run Code Online (Sandbox Code Playgroud)

kob*_*bik 5

您需要预先加载一个有效的 HTML“模板”字符串/流,包括BASE您在其中设置所需路径(带有尾部斜杠)的标签,例如"file:///c:/MyProjects/".

并切换到编辑模式,您的图像SRC应该是相对的,例如"resources/R.PNG"。您在编辑后最终提取的 HTML 应该是body.innerHTMLor body.outerHTML(无论您需要什么)。您甚至可以获取整个文档源(谷歌它)。

用没有BASE标签的有效 HTML/Body 包装提取的源代码并保存到磁盘c:\MyProjects.

但是为 IMG SRC 生成的代码是完整路径!

你对此无能为力。这就是 DOM 表示 HTML 的方式——HTML 源代码不是必需的。这种行为是不一致的。并且还取决于您如何插入图像(我不使用execCommand并拥有自己的对话框并插入我自己的 html 代码)。您需要"file:///c:/MyProjects/"用空字符串手动替换提取的源。至少,我是这样做的。

编辑:您不需要Navigate()外部文件。您可以通过document.write(HTML).

尝试这个:

const
  HTML_TEMPLATE = '<html><head><base href="file:///%s"></head><body style="font-family:Arial">%s</body></html>';

procedure TForm1.LoadHTML(HTMLCode: string);
var
  Doc: Variant;
  HTML, Path: string;
begin
  Path := 'D:\Temp\';
  HTML := Format(HTML_TEMPLATE, [Path, HTMLCode]);
  WebBrowser1.Navigate('about:blank');
  Doc := WebBrowser1.Document;
  Doc.Write(HTML);
  Doc.Close;
  Doc.DesignMode := 'On';
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  LoadHTML('<b>Hello</b><img SRC="resources/1.png">');
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  Doc: IHTMLDocument2;
begin
  Doc := WebBrowser1.Document as IHTMLDocument2;
  if Assigned(Doc) then
  begin
    ShowMessage(Doc.body.innerHTML); 
  end;
end;
Run Code Online (Sandbox Code Playgroud)

我的输出是:<B>Hello</B><IMG src="resources/1.png">. 在某些情况下,src 可能包含完整路径。当这种情况发生时,我不能 100% 确定。但是您需要准备好通过手动替换路径来处理这种情况。没有关于这一点的决定性文件,所以我总是在任何情况下处理这个问题。

  • @DarkPresidentOfAmerica,“希望我能让 TWeBBrowser 真正使用相对路径”。但是当您使用 BASE 时它*确实*!:) `innerHTML` 是 DOM 如何解析源代码的表示。我知道这个替代品是“hacky”,但是AFAIK你无能为力。 (2认同)