Kategorien
PC & Accessoires Sonstiges

DOM: Javascript vs. C++

Musste das Syntax-Highlighting wegmachen, das hat das Layout zerstört

Ich geh gerade mal sterben. Beide Funktionen haben dieselbe Funktionalität, nämlich alles Bilder innerhalb einer Webseite zu „verstecken“. Hier der Javascript-Code

function RemoveImages(doc) {
  var images = doc.images;
    if (images != null) {
      for (var i = 0; i < images.length; i++) {
        var img = images.item(i);
        img.style.display = "none";
      }
    }
}


Hurra. Nun in C++

void CHelloWorldBHO::RemoveImages(IHTMLDocument2* pDocument) {
  CComPtr<IHTMLElementCollection> spImages;

  // Get the collection of images from the DOM.
  HRESULT hr = pDocument->get_images(&spImages;);
  if (hr == S_OK && spImages != NULL) {
    // Get the number of images in the collection.
    long cImages = 0;
    hr = spImages->get_length(&cImages);
    if (hr == S_OK && cImages > 0) {
      for (int i = 0; i < cImages; i++) {
        CComVariant svarItemIndex(i);
        CComVariant svarEmpty;
        CComPtr<IDispatch> spdispImage;

        // Get the image out of the collection by index.
        hr = spImages->item(svarItemIndex, svarEmpty, &spdispImage);
        if (hr == S_OK && spdispImage != NULL) {
          // First, query for the generic HTML element interface...
          CComQIPtr<IHTMLElement> spElement = spdispImage;

          if (spElement) {
            // ...then ask for the style interface.
            CComPtr<IHTMLStyle> spStyle;
            hr = spElement->get_style(&spStyle;);

            // Set display="none" to hide the image.
            if (hr == S_OK && spStyle != NULL) {
              static const CComBSTR sbstrNone(L"none");
              spStyle->put_display(sbstrNone);
            }
          }
        }
      }
    }
  }
}

Kommentar:

Interacting with the DOM in C++ is more verbose than JavaScript, but the code flow is essentially the same.

Quelle: http://msdn.microsoft.com/en-us/library/bb250489.aspx