-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUncheckyBHO.cpp
More file actions
250 lines (213 loc) · 5.04 KB
/
UncheckyBHO.cpp
File metadata and controls
250 lines (213 loc) · 5.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// UncheckyBHO.cpp : Implementation of CUncheckyBHO
#include "stdafx.h"
#include "UncheckyBHO.h"
// CUncheckyBHO
STDMETHODIMP CUncheckyBHO::SetSite(IUnknown* pUnkSite)
{
if(pUnkSite != NULL)
{
// Cache the pointer to IWebBrowser2.
HRESULT hr = pUnkSite->QueryInterface(IID_IWebBrowser2, (void **)&m_spWebBrowser);
if(SUCCEEDED(hr))
{
// Register to sink events from DWebBrowserEvents2.
hr = IDWebBrowserEvents2Impl::DispEventAdvise(m_spWebBrowser);
if(SUCCEEDED(hr))
{
m_fAdvised = TRUE;
m_wndMsg.Create(HWND_MESSAGE, CWindow::rcDefault);
}
}
}
else
{
// Unregister event sink.
if(m_fAdvised)
{
IDWebBrowserEvents2Impl::DispEventUnadvise(m_spWebBrowser);
m_fAdvised = FALSE;
if(m_wndMsg)
m_wndMsg.DestroyWindow();
}
// Release cached pointers and other resources here.
m_spWebBrowser.Release();
// Unregister checkbox.
CheckboxUnadvise();
}
// Return the base class implementation
return IObjectWithSiteImpl<CUncheckyBHO>::SetSite(pUnkSite);
}
void STDMETHODCALLTYPE CUncheckyBHO::OnDocumentComplete(IDispatch *pDisp, VARIANT *pvarURL)
{
HRESULT hr = S_OK;
// Query for the IWebBrowser2 interface.
CComQIPtr<IWebBrowser2> spTempWebBrowser = pDisp;
// Is this event associated with the top-level browser?
if(spTempWebBrowser && m_spWebBrowser &&
m_spWebBrowser.IsEqualObject(spTempWebBrowser))
{
EndTimer();
// Unregister checkbox.
CheckboxUnadvise();
CComBSTR bsUrl;
hr = m_spWebBrowser->get_LocationURL(&bsUrl);
if(SUCCEEDED(hr))
{
// Get the current document object from browser...
CComPtr<IDispatch> spDispDoc;
hr = m_spWebBrowser->get_Document(&spDispDoc);
if(SUCCEEDED(hr))
{
// ...and query for an HTML document.
CComQIPtr<IHTMLDocument2> spHTMLDoc = spDispDoc;
if(spHTMLDoc != NULL)
{
// Finally, handle the page.
if(IsAdobePage(bsUrl, spHTMLDoc))
{
HandleAdobePage(bsUrl, spHTMLDoc);
StartTimer(200);
}
}
}
}
}
}
void STDMETHODCALLTYPE CUncheckyBHO::OnMouseClick(IHTMLEventObj *pEvtObj)
{
EndTimer();
HWND hwnd;
HRESULT hr = m_spWebBrowser->get_HWND((LONG_PTR*)&hwnd);
if(SUCCEEDED(hr))
{
int nRet = MessageBox(hwnd, L"Are you sure?!", L"Unchecky", MB_YESNO);
if(nRet == IDYES)
{
CheckboxUnadvise();
}
else
{
StartTimer(200);
}
}
}
LRESULT CUncheckyBHO::OnTimer(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
{
HRESULT hr;
if(m_spWebBrowser)
{
CComBSTR bsUrl;
hr = m_spWebBrowser->get_LocationURL(&bsUrl);
if(SUCCEEDED(hr))
{
// Get the current document object from browser...
CComPtr<IDispatch> spDispDoc;
hr = m_spWebBrowser->get_Document(&spDispDoc);
if(SUCCEEDED(hr))
{
// ...and query for an HTML document.
CComQIPtr<IHTMLDocument2> spHTMLDoc = spDispDoc;
if(spHTMLDoc != NULL)
{
// Finally, handle the page.
HandleAdobePage(bsUrl, spHTMLDoc);
}
}
}
}
return 0;
}
void CUncheckyBHO::StartTimer(UINT nElapse)
{
if(m_wndMsg && !m_bTimerRunning)
{
m_wndMsg.SetTimer(1, nElapse);
m_bTimerRunning = TRUE;
}
}
void CUncheckyBHO::EndTimer()
{
if(m_wndMsg && m_bTimerRunning)
{
m_wndMsg.KillTimer(1);
m_bTimerRunning = FALSE;
}
}
void CUncheckyBHO::CheckboxAdvise(IHTMLElement *elementCheckbox)
{
if(!m_fCheckboxAdvised)
{
HRESULT hr = IHTMLInputTextElementEvents2Impl::DispEventAdvise(elementCheckbox);
if(SUCCEEDED(hr))
{
ATLASSERT(!m_spAdvisedCheckbox);
m_spAdvisedCheckbox = elementCheckbox;
m_fCheckboxAdvised = TRUE;
}
else
ATLASSERT(0);
}
}
void CUncheckyBHO::CheckboxUnadvise()
{
if(m_fCheckboxAdvised)
{
ATLASSERT(m_spAdvisedCheckbox);
HRESULT hr = IHTMLInputTextElementEvents2Impl::DispEventUnadvise(m_spAdvisedCheckbox);
if(SUCCEEDED(hr))
{
m_spAdvisedCheckbox.Release();
m_fCheckboxAdvised = FALSE;
}
else
ATLASSERT(0);
}
}
bool CUncheckyBHO::IsAdobePage(BSTR bsUrl, IHTMLDocument2 *pDocument)
{
WCHAR *p = bsUrl;
if(_wcsnicmp(p, L"http://", sizeof("http://") - 1) == 0)
{
p += sizeof("http://") - 1;
}
else if(_wcsnicmp(p, L"https://", sizeof("https://") - 1) == 0)
{
p += sizeof("https://") - 1;
}
else
return false;
if(_wcsnicmp(p, L"get.adobe.com/", sizeof("get.adobe.com/") - 1) == 0)
{
p += sizeof("get.adobe.com") - 1;
}
else
return false;
if(!wcsstr(p, L"/flashplayer/") && !wcsstr(p, L"/reader/"))
return false;
return true;
}
void CUncheckyBHO::HandleAdobePage(BSTR bsUrl, IHTMLDocument2 *pDocument)
{
HRESULT hr;
CComQIPtr<IHTMLDocument3> document3 = pDocument;
ATLENSURE_RETURN_VAL(document3, );
CComPtr<IHTMLElement> elementCheckbox;
hr = document3->getElementById(CComBSTR(L"offerCheckbox"), &elementCheckbox);
ATLENSURE_RETURN_VAL(SUCCEEDED(hr), );
if(!elementCheckbox)
{
return;
}
CComQIPtr<IHTMLInputElement> inputElementCheckbox = elementCheckbox;
ATLENSURE_RETURN_VAL(inputElementCheckbox, );
VARIANT_BOOL isChecked;
hr = inputElementCheckbox->get_checked(&isChecked);
ATLENSURE_RETURN_VAL(SUCCEEDED(hr), );
if(!isChecked)
{
return;
}
CheckboxUnadvise();
elementCheckbox->click();
CheckboxAdvise(elementCheckbox);
}