pub.cpp
来自「一个在IExplorer中运行」· C++ 代码 · 共 1,736 行 · 第 1/4 页
CPP
1,736 行
{
pDocHandler->m_ptimeClick = &m_timeClick;
CComQIPtr<IDispatch> spDocHandlerDisp(pDocHandler);
ATLASSERT(spDocHandlerDisp);
ATLASSERT(pConnInfo->m_dwDocumentCookie == 0);
dwCookie = 0;
hr = AtlAdvise(spDoc, spDocHandlerDisp, DIID_HTMLDocumentEvents2, &dwCookie);
ATLASSERT(SUCCEEDED(hr));
if (SUCCEEDED(hr))
pConnInfo->m_dwDocumentCookie = dwCookie;
}
CComObject<CWindowEventHandler>* pWndHandler = NULL;
hr = CComObject<CWindowEventHandler>::CreateInstance (&pWndHandler);
ATLASSERT(SUCCEEDED(hr));
if (SUCCEEDED(hr) && pWndHandler)
{
pWndHandler->m_pbUnloading = &m_bUnloading;
CComQIPtr<IDispatch> spWndHandlerDisp(pWndHandler);
ATLASSERT(spWndHandlerDisp);
ATLASSERT(pConnInfo->m_dwWindowCookie == 0);
dwCookie = 0;
hr = AtlAdvise(spWnd, spWndHandlerDisp, DIID_HTMLWindowEvents2, &dwCookie);
ATLASSERT(SUCCEEDED(hr));
if (SUCCEEDED(hr))
pConnInfo->m_dwWindowCookie = dwCookie;
}
}
else
{
//ATLASSERT(pConnInfo->m_dwWindowCookie != 0);
if(pConnInfo->m_dwWindowCookie != 0)
hr = AtlUnadvise(spWnd, DIID_HTMLWindowEvents2, pConnInfo->m_dwWindowCookie);
//ATLASSERT(SUCCEEDED(hr) || hr == CONNECT_E_NOCONNECTION);
pConnInfo->m_dwWindowCookie = 0;
//ATLASSERT(pConnInfo->m_dwDocumentCookie != 0);
if(pConnInfo->m_dwDocumentCookie != 0)
hr = AtlUnadvise(spDoc, DIID_HTMLDocumentEvents2, pConnInfo->m_dwDocumentCookie);
//ATLASSERT(SUCCEEDED(hr));
pConnInfo->m_dwDocumentCookie = 0;
}
}
}
}
}
//ATLASSERT(SUCCEEDED(hr));
return hr;
}
//
// Load persisted settings
//
void CPub::LoadRegistrySettings()
{
if (g_bRegistrySettingsLoaded)
return;
g_bRegistrySettingsLoaded = TRUE;
CRegKey rk;
// Changed from HKEY_LOCAL_MACHINE to HKEY_CURRENT_USER. Thanks to Kiliman!
DWORD dwErr = rk.Open(HKEY_CURRENT_USER, g_sCoRegKey + g_sPubRegKey);
if (dwErr == ERROR_SUCCESS)
{
DWORD dwVal;
DWORD dwErr = rk.QueryDWORDValue(_T("Enable"), dwVal);
if (dwErr == ERROR_SUCCESS)
g_bEnabled = (BOOL)dwVal;
dwErr = rk.QueryDWORDValue(_T("BlockFlash"), dwVal);
if (dwErr == ERROR_SUCCESS)
g_bBlockFlash = (BOOL)dwVal;
}
else if (dwErr == ERROR_FILE_NOT_FOUND)
{
dwErr = rk.Create(HKEY_CURRENT_USER, g_sCoRegKey);
if (dwErr == ERROR_SUCCESS)
{
rk.Close();
dwErr = rk.Create(HKEY_CURRENT_USER, g_sCoRegKey + g_sPubRegKey);
if (dwErr == ERROR_SUCCESS)
{
rk.SetDWORDValue(_T("Enable"), g_bEnabled);
}
else
{
AtlMessageBox(NULL, IDS_ERR_CREATEREGKEY, IDS_PROJNAME);
}
}
}
else
{
AtlMessageBox(NULL, IDS_ERR_LOADSETTINGS, IDS_PROJNAME);
}
}
//
// If it does not already exist, copy the white list template to the
// users personal directory.
//
//
// Enable/disable the popup blocker and save the setting.
//
void CPub::EnablePopupBlocker()
{
g_bEnabled = !g_bEnabled;
try
{
CRegKey rk;
DWORD dwErr = rk.Open(HKEY_CURRENT_USER, g_sCoRegKey + g_sPubRegKey);
if (dwErr == ERROR_SUCCESS)
rk.SetDWORDValue(_T("Enable"), g_bEnabled);
}
catch(...)
{
}
}
void CPub::NotifiyUser()
{
static int blocked = 0;
// Show message in status bar
if (g_bStatusMsg && m_pHTMLWindow)
{
blocked++;
// IE shows default status when there is nothing better to do.
CComBSTR bsPopupsWarning = L"Blocked %d popup windows!";
swprintf(bsPopupsWarning.m_str,L"自动拦截 %d个弹出窗口!",blocked);
m_pHTMLWindow->put_defaultStatus(bsPopupsWarning);
}
}
void CPub::UpdateStats(BSTR bsUrl)
{
InterlockedIncrement((LPLONG)&g_dwBlockedSession);
InterlockedIncrement((LPLONG)&g_dwBlockedTotal);
if (WAIT_OBJECT_0 != WaitForSingleObject(m_hMutex, 1000))
return;
try
{
CRegKey rk;
DWORD dwErr = rk.Open(HKEY_CURRENT_USER, g_sCoRegKey + g_sPubRegKey);
if (dwErr == ERROR_SUCCESS)
rk.SetDWORDValue(_T("BlockedTotal"), g_dwBlockedTotal);
}
catch(...)
{
}
ReleaseMutex(m_hMutex);
}
//
// A local function to hide a DOM element using
// inline CSS.
//
HRESULT HideElement(IDispatch* pElemDisp)
{
HRESULT hr = E_FAIL;
CComQIPtr<IHTMLElement, &IID_IHTMLElement>
spElem(pElemDisp);
if (spElem)
{
CComBSTR bs = L"hidden";
CComPtr<IHTMLStyle> spStyle;
hr = spElem->get_style(&spStyle);
if (SUCCEEDED(hr) && spStyle)
{
spStyle->put_visibility(bs);
spStyle->put_pixelHeight(0);
spStyle->put_pixelWidth(0);
}
}
return hr;
}
HRESULT GetElementCollection(LPCOLESTR pszTagName,
IDispatch* pDocDisp,
IHTMLElementCollection** ppColl)
{
HRESULT hr = E_FAIL;
CComQIPtr<IHTMLDocument2, &IID_IHTMLDocument2> spHTML(pDocDisp);
if (spHTML)
{
CComPtr<IHTMLElementCollection> spAll;
hr = spHTML->get_all(&spAll);
if (SUCCEEDED(hr) && spAll)
{
// Find all the sepecified tags in the
// (maybe partially loaded) document
CComVariant vTagName = pszTagName;
vTagName.ChangeType(VT_BSTR);
CComPtr<IDispatch> spTagsDisp;
hr = spAll->tags(vTagName, &spTagsDisp);
if (SUCCEEDED(hr) && spTagsDisp)
{
CComQIPtr<IHTMLElementCollection, &IID_IHTMLElementCollection>
spElems(spTagsDisp);
if (spElems)
hr = spElems.CopyTo(ppColl);
else
hr = E_FAIL;
}
}
}
return hr;
}
//
// A local function to block objects with
// the specified class ID.
//
HRESULT BlockObject(IDispatch* pDocDisp, LPCOLESTR pszClassID)
{
CComBSTR bsClsidToBlock = L"CLSID:";
bsClsidToBlock += pszClassID;
bsClsidToBlock.ToUpper();
CComPtr<IHTMLElementCollection> spElems;
HRESULT hr = GetElementCollection(L"OBJECT", pDocDisp, &spElems);
if (SUCCEEDED(hr) && spElems)
{
long nCnt;
hr = spElems->get_length(&nCnt);
if (SUCCEEDED(hr))
{
for (long i = 0; i < nCnt; i++)
{
CComVariant varIdx;
varIdx = i;
varIdx.ChangeType(VT_I4);
CComPtr<IDispatch> spItemDisp;
hr = spElems->item(varIdx, varIdx, &spItemDisp);
if (SUCCEEDED(hr) && spItemDisp)
{
CComQIPtr<IHTMLObjectElement,
&IID_IHTMLObjectElement> spObject(spItemDisp);
if (spObject)
{
CComBSTR bsObjClassID;
hr = spObject->get_classid(&bsObjClassID);
if (SUCCEEDED(hr) && bsObjClassID)
{
bsObjClassID.ToUpper();
if (bsObjClassID == bsClsidToBlock)
{
// This is a blocked activex control.
// Remove it from view.
hr = HideElement(spItemDisp);
ATLTRACE(_T("Blocked object (%s)\n"),
(LPCTSTR)CW2T(bsObjClassID.m_str));
}
}
}
}
}
}
}
return hr;
}
//
// A local function to block embedded objects with
// the specified file extension.
//
HRESULT BlockEmbed(IDispatch* pDocDisp, LPCOLESTR pszFileExt)
{
CComBSTR bsFileExtToBlock = pszFileExt;
bsFileExtToBlock.ToUpper();
CComPtr<IHTMLElementCollection> spElems;
HRESULT hr = GetElementCollection(L"EMBED", pDocDisp, &spElems);
if (SUCCEEDED(hr) && spElems)
{
long nCnt;
hr = spElems->get_length(&nCnt);
if (SUCCEEDED(hr))
{
for (long i = 0; i < nCnt; i++)
{
CComVariant varIdx;
V_VT(&varIdx) = VT_I4;
V_I4(&varIdx) = i;
CComPtr<IDispatch> spItemDisp;
hr = spElems->item(varIdx, varIdx, &spItemDisp);
if (SUCCEEDED(hr) && spItemDisp)
{
CComQIPtr<IHTMLEmbedElement, &IID_IHTMLEmbedElement>
spObject(spItemDisp);
if (spObject)
{
CComBSTR bsSrc;
hr = spObject->get_src(&bsSrc);
if (SUCCEEDED(hr) && bsSrc)
{
bsSrc.ToUpper();
CComBSTR bsFileExt = wcsrchr(bsSrc, L'.');
if (bsFileExt == bsFileExtToBlock)
{
// This is a blocked embedded object.
// Remove it from view.
HideElement(spItemDisp);
ATLTRACE(_T("Blocked embed (%s)\n"),
(LPCTSTR)CW2T(bsSrc.m_str));
}
}
}
}
}
}
}
return hr;
}
BOOL IsAnimatedImage(CComBSTR& bsUrl)
{
/*
CComBSTR bsTmp = bsUrl;
bsTmp.ToUpper();
CComBSTR bsFileExt = wcsrchr(bsTmp, L'.');
if (bsFileExt != L".GIF")
return FALSE;
*/
BOOL bAnimated = FALSE;
DWORD dwSize = sizeof(INTERNET_CACHE_ENTRY_INFOW);
BYTE* pbyBuf = NULL;
wchar_t szFilename[4096];
BOOL bFileIsCached = FALSE;
do
{
delete [] pbyBuf;
pbyBuf = new BYTE[dwSize];
if (!pbyBuf)
break;
memset(pbyBuf, 0, dwSize);
LPINTERNET_CACHE_ENTRY_INFOW pInfo = (LPINTERNET_CACHE_ENTRY_INFOW)pbyBuf;
pInfo->dwStructSize = dwSize;
BOOL bOK = ::GetUrlCacheEntryInfoW(bsUrl.m_str, pInfo, &dwSize);
if (bOK && pInfo->lpszLocalFileName)
{
wcscpy(szFilename, (const wchar_t*)pInfo->lpszLocalFileName);
bFileIsCached = TRUE;
break;
}
}
while (GetLastError() == ERROR_INSUFFICIENT_BUFFER);
delete [] pbyBuf;
if (!bFileIsCached && g_bBlockUncachedImages)
{
HRESULT hr = ::URLDownloadToCacheFileW(NULL, bsUrl.m_str,
szFilename, sizeof(szFilename), 0, NULL);
if (SUCCEEDED(hr))
bFileIsCached = TRUE;
}
if (bFileIsCached)
{
Gdiplus::Image img(szFilename, FALSE);
UINT nCnt = img.GetFrameDimensionsCount();
if (nCnt > 0)
{
GUID* pGUID = new GUID[nCnt];
Gdiplus::Status s = img.GetFrameDimensionsList(pGUID, nCnt);
if (s == Gdiplus::Ok)
{
for (UINT i = 0; i < nCnt; i++)
{
bAnimated = (img.GetFrameCount(pGUID + i) > 1);
if (bAnimated)
break;
}
}
delete [] pGUID;
}
}
return bAnimated;
}
void HideImgElement(const CComBSTR& bsUrl, IHTMLImgElement * spImage)
{
ATLASSERT(spImage);
long w, h;
spImage->get_width(&w);
spImage->get_height(&h);
if (w >= 16 && h >= 16)
{
CComQIPtr<IHTMLElement, &IID_IHTMLElement> spElem(spImage);
if (spElem)
{
CComPtr<IHTMLStyle> spStyle;
HRESULT hr = spElem->get_style(&spStyle);
if (SUCCEEDED(hr) && spStyle)
{
spStyle->put_visibility(L"hidden");
spStyle->put_pixelHeight(0);
spStyle->put_pixelWidth(0);
ATLTRACE(_T("Blocked animated GIF (%S)\n"), bsUrl.m_str);
}
}
}
}
//
// A local function to block animated GIF files.
// Contributed by Ferdinand Oeinck.
//
HRESULT BlockAnimatedImages(IDispatch* pDocDisp)
{
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?