Example #1
0
//-----------------------------------------------------------------------------
// Name: AddHtmlContentToElement
// Object: 
// Parameters :
//     in  : 
//     out : 
//     return : 
//-----------------------------------------------------------------------------
BOOL CHtmlViewer::AddHtmlContentToElement(TCHAR* Id,TCHAR* Content)
{
    if (this->pIWebBrowser==NULL)
        return FALSE;

    if (!this->IsPageCompleted())
        return FALSE;

    HRESULT hr;
    BOOL bRet;
    IDispatch* pHtmlDocDispatch = NULL;
    IHTMLDocument3* pHtmlDoc = NULL;
    IHTMLElement* pElem = NULL;
    BSTR bstrElementId;

#if ((!defined(UNICODE))&& (!defined(_UNICODE)))
    WCHAR* psz;
#endif

    // get IDispatch document interface
    hr = pIWebBrowser->get_Document(&pHtmlDocDispatch);
    if (FAILED (hr) || (pHtmlDocDispatch == NULL))
        return FALSE;

    // get IHTMLDocument3 document interface
    hr = pHtmlDocDispatch->QueryInterface(IID_IHTMLDocument3,(void**)&pHtmlDoc);
    if (FAILED (hr) || (pHtmlDoc == NULL))
    {
        pHtmlDocDispatch->Release();
        return FALSE;
    }

    // get pointer to element from it's Id
#if (defined(UNICODE)||defined(_UNICODE))
    bstrElementId=SysAllocString(Id);
#else
    CAnsiUnicodeConvert::AnsiToUnicode(Id,&psz);
    bstrElementId=SysAllocString(psz);
    free(psz);    
#endif

    hr = pHtmlDoc->getElementById(bstrElementId,&pElem);
    SysFreeString(bstrElementId);
    if (FAILED (hr) || (pElem == NULL))
    {
        pHtmlDoc->Release();
        pHtmlDocDispatch->Release();
        return FALSE;
    }

    bRet=this->AddHtmlContentToElement(pElem,Content);

    pElem->Release();
    pHtmlDoc->Release();
    pHtmlDocDispatch->Release();

    return bRet;
}
Example #2
0
HRESULT TestMarkupServices(BSTR bstrHtml, MarkupCallback *pCallback, BSTR &message)
{
	IHTMLDocument3 *pHtmlDocRoot = NULL;

	// Create the root document -- a "workspace" for parsing.
	HRESULT hr = CoCreateInstance(CLSID_HTMLDocument, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pHtmlDocRoot));
	if (SUCCEEDED(hr) && pHtmlDocRoot) {
		IPersistStreamInit *pPersistStreamInit = NULL;

		hr = pHtmlDocRoot->QueryInterface(IID_PPV_ARGS(&pPersistStreamInit));
		if (SUCCEEDED(hr)) {
			// Initialize the root document to a default state -- ready for parsing.
			pPersistStreamInit->InitNew();

			IMarkupServices *pMarkupServices = NULL;
			hr = pHtmlDocRoot->QueryInterface(IID_PPV_ARGS(&pMarkupServices));
			if (SUCCEEDED(hr)) {
				IMarkupPointer *pMarkupBegin = NULL;
				IMarkupPointer *pMarkupEnd = NULL;

				// These markup pointers indicate the insertion point.
				hr = pMarkupServices->CreateMarkupPointer(&pMarkupBegin);
				if (SUCCEEDED(hr))
					hr = pMarkupServices->CreateMarkupPointer(&pMarkupEnd);

				if (SUCCEEDED(hr) && pMarkupBegin && pMarkupEnd) {
					IMarkupContainer *pMarkupContainer = NULL;

					// Parse the string -- the markup container contains the parsed HTML.
					// Markup pointers are updated to point to begining and end of new container.
					hr = pMarkupServices->ParseString(bstrHtml, 0, &pMarkupContainer, pMarkupBegin, pMarkupEnd);
					if (SUCCEEDED(hr) && pMarkupContainer) {
						IHTMLDocument3 *pHtmlDoc = NULL;

						// Retrieve the document interface to the markup container.
						hr = pMarkupContainer->QueryInterface(IID_PPV_ARGS(&pHtmlDoc));
						if (SUCCEEDED(hr) && pHtmlDoc) {
							// Invoke the user-defined action for this new fragment.
							hr = pCallback(pHtmlDoc, message);

							// Clean up.
							pHtmlDoc->Release();
						}
						pMarkupContainer->Release();
					}
					pMarkupEnd->Release();
				}
				if (pMarkupBegin)
					pMarkupBegin->Release();
				pMarkupServices->Release();
			}
			pPersistStreamInit->Release();
		}
		pHtmlDocRoot->Release();
	}
	return hr;
}
Example #3
0
BOOL SimpleBrowser::GetElementText(LPCTSTR elementName, LPTSTR bf, UINT bfLen)
{
	IHTMLDocument3* pDocument = GetDocument3();
	if (pDocument == NULL)
		return FALSE;
	BOOL bRet = FALSE;
	IHTMLElement* pElement = NULL;
	CComBSTR elemName = elementName;
	if (pDocument->getElementById(elemName, &pElement) == S_OK && pElement != NULL)
	{
		CComBSTR innerText;
		if (pElement->get_innerText(&innerText) == S_OK)
		{
			if (innerText.Length() > 0)
			{
				_tcsncpy(bf, innerText, bfLen);
				bf[bfLen - 1] = 0;
			}
			else
				bf[0] = 0;
			bRet = TRUE;
		}
		pElement->Release();
	}
	pDocument->Release();
	return bRet;

}
Example #4
0
void CHtmlViewer::OnDocumentCompleted()
{
    // disconnect / remove old documents events
    if (this->pDocumentElementEvents)
        delete this->pDocumentElementEvents;

    // attach new documents events
    HRESULT hr;
    IDispatch* pHtmlDocDispatch = NULL;
    IHTMLDocument3* pHtmlDoc = NULL;
    // Retrieve the document object.

    hr = pIWebBrowser->get_Document(&pHtmlDocDispatch);
    if (SUCCEEDED (hr) && (pHtmlDocDispatch != NULL))
    {
        hr = pHtmlDocDispatch->QueryInterface(IID_IHTMLDocument3,(void**)&pHtmlDoc);
        if (SUCCEEDED (hr) && (pHtmlDoc != NULL))
        {

            IHTMLElement* pElem = NULL;
            pHtmlDoc->get_documentElement(&pElem);

            this->pDocumentElementEvents=new CElementEvents(pElem);
            this->pDocumentElementEvents->ConnectEvents();

            this->pDocumentElementEvents->SetElementEventsCallBack(this->ElementEventsCallBack,this->ElementEventsCallBackUserParam);
            this->pDocumentElementEvents->SetElementEventsCallBackEx(this->ElementEventsCallBackEx,this->ElementEventsCallBackExUserParam);

            this->pDocumentElementEvents->EnableSelection(this->bEnableSelection);
            this->pDocumentElementEvents->EnableContextMenu(this->bEnableContextMenu);

            pElem->Release();
            pHtmlDoc->Release();
        }

        pHtmlDocDispatch->Release();
    }
}
Example #5
0
BOOL CHtmlViewer::SaveAs(TCHAR* FileName)
{
    if (this->pIWebBrowser==NULL)
        return FALSE;

    BOOL bSuccess=FALSE;

    // this->pIWebBrowser->ExecWB(OLECMDID_SAVEAS,...) troubles : 
    // don't get updated content 
    // don't update res:// links)

    // this saving way troubles : don't save remote files only res and file protocols supported

    CHtmlViewerSave_FirstItem FirstItem;

    HRESULT hr;
    HANDLE hFile;
    IDispatch* pHtmlDocDispatch = NULL;
    IHTMLDocument3* pHtmlDoc = NULL;
    // Retrieve the document object.

    hr = pIWebBrowser->get_Document(&pHtmlDocDispatch);
    if (SUCCEEDED (hr) && (pHtmlDocDispatch != NULL))
    {
        hr = pHtmlDocDispatch->QueryInterface(IID_IHTMLDocument3,(void**)&pHtmlDoc);
        if (SUCCEEDED (hr) && (pHtmlDoc != NULL))
        {

            IHTMLElement* pElem = NULL;

            hr = pHtmlDoc->get_documentElement(&pElem);
            if (SUCCEEDED (hr) && (pElem != NULL))
            {

                BSTR bstrDocContent;
                pElem->get_innerHTML(&bstrDocContent);
                TCHAR* pDocContent;
                TCHAR* pszCurrent;
                TCHAR* pszResProtocol;
                TCHAR* pszResProtocolEnd;
                TCHAR* pszFileProtocol;
                TCHAR* pszFileProtocolEnd;
                TCHAR* pszDontSaveBegin;
                TCHAR* pszDontSaveEnd;
                TCHAR* pszMin;
                TCHAR* psz;
                TCHAR Link[MAX_PATH];
                TCHAR ChangedLink[MAX_PATH];
    #if (defined(UNICODE)||defined(_UNICODE))
                pDocContent=bstrDocContent;
    #else
                CAnsiUnicodeConvert::UnicodeToAnsi(bstrDocContent,&pDocContent);
    #endif
                if (CTextFile::CreateTextFile(FileName,&hFile))
                {
                    CTextFile::WriteText(hFile,CHtmlViewer_DOCUMENT_HEADER);

                    pszCurrent=pDocContent;

                    for (;;)
                    {
                        // find next dont save begin
                        pszDontSaveBegin=_tcsstr(pszCurrent,CHtmlViewer_DONT_SAVE_BEGIN);

                        // find next file:// protocol
                        pszFileProtocol=_tcsstr(pszCurrent,CHtmlViewer_FILE_PROTOCOL);

                        // find next res:// protocol (like "res://foo.exe/jpg/105")
                        pszResProtocol=_tcsstr(pszCurrent,CHtmlViewer_RES_PROTOCOL);


                        // if no more res::// protocol, file:// protocol or <!--DONT_SAVE_BEGIN-->
                        if ((pszResProtocol==NULL)
                            && (pszFileProtocol==NULL)
                            && (pszDontSaveBegin==NULL)
                            ) 
                        {
                            CTextFile::WriteText(hFile,pszCurrent);
                            break;
                        }

                        // at least one item is not null
                        // get first not null item
                        if (pszDontSaveBegin)
                            pszMin=pszDontSaveBegin;
                        else
                            pszMin=(TCHAR*)-1;
                        FirstItem=CHtmlViewerSave_FirstItem_DontSave;

                        if ((pszFileProtocol<pszMin)
                            && (pszFileProtocol!=0)
                            )
                        {
                            FirstItem=CHtmlViewerSave_FirstItem_File;
                            pszMin=pszFileProtocol;
                        }

                        if ((pszResProtocol<pszMin)
                            && (pszResProtocol!=0)
                            )
                        {
                            FirstItem=CHtmlViewerSave_FirstItem_Res;
                            pszMin=pszResProtocol;
                        }

                        // do treatment for first found type
                        // (use if instead case to allow breaking for(;;). Else we need a goto)
                        if (FirstItem==CHtmlViewerSave_FirstItem_Res)
                        {
                            // write data before res:// protocol
                            CTextFile::WriteText(hFile,pszCurrent,pszResProtocol-pszCurrent+1);

                            // look for the end of res:// protocol
                            pszResProtocolEnd=_tcschr(pszResProtocol+_tcslen(CHtmlViewer_RES_PROTOCOL),'"');
                            if (!pszResProtocolEnd)
                            {
                                // bad html format, output raw content
                                SetFilePointer(hFile,0,0,FILE_BEGIN);
                                CTextFile::WriteText(hFile,CHtmlViewer_DOCUMENT_HEADER);
                                CTextFile::WriteText(hFile,pDocContent);
                                break;
                            }

                            _tcsncpy(Link,pszResProtocol,pszResProtocolEnd-pszResProtocol+1);
                            Link[pszResProtocolEnd-pszResProtocol+1]=0;

                            // change link from resource one to extracted resource one
                            this->ModifyLinkAndExtractResource(FileName,Link,ChangedLink);
                            // write modified link
                            CTextFile::WriteText(hFile,ChangedLink);
                            // end link by adding '"'
                            CTextFile::WriteText(hFile,_T("\""));

                            // pointer after res protocol
                            pszCurrent=pszResProtocolEnd+1;
                        }
                        else if (FirstItem==CHtmlViewerSave_FirstItem_File)
                        {
                            // write data before file:// protocol
                            CTextFile::WriteText(hFile,pszCurrent,pszFileProtocol-pszCurrent+1);

                            // look for the end of file:// protocol
                            pszFileProtocolEnd=_tcschr(pszFileProtocol+_tcslen(CHtmlViewer_FILE_PROTOCOL),'"');
                            if (!pszFileProtocolEnd)
                            {
                                // bad html format, output raw content
                                SetFilePointer(hFile,0,0,FILE_BEGIN);
                                CTextFile::WriteText(hFile,CHtmlViewer_DOCUMENT_HEADER);
                                CTextFile::WriteText(hFile,pDocContent);
                                break;
                            }
                            pszFileProtocol+=_tcslen(CHtmlViewer_FILE_PROTOCOL);
                            _tcsncpy(Link,pszFileProtocol,pszFileProtocolEnd-pszFileProtocol);
                            Link[pszFileProtocolEnd-pszFileProtocol]=0;
                            

                            // get saving filename
                            _tcscpy(ChangedLink,FileName);
                            // remove file extension of saving file name
                            CStdFileOperations::RemoveFileExt(ChangedLink);
                            // add "_files\" to saving file to create a directory like SavingFileName_files
                            _tcscat(ChangedLink,CHtmlViewer_FILES_DIR_EXT);
                            // add the name of file
                            _tcscat(ChangedLink,CStdFileOperations::GetFileName(Link));
                            // create directory if necessary
                            CStdFileOperations::CreateDirectoryForFile(ChangedLink);
                            // copy file to directory
                            CopyFile(Link,ChangedLink,FALSE);
                           
                            // now make a relative link to file
                            _tcscpy(Link,ChangedLink);
                            psz=_tcsrchr(Link,'\\');
                            if (psz)
                            {
                                *psz='/';
                                psz=_tcsrchr(Link,'\\');
                                if (psz)
                                {
                                    _tcscpy(ChangedLink,psz+1);
                                }
                            }

                            // write modified link
                            CTextFile::WriteText(hFile,ChangedLink);
                            // end link by adding '"'
                            CTextFile::WriteText(hFile,_T("\""));

                            // pointer after file protocol
                            pszCurrent=pszFileProtocolEnd+1;
                        }
                        else if (FirstItem==CHtmlViewerSave_FirstItem_DontSave)
                        {
                            // write data before <!--DONT_SAVE_BEGIN-->
                            CTextFile::WriteText(hFile,pszCurrent,pszDontSaveBegin-pszCurrent);

                            pszDontSaveEnd=_tcsstr(pszDontSaveBegin+_tcslen(CHtmlViewer_DONT_SAVE_BEGIN),CHtmlViewer_DONT_SAVE_END);
                            if (!pszDontSaveEnd)
                            {
                                // bad html format, output raw content
                                SetFilePointer(hFile,0,0,FILE_BEGIN);
                                CTextFile::WriteText(hFile,CHtmlViewer_DOCUMENT_HEADER);
                                CTextFile::WriteText(hFile,pDocContent);
                                break;
                            }
                            // point after <!--DONT_SAVE_END-->
                            pszCurrent=pszDontSaveEnd+_tcslen(CHtmlViewer_DONT_SAVE_END);
                        }
                    }
                    CTextFile::WriteText(hFile,CHtmlViewer_DOCUMENT_FOOTER);
                    CloseHandle(hFile);

                    bSuccess=TRUE;
                }
    #if ((!defined(UNICODE))&& (!defined(_UNICODE)))
                // free allocated string for ansi
                free(pDocContent);
    #endif

                SysFreeString(bstrDocContent);

                pElem->Release();
            }
            pHtmlDoc->Release();
        }
        
        pHtmlDocDispatch->Release();
    }
    return bSuccess;
}