Beispiel #1
0
void CDocEvtHandler::OnMouseMove(MSHTML::IHTMLEventObjPtr pEvtObj)
{
	MSHTML::IHTMLElementPtr pElement = pEvtObj->GetsrcElement(); // 事件发生的对象元素

	//活动对象改变时进行处理
	if (m_pLastActiveElement != pElement)
	{
		m_pLastActiveElement = pElement;

		_bstr_t strTagname;
		pElement->get_outerHTML(&strTagname.GetBSTR());
		CStdString strMsg = strTagname;

		m_pNotifyView->SendMessage(WM_VIEW_EVENT_ACTIVE_ELEMENT, (WPARAM)(LPCTSTR)strMsg);
	}
}
/**
 * Gets value of specified attribute out of some HTML Tag
 * @param pElem HTML element pointer
 * @param bstrText Name of the attribute
 * @param sValue Reference of the string that will hold the value
 * @return true if attribute is present false otherwise
 */
bool COfflineBrowser::GetAttribute(MSHTML::IHTMLElementPtr pElem, BSTR bstrText, std::string& sValue)
{
	try
	{
		CComVariant var = pElem->getAttribute(bstrText, 0);
		USES_CONVERSION;
		if (var.vt == VT_BSTR && var.bstrVal != NULL)
			sValue = (LPCTSTR)OLE2T(var.bstrVal);
		else
			sValue = "";
	}
	//Attribute not present
	catch (_com_error e)
	{
		sValue = "";
	}
	//Special condition for about:blank, keep remember it
	int nIndex = sValue.find("about:blank");
	if(nIndex == 0)
	{
		int nLen = strlen("about:blank");
		sValue = sValue.substr(nLen, sValue.length()-nLen);
	}
	return sValue.length() ? true : false;
}
/**
 * Sets value of specified attribute in some HTML Tag
 * @param pElem HTML element pointer
 * @param bstrText Name of the attribute
 * @param sValue Value of the attribute
 */
void COfflineBrowser::SetAttribute(MSHTML::IHTMLElementPtr pElem, BSTR bstrText, const std::string& sValue)
{
	try
	{
		pElem->setAttribute(bstrText, sValue.c_str(), 0);
	}
	//Some erroneous condition occur, it shouldn't be thrown under normal conditions
	catch(_com_error e)
	{
		int a;
		a=0;
	}
}
/**
 * It Downloads all the resources, main function of concern, all the required members variables must 
 * be populated for it to work correctly
 */
void COfflineBrowser::BrowseOffline()
{
	USES_CONVERSION;

	//Delete if this directory exists, it is a disputed call, you can safely comment it out
	DeleteDirectory(m_sDir);
	//Create the directory again
	CreateDirectory(m_sDir.c_str(), NULL);
	//SaveHtml(m_sHtml);
	
	//Create HTML Document which will be hold the html that was have 
	MSHTML::IHTMLDocument2Ptr pDoc;
	HRESULT hr = CoCreateInstance(CLSID_HTMLDocument, NULL, CLSCTX_INPROC_SERVER, 
								  IID_IHTMLDocument2, (void**)&pDoc);
	if(pDoc == NULL)
		return;

	//Load HTML to Html Document
	SAFEARRAY* psa = SafeArrayCreateVector(VT_VARIANT, 0, 1);
	VARIANT *param;
	bstr_t bsData = (LPCTSTR)m_sHtml.c_str();
	hr =  SafeArrayAccessData(psa, (LPVOID*)&param);
	param->vt = VT_BSTR;
	param->bstrVal = (BSTR)bsData;
	
	//write your buffer
	hr = pDoc->write(psa);	
	//closes the document, "applying" your code  
	hr = pDoc->close();	

	//Don't forget to free the SAFEARRAY!
	SafeArrayDestroy(psa);

	//Iterate through all the elements in the document
	MSHTML::IHTMLElementCollectionPtr pCollection = pDoc->all;

	for(long a=0;a<pCollection->length;a++)
	{
		std::string sValue;
		IHTMLElementPtr pElem = pCollection->item( a );
		//If src attribute is found that means we've a resource to download
		if(GetAttribute(pElem, L"src", sValue))
		{
			//If resource URL is relative
			if(!IsAbsolute(sValue))
			{
				if(sValue[0] == '/')
					sValue = sValue.substr(1, sValue.length()-1);
				//Create directories needed to hold this resource
				//CreateDirectories(sValue, m_sDir);
				//Download the resource
				if(1)//!DownloadResource(sValue, sValue))
				{
					std::string sTemp = m_sScheme + m_sHost;
					sTemp += sValue;
					//Update src to the new src and put the original src attribute as
					//srcdump just for future references
					if(sTemp[0] == '/')
						sTemp = sTemp.substr(1, sTemp.length()-1);
					SetAttribute(pElem, L"src", sTemp);
					SetAttribute(pElem, L"srcdump", sValue);
				}
				//Unable to download the resource
				else
				{
					//Put srcdump same as src, It if for no use, I just put it to make
					//HTML DOM consistent
					SetAttribute(pElem, L"srcdump", sValue);
				}
			}
			//If resource URL is absolute
			else
			{
				std::string sTemp;
				//Make URL relative
				sTemp = TrimHostName(sValue);
				//Create directories needed to hold this resource
				//CreateDirectories(sTemp, m_sDir);
				//Dowload the resource
				if(1)//DownloadResource(sTemp, sTemp))
				{
					//Update src to the new src and put the original src attribute as
					//srcdump just for future references
					if(sTemp[0] == '/')
						sTemp = sTemp.substr(1, sTemp.length()-1);
					SetAttribute(pElem, L"src", sTemp);
					SetAttribute(pElem, L"srcdump", sValue);
				}
			}
		}
	}
	
	//Get upated HTML out of amendments we made and save it to the described directory
	MSHTML::IHTMLDocument3Ptr pDoc3 = pDoc;
	MSHTML::IHTMLElementPtr pDocElem;
	pDoc3->get_documentElement(&pDocElem);
	BSTR bstrHtml;
	pDocElem->get_outerHTML(&bstrHtml);
	std::string sNewHtml((const wchar_t*)OLE2T(bstrHtml));
	SaveHtml(sNewHtml);
}