Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Internet Explorer AdBlock

, 17 Jan 2008 GPL3
Internet Explorer AdBlock Add-on
ieadblock_src.zip
IE AdBlockPS
IE AdBlockps.def
IE AdBlock
AdBlockBand.rgs
AdBlockBHO.rgs
bin
XListCtrlSSDU.lib
XListCtrlSSRU.lib
IE AdBlock.def
IE AdBlock.rgs
res
bmpToolbar.bmp
checkboxes.bmp
icoSettings.ico
XListCtrl
checkboxes.bmp
//*********************************************************************
// INCLUDES
//*********************************************************************

#include "stdafx.h"
#include "AdBlock.h"

//*********************************************************************
// CONSTRUCTOR & DESTRUCTOR
//*********************************************************************

CAdBlock::CAdBlock()
{
	// Initialize singleton classes
	m_pSettings = CSettings::Instance();
	m_pListData = CListData::Instance();
}

//=====================================================================

CAdBlock::~CAdBlock()
{
}

//*********************************************************************
// PUBLIC FUNCTIONS
//*********************************************************************

int CAdBlock::BlockAdvertisements(IHTMLDocument2 * pDocument)
{
	// Declare variables
	CComPtr<IHTMLElementCollection> spApplets;
	CComPtr<IHTMLElementCollection> spEmbeds;
	CComPtr<IHTMLFramesCollection2> spFrames;
	CComPtr<IHTMLElementCollection> spImages;
	CComPtr<IHTMLElementCollection> spImagesUsingEnum;
	CComPtr<IHTMLElementCollection> spScripts;
	CComPtr<IHTMLStyleSheetsCollection> spStyleSheets;
	int iObjectsBlocked = 0;

	// Declare variables
	LARGE_INTEGER iFrequency, iTimeStart, iTimeEnd;
	int iAppletsTime = 0, iEmbedsTime = 0, iFramesTime = 0;
	int iImagesTime = 0, iScriptsTime = 0, iStyleSheetsTime = 0;

	// Calculate frequency
	QueryPerformanceFrequency(&iFrequency);

	// Check if we have a valid document
	if (pDocument != NULL)
	{
		// Record the time
		QueryPerformanceCounter(&iTimeStart);

		// Remove applets
		if (m_pSettings->GetHandleApplets())
		{
			if (pDocument->get_applets(&spApplets) == S_OK)
			{
				iObjectsBlocked += RemoveObjects(spApplets);
			}
		}

		// Stop the time
		QueryPerformanceCounter(&iTimeEnd);

		// Calculate duration
		iAppletsTime = (int)((iTimeEnd.QuadPart - iTimeStart.QuadPart) / (iFrequency.QuadPart / 1000));

		// Record the time
		QueryPerformanceCounter(&iTimeStart);

		// Remove embeds
		if (m_pSettings->GetHandleEmbeds())
		{
			if (pDocument->get_embeds(&spEmbeds) == S_OK)
			{
				iObjectsBlocked += RemoveObjects(spEmbeds);
			}
		}
		// Stop the time
		QueryPerformanceCounter(&iTimeEnd);

		// Calculate duration
		iEmbedsTime = (int)((iTimeEnd.QuadPart - iTimeStart.QuadPart) / (iFrequency.QuadPart / 1000));

		// Record the time
		QueryPerformanceCounter(&iTimeStart);

		// Remove frames
		if (m_pSettings->GetHandleFrames())
		{
			if (pDocument->get_frames(&spFrames) == S_OK)
			{
				iObjectsBlocked += RemoveFrames(spFrames, pDocument);
			}
		}

		// Stop the time
		QueryPerformanceCounter(&iTimeEnd);

		// Calculate duration
		iFramesTime = (int)((iTimeEnd.QuadPart - iTimeStart.QuadPart) / (iFrequency.QuadPart / 1000));

		// Record the time
		QueryPerformanceCounter(&iTimeStart);

		// Remove images
		if (m_pSettings->GetHandleImages())
		{
			if (pDocument->get_images(&spImages) == S_OK)
			{
				iObjectsBlocked += RemoveObjects(spImages);
			}
		}

		// Stop the time
		QueryPerformanceCounter(&iTimeEnd);

		// Calculate duration
		iImagesTime = (int)((iTimeEnd.QuadPart - iTimeStart.QuadPart) / (iFrequency.QuadPart / 1000));

		// Record the time
		QueryPerformanceCounter(&iTimeStart);

		// Remove scripts
		if (m_pSettings->GetHandleScripts())
		{
			if (pDocument->get_scripts(&spScripts) == S_OK)
			{
				iObjectsBlocked += RemoveObjects(spScripts);
			}
		}

		// Stop the time
		QueryPerformanceCounter(&iTimeEnd);

		// Calculate duration
		iScriptsTime = (int)((iTimeEnd.QuadPart - iTimeStart.QuadPart) / (iFrequency.QuadPart / 1000));

		// Record the time
		QueryPerformanceCounter(&iTimeStart);

		// Remove stylesheets
		if (m_pSettings->GetHandleStylesheets())
		{
			if (pDocument->get_styleSheets(&spStyleSheets) == S_OK)
			{
				iObjectsBlocked += RemoveStyleSheets(spStyleSheets);
			}
		}

		// Stop the time
		QueryPerformanceCounter(&iTimeEnd);

		// Calculate duration
		iStyleSheetsTime = (int)((iTimeEnd.QuadPart - iTimeStart.QuadPart) / (iFrequency.QuadPart / 1000));
	}

	// Show summary
	if (m_pSettings->GetShowPerformanceMessages())
	{
		CString sSummary = _T("");
		sSummary.Format(_T("IE AdBlock performance info:\n\nApplets:\t\t%d ms\nEmbeds:\t\t%d ms\nFrames:\t\t%d ms\nImages:\t\t%d ms\nScripts:\t\t%d ms\nStylesheets:\t%d ms"),
			iAppletsTime, iEmbedsTime, iFramesTime, iImagesTime, iScriptsTime, iStyleSheetsTime);
		AfxMessageBox(sSummary, MB_ICONINFORMATION);
	}

	// Return number of blocked elements
	return iObjectsBlocked;
}

