Beispiel #1
0
// This method is called when a document is finished loading.  It will hook the
//   document's createElement() method, passing all dynamically-created elements
//   to the hook as they are created.
//
void JSHook::hookNewPage(MSHTML::IHTMLDocument2Ptr doc) {
	MSHTML::IHTMLWindow2Ptr wnd = doc->parentWindow;

	// Create a temporary function to hook createElement().
	//
	wnd->execScript(
		L"function __drip_replaceCreateElement(jsHook) {"
		L"  var oldCE = document.createElement;"
		L"  document.createElement = function(tag) {"
		L"    var elem = oldCE(tag);"
		L"    jsHook.logElement(elem, document);"
		L"    return elem;"
		L"  };"
		L"}",
		L"javascript");

	// Create a temporary function to hook cloneNode().
	//
	wnd->execScript(
		L"function __drip_replaceCloneNode(jsHook) {"
		L"  var oldCE = document.cloneNode;"
		L"  document.cloneNode = function(tag) {"
		L"    var elem = oldCE(tag);"
		L"    jsHook.logElement(elem, document);"
		L"    return elem;"
		L"  };"
		L"}",
		L"javascript");

	// Create a parameter list containing the hook, then invoke the
	//   temporary function to attach it to the document.
	//
	VARIANT vHook;
	VariantInit(&vHook);
	vHook.vt = VT_DISPATCH;
	vHook.pdispVal = this;
	this->AddRef();

	DISPPARAMS params;
	memset(&params, 0, sizeof(DISPPARAMS));
	params.cArgs = 1;
	params.rgvarg = &vHook;

	CComPtr<IDispatch> scriptObj = doc->Script;

	DISPID dispId;
	OLECHAR *name = SysAllocString(L"__drip_replaceCreateElement");
	scriptObj->GetIDsOfNames(IID_NULL, &name, 1, LOCALE_SYSTEM_DEFAULT, &dispId);
	SysFreeString(name);

	scriptObj->Invoke(dispId, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD, &params, NULL, NULL, NULL);

	name = SysAllocString(L"__drip_replaceCloneNode");
	scriptObj->GetIDsOfNames(IID_NULL, &name, 1, LOCALE_SYSTEM_DEFAULT, &dispId);
	SysFreeString(name);
	scriptObj->Invoke(dispId, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD, &params, NULL, NULL, NULL);

	VariantClear(&vHook);
}
Beispiel #2
0
// Collect all leaked elements, passing them to the specified leak dialog.
//
void JSHook::showLeaks(MSHTML::IHTMLWindow2Ptr wnd, CLeakDlg* dlg) {
	// Ensure that all garbage collection is completed so that elements will
	//   be released.
	//
	wnd->execScript(L"window.CollectGarbage()", L"javascript");

	for (std::map<IUnknown*,Elem>::const_iterator it = m_elements.begin(); it != m_elements.end(); ++it) {
		IUnknown *unk = it->first;
		Elem const& elem = it->second;

		// For each element, AddRef() and Release() it.  The latter method will return
		//   the current ref count.
		//
		unk->AddRef();
		int refCount = unk->Release();

		// If any references (other than the one that we hold) are outstanding, then
		//   the element has been leaked.
		//
		if (refCount > 1)
			dlg->addElement(unk, elem.url, refCount - 1);
	}

	// When finished, clear the element list.
	//
	clearElements();
}
Beispiel #3
0
void CIECanvas::ExecuteJavaScript(const char *javaScript) {
	if (m_spBrowser) {
      try {
         HRESULT hr = CoInitialize(NULL);
         if (FAILED(hr)) {
            printf("CoInitialize Failed: %ld\n",hr);
            return;
         }

	      SHDocVw::IWebBrowser2Ptr browser = m_spBrowser;
	      MSHTML::IHTMLDocument2Ptr document = browser->Document;
         if (document == NULL)
            return;

         MSHTML::IHTMLWindow2Ptr window = document->parentWindow;
         if (window == NULL)
            return;

         window->execScript(javaScript, "javascript");
      } catch (_com_error e) {
         printf("Exception in running Javascript %ld, %s\n", e.Error(), e.ErrorMessage());
      }
    }
}