Question

I use IHTMLDocument2 interface to retrieve IHTMLStyleSheetRulesCollection using the IHTMLDocument2::styleSheets property. It works fine for retrieving styles within <style> tag (in document <head>).

I use the above to find all styles which have backgroundImage set (I need to read the value).

However, this collection doesn't include inline styles inside <body> like:

<p style="background:url(image.jpg);"></p> to extract "image.jpg" from this <p> tag.

What's the approach to scan entire DOM tree to find elements that have "style" property attached with background image? Code examples in C++ or Delphi are welcome.

Was it helpful?

Solution

You will have to iterate all instead of styleSheets and take a look on the styles of the elements.

uses mshtml;

Procedure WebBrowserCreateStringListImagesFromElementStyles(const AWebBrowser: TWebBrowser; ResultList: TStrings; NoUrl: Boolean = false);
var
  nCount: Integer;
  element: IHTMLElement;
  elements: IHTMLElementCollection;
  bgImage: String;
begin
  Assert(Assigned(AWebBrowser.Document) and Assigned(ResultList));
  if Assigned(AWebBrowser.Document) then
  begin
    elements := (AWebBrowser.Document as IHTMLDocument2).all;
    for nCount := 0 To Pred(elements.Length) do
    begin
      element := elements.item(nCount, '') as IHTMLElement;
      if (element.style.backgroundImage <> '') and (UpperCase(element.style.backgroundImage) <> 'NONE') then
      begin
        bgImage := element.style.backgroundImage;
        if NoUrl then
        begin
          if Pos('URL(', UpperCase(bgImage)) = 1 then
          begin
            Delete(bgImage, 1, Length('URL('));
            if Pos(')', bgImage) = Length(bgImage) then
              Delete(bgImage, Length(bgImage), 1);
          end;
        end;
        ResultList.Add(AnsiLowerCase(bgImage));
      end;
    end;
  end;
end;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top