//*********************************************************************
// PRIVATE FUNCTIONS
//*********************************************************************

int	CAdBlock::RemoveFrames(IHTMLFramesCollection2 * pFramesCollection, IHTMLDocument2 * pDocument)
{
	// Declare variables
	long lObjectCount = 0;
	HRESULT hr = S_OK;
	int iObjectsBlocked = 0;

	// Get length
	hr = pFramesCollection->get_length(&lObjectCount);

	// Did we found any objects?
    if ((hr == S_OK) && (lObjectCount > 0))
	{
		// Loop all objects
        for (int i = 0; i < lObjectCount; i++)
		{
			// Declare variables specially required by frames
			IOleContainer * pContainer = NULL;
			IEnumUnknown * pEnumerator = NULL;
			IHTMLDocument2 * pFrameDoc = NULL;
			IWebBrowser2 * pFrameBrowser = NULL;
			IDispatch * pFrameDispatch = NULL;
			IUnknown * pUnk = NULL;
			ULONG uFetched = 0;

			// Get the container
			hr = pDocument->QueryInterface(IID_IOleContainer, (void**)&pContainer);
			if (FAILED(hr)) break;

			// Get an enumerator for the frames
			hr = pContainer->EnumObjects(OLECONTF_EMBEDDINGS, &pEnumerator);
			if (FAILED(hr)) break;

			// Release container
			pContainer->Release();

			// Enumerate and refresh all the frames
			for (UINT i = 0; S_OK == pEnumerator->Next(1, &pUnk, &uFetched); i++)
			{
				// Query frame browser
				hr = pUnk->QueryInterface(IID_IWebBrowser2, (void**)&pFrameBrowser);

				// Release unknown
				pUnk->Release();

				// Check if we failed
				if (FAILED(hr)) break;

				// Did we succeed?
				if (SUCCEEDED(hr))
				{
					// Get document
					hr = pFrameBrowser->get_Document(&pFrameDispatch);
					hr = pFrameDispatch->QueryInterface(IID_IHTMLDocument2, (void **)&pFrameDoc);

					// Remove advertisement from frame
					iObjectsBlocked += BlockAdvertisements(pFrameDoc);

					// Release
					pFrameBrowser->Release();
					pFrameDispatch->Release();
					pFrameDoc->Release();
				}
			}

			// Release enumerator
			pEnumerator->Release();
		}
	}

	// Return number of blocked elements
	return iObjectsBlocked;
}

