Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Overloading Script Engine to Hook eval Function

36 views
Skip to first unread message

sars...@yahoo.com

unread,
Jun 2, 2009, 1:43:36 PM6/2/09
to
Hi Everyone,

What i Want to Do:
---------------------------
I want to hook JS eval function but not sure if its possible. I have
been looking around and got some clues but so far no success. I have
run into issues discussed later in the post.


When i say hook, it means whenever a URL is retrieved and eval
function is executed as part of that page then every call to eval
comes to my wrapper function written in C++ first. From my wrapper
function i will call the original eval function after doing some of
my
own things.


<script type="text/javascript">


eval("x=10;y=20;document.write(x*y)");//At this call, control should
come to


my wrapper C++ function from where i will be calling the original
eval.


</script>


What i Have Done So Far:
------------------------
Since i was not sure if its possible therefore, to keep things simple
i started with trying to hook a function (func1) defined in <Script>
tag of an a web page.


<html>
<title>test</title>


<script>
function func1(msg)
{
alert(msg);


}


</script>

<body>
<button onclick=func1('hello')>button1</button>
</body>


</html>


The work that i have done so far is based on the following two links.
They were quite helpful.


http://www.tech-archive.net/Archive/InetSDK/microsoft.public.inetsdk....


http://msdn.microsoft.com/en-us/library/sky96ah7(VS.85).aspx


Source Code (for hooking func1):
--------------------------------
I have omitted the obvious code and error checks.


In the Invoke function at DISPID_DOCUMENTCOMPLETE event:


//Created my own object where CMyCOMObj is inherited from IDispatch
CComObject<CMyCOMObj>* pObj;
CComObject<CMyCOMObj>::CreateInstance(&pObj);


BSTR name = SysAllocString(OLESTR("func1"));


//Get dispatch pointer to global script object


pDisp->QueryInterface(IID_IHTMLDocument2, (void **) &pHTMLDocument2);
pHTMLDocument2->get_Script(&pDisp2);


//Get dispatchEx so that GetDispID can be called
pDisp2->QueryInterface(IID_IDispatchEx, (void **) &pDispEx);


//Get disp ID of func1
hr = pDispEx->GetDispID(name, 0, &dispID);


//Here, retrieve and save original address of function func1 first.
hr = pDispEx->InvokeEx(dispID, LOCALE_USER_DEFAULT,
DISPATCH_PROPERTYGET,
&dispparamsNoArgs, &var, NULL, NULL);


//Dispatch pointer for func1 saved.
pDispFunc1 = var.pdispVal;


// Assign CMyCOMObj pointer.
putid = DISPID_PROPERTYPUT;
var.pdispVal = pObj;//Pointer to my object placed so that when the
original


func1 gets called then control first arrives at my function.
dispparams.rgvarg = &var;
dispparams.rgdispidNamedArgs = &putid;
dispparams.cArgs = 1;
dispparams.cNamedArgs = 1;


//Now call InvokeEx on func1 disp id with DISPATCH_PROPERTYPUTREF.
Here doing so was suppose to install the hook so that whenever eval
is
called, control arrives at my C++ function but it does not. No error
here though.
hr = pDispEx->InvokeEx(dispID, LOCALE_USER_DEFAULT,
DISPATCH_PROPERTYPUTREF,


&dispparams, NULL, NULL, NULL);


Now after when the URL is loaded and above logic is executed,
clicking
the button does not show any alert. Instead, there is an error in the
page which says Library not Registered.


A word about CMyCOMObj:
-----------------------
This class inherits from IDispatch and has only one function which is
suppose to be the wrapper function. According to post (first URL
above) "To override a function, you have to provide your own function
object,
which is, again, a COM object implemeting one method with a dispid of
DISPID_VALUE."


I dont know how to set its dispid as DISPID_VALUE.


How Browser is Being Accessed:
------------------------------
I am not hosting a browser control in my application. I create a
browser instance using CoCreateInstance. I have an event sink
implemented and can receive events such as DISPID_BEFORENAVIGATE2
etc.
From my application i can access dispatch pointers and retrieve Disp
IDs of eval, fucn1 etc.


So my primary questions are:


1- Is it possible to hook eval function so that for every call of
eval, control first arrives at my wrapper C++ function? If yes then i
would be grateful for any help, sample code, pointers.


2- What am i doing wrong in the above code (Source Code (for hooking
func1):) where i cant event get to hook a function defined in the JS
tag?


I am new to COM so may be i am making a fundamental mistake here. Any
help would be highly appreciated.


Thanks,
Sarshah.


0 new messages