//=====================================================================

int CAdBlock::RemoveObjects(IHTMLElementCollection * pElementCollection)
{
	// Declare variables
	long lObjectCount = 0;
	HRESULT hr = S_OK;
	int iObjectsBlocked = 0;

	// Get length
	hr = pElementCollection->get_length(&lObjectCount);

	// Did we found any objects?
    if ((hr == S_OK) && (lObjectCount > 0))
	{
		// Loop all objects
        for (int i = 0; i < lObjectCount; i++)
		{
			// Declare variables
            CComVariant svarItemIndex = i;
			CComVariant svarEmpty;
            CComPtr<IDispatch> spdispObject;

			// Get all items
			hr = pElementCollection->item(svarItemIndex, svarEmpty, &spdispObject);

			// Did we get a valid object?
            if ((hr == S_OK) && (spdispObject != NULL))
            {
                // First, query for the generic HTML element interface...
                CComQIPtr<IHTMLElement> spElement = spdispObject;

				// Do we have a valid element?
                if (spElement)
                {
					// Handle item
					if (HandleItem(spElement))
					{
						// Increase objects blocked
						iObjectsBlocked++;
					}
				}
			}
		}
	}

	// Return number of blocked elements
	return iObjectsBlocked;
}

//=====================================================================

int	CAdBlock::RemoveStyleSheets(IHTMLStyleSheetsCollection * pStyleSheetsCollection)
{
	// Declare variables
	HRESULT hr = S_OK;
	int iObjectsBlocked = 0;
	IUnknown * pUnknown = NULL;
	IEnumVARIANT * pNewEnum = NULL;
	IHTMLStyleSheet * pElement = NULL;
	VARIANT varElement;
	BSTR bstrStyleSheet = _T("");
	CString sStyle = _T("");

	// Get enumerator
	if (SUCCEEDED(pStyleSheetsCollection->get__newEnum(&pUnknown)) && (pUnknown != NULL))
	{
		// Get new enumerator
		pUnknown->QueryInterface(IID_IEnumVARIANT, (void **)&pNewEnum);

		// Check if we have an enumerator
		if (pNewEnum == NULL)
		{
			// No, exit	
			return iObjectsBlocked;
		}

		// Loop all items
		while (pNewEnum->Next(1, &varElement, NULL) == S_OK)
		{
			// Check if we can use this item
			if (varElement.vt != VT_DISPATCH)
			{
				// No, clear variant
				VariantClear(&varElement);
				
				// Break out of loop
				break;
			}

			// Get the element
			varElement.pdispVal->QueryInterface(IID_IHTMLStyleSheet, (void **)&pElement);

			// Do we have a valid element?
			if (pElement != NULL)
			{
				// Get or set style properties
				/*pElement->get_cssText(&bstrStyleSheet);
				_bstr_t bstr1(bstrStyleSheet);
				sStyle.Format(_T("%s"), (LPCTSTR)bstr1);*/
			}

			// Clear variant
			VariantClear(&varElement);
		}

		// Release enumerator
		if (pNewEnum != NULL)
		{
			pNewEnum->Release();
		}
	}

	// Return number of blocked elements
	return iObjectsBlocked;
}

//=====================================================================

void CAdBlock::HideElement(CComQIPtr<IHTMLElement> spElement)
{
	// Declare variables
	CComPtr<IHTMLStyle> spStyle;
	HRESULT hr = spElement->get_style(&spStyle);

	// Is element a frame?
	if (false)
	{
    /*var frameTags = {FRAME: true, FRAMESET: true};
    var index = -1;
    for (var frame = node; frame; frame = frame.previousSibling)
      if (frame.nodeName.toUpperCase() in frameTags)
        index++;

    var attr = (parentNode.hasAttribute("rows") ? "rows" : "cols");
    var weights = parentNode.getAttribute(attr).split(",");
    weights[index] = "0";
    parentNode.setAttribute(attr, weights.join(","));*/
	}
	else
	{
		// Set display="none" to hide the object
		if ((hr == S_OK) && (spStyle != NULL))
		{
			static const CComBSTR sbstrNone(_T("none"));
			spStyle->put_display(sbstrNone);
		}
	}
}

//=====================================================================

void CAdBlock::HideNamedElement(IHTMLDocument2 * pDocument, CString sName,
								   ElementType iType)
{
	// Declare variables
	CComPtr<IHTMLElementCollection>	spAll;
	HRESULT hr = E_FAIL;
	long lCount = 0;

	// Get the collection of objects from the DOM
	hr = pDocument->get_all(&spAll);

	// Check if we succeeded
    if (hr == S_OK)
    {
        // Get the number of objects in the collection
		hr = spAll->get_length(&lCount);

		// Did we found any objects?
        if ((hr == S_OK) && (lCount > 0))
		{
			// Declare variables
			CComVariant svarItemIndex = sName;
			CComVariant svarEmpty;
			CComPtr<IDispatch> spdispObject;

            // Get the object out of the collection by index
			hr = spAll->item(svarItemIndex, svarEmpty, &spdispObject);

			// Did we get a valid object?
            if ((hr == S_OK) && (spdispObject != NULL))
            {
                // First, query for the generic HTML element interface...
                CComQIPtr<IHTMLElement> spElement = spdispObject;

				// Do we have a valid element?
                if (spElement)
                {
					// Hide element
					HideElement(spElement);
				}
			}
		}
	}
}

//=====================================================================

inline bool CAdBlock::HandleItem(CComQIPtr<IHTMLElement> spElement)
{
	// Declare variables
	CComVariant svarURL = _T("");
	CComBSTR bstrElementType = _T("");
	HRESULT hr = S_OK;

	// Get source url
	hr = spElement->getAttribute(_T("src"), 0, &svarURL);
	if ((hr != S_OK) || (svarURL.vt == VT_NULL))
	{
		// Exit function, hiding not necessary
		return false;
	}

	// Get element type
	hr = spElement->get_tagName(&bstrElementType);
	if (hr != S_OK) 
	{
		// Exit function, hiding not necessary
		return false;
	}

	// Is this site listed on whitelist?
	if ((svarURL.vt != VT_NULL) &&
		(!m_pListData->GetWhiteList()->Matches(svarURL, CString(bstrElementType))))
	{
		// Is this blacklisted?
		if (m_pListData->GetBlackList()->Matches(svarURL, CString(bstrElementType)))
		{
			// Hide element
			HideElement(spElement);

			// We have hidden the item
			return true;
		}
	}			

	// Hiding not neccessary
	return false;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)

Share

About the Author

Geert van Horrik
Software Developer CatenaLogic
Netherlands Netherlands
I am Geert van Horrik, and I have studied Computer Science in the Netherlands.
 
I love to write software using .NET (especially the combination of WPF and C#). I am also the lead developer of Catel, an open-source application development framework for WPF, Silverlight, WP7 and WinRT with the focus on MVVM.
 
I have my own company since January 1st 2007, called CatenaLogic. This company develops commercial and non-commercial software.
 
To download (or buy) applications I have written, visit my website: http://www.catenalogic.com
Follow on   Twitter

| Advertise | Privacy | Mobile
Web03 | 2.8.141022.2 | Last Updated 17 Jan 2008
Article Copyright 2008 by Geert van Horrik
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid