Fernando Aramburu

Hi everybody,

I'm building a BHO in .NET and i'm testing with IE7 and Win XP Professional. My intention is to do something that is really simple to explain but I'm having lot of troubles at building time. I need to on DocumentComplete event:
1) add some javascript script s to the current document
2) after step 1 is complete call a simple Javascript function located in one of the js file I added before
3) This script just adds a little style to some words on any document loaded into IE window.

Step 3 is done with no problems but I'm having problems with the first to steps.
I tried by different ways (document.parentWindow.execScript, document.body.insertAdjacentHTML for example) but in every way I tested when I add a DIV everything works ok but I tried to add a script then nothing happens.

Any ideas Any help

I give you an example so you can understand better... Imagine I have a script located in
http://myAddress/myScript1.js
that contains
function helloWorldFromJS() { whatever code }

then I need to add to all documents loaded into IE the next two things:
<script language='javascript' src='http://myAddress/myScript1.js'> </script>
and
<script language='javascript' > helloWorldFromJS(); </script>

I can't add both two things.... I will really appreciate your help.

Regards.

Fernando Aramburu



Re: Internet Explorer Extension Development Injecting Script tag into current document

Appxweb

Hello Fernando,

From my experience of trying to do the same thing, directly injecting script tags/code into a web page is not possible. You can achieve the same functionality though using a different approach which I think is what programs like GreaseMonkey/Trixie use. Here are the steps.

Step 1 - Within the your BHO create your script host.

Step 2 - Get hold of current IDispatch interface of IE.

Step 3 - Add IE as a named item to your script host

Step 4- Run your particular script within your own script host, manipulating the IE DOM using the IE named item you previously added.

This sounds simple but is very difficult to achieve (well for me at least). To see this approach in action have a look at the second part of the "Scripting and HTML Extensions" code sample at my website here http://www.appxweb.com/developer/developer_samples.html.This code sample uses a standard GreaseMonkey script to add a noframe link to images in Google Images search. Please read the "Getting Started Guide" first (should take 5 mins) - otherwise you could waste a lot of time.

Kind Regards

Ian Hart

www.appxweb.com






Re: Internet Explorer Extension Development Injecting Script tag into current document

Fernando Aramburu

Ian,

Thanks a lot for the answer but I can't access to your website. I guess it's down because i'm a getting a time out error.

Hope you can tell me if I can access this information in another way or please feel free to send all information to fernaramburu at hotmail

Thanks a lot again, I'm really surprised about this unbelievable restrictions of IE.. I build the same idea in Firefox in just 2 days, and with IE it takes me 5 days I'm still fighting with this and... I'm a .NET developer so it should be easier but ....

Hope to hear from you soon.

Fernando





Re: Internet Explorer Extension Development Injecting Script tag into current document

IECUSTOMIZER

Hi Fernando,

Alternatively I think you can use the Tags collection to add a new script tag on the document complete event. I am not on my regular machine so I cannot refer to my samples, to be sure of my advice.

Regards.






Re: Internet Explorer Extension Development Injecting Script tag into current document

Appxweb

Hi Fernando and IECustomizer

Fernando the website is up. With regards to IECustomizer's suggestion I am able to easily add new script tags/code to the DOM but I was not able to make them execute. Any pointers on making this work would really be helpful as it would simplify a number of things for me.

Kind Regards

Ian






Re: Internet Explorer Extension Development Injecting Script tag into current document

Fernando Aramburu

Hi IECustomizer,

thanks a lot for your answer and also for being a great answerer here ni this forum .

I tried to do what you say but javacsript functions don't execute. I'm not only trying to inject JS but also to run those script but a line like this
<script language='javascript'> InjectedFunction(); </script>

as the last line before the end of body so everything I inject its executed.

Do you say this is posible Do you have any example (.NET preferably but any language is also great Smile ) to send me to fernaramburu at hotmail dot com.

Thanks a lot for your answer.

Fernando





Re: Internet Explorer Extension Development Injecting Script tag into current document

Fernando Aramburu

Ian,

Thanks a lot for your answer but I still can't access to your website. I don't know what's going on but I try to enter from machines located in Argentina , Mexico and Spain and I can't do it. Can you send me those files by email or by chat. My msn is fernaramburu at hotmail dot com.

Thanks a lot again and I'm getting the same problem than me with regards to IECustomizer's suggestion. Maybe he knows how to do it Big Smile (I'm praying for that Smile )

Regards.

Fernando







Re: Internet Explorer Extension Development Injecting Script tag into current document

IECUSTOMIZER

Hi Fernando,

Once again I am answering from memory, having not prototyped or tested what I am suggesting.

First up you may like to get yourself a copy of either DebugBar http://www.debugbar.com or IEWatch http://www.iewatch.com

The debug bar has a useful utility to execute script in the currently loaded document.... you may get some ideas how this is done by investigating the workings of this addon.( I suspect it may be the external.execscript method)

... Geting a script tag to load its src

Give your injected script tag an id value and add it to the document. You may have to inject this within the <head> tags (I am unsure).

eg. <script id="myscriptfile" language="javascript"></script>

Use the DOM to set the src value of the tag that was just added (javascript shown here, but you should be able to convert this to the lanuage used in your BHO, just add a reference to the MSHTML library... mshtml.dll to expose the DOM to your BHO)

document.getElementById('myscriptfile').src = 'path to your local script file.js';

or if you want to insert functions etc

document.getElementById('myscriptfile').innerHTML = 'function SayHello(){alert(/"hello world/");}';

(be carefull of how you use the " and ' characters in your script)

This should force IE to load your script file. Any subsequent script tags that refer to functions in your script source should then be able to execute.

You should find that the debug bar will be very useful in debugging your scripts.... I highly recommend it.

Other useful tools for bho and toolbar development are

Spy++ (you will find this in your VS/Tools folder)

and

Debugging Tools for Windows (download from http://www.microsoft.com/whdc/devtools/debugging/default.mspx)

Last up...

Do no evil...

BTW... the method of assigning an id value to a tag is very usefull.... see the <base> and <bgsound> tags..

Regards.






Re: Internet Explorer Extension Development Injecting Script tag into current document

Fernando Aramburu

Dear IECustomizer,

I just tried something I am writing here...

I insert this script
<script language='javascript'>
function hello()
{
window.alert('hello function');
}
</script>

When I try to insert this one by using ExecuteScript function
string strCode = "function callHello() { hello(); } callHello(); ";
document.parentWindow.execScript(strCode, "javascript");

callHello function is executed but inside callHello, when I call hello() function I get a "not found" error like if function hello of the previous script was never inserted. Do you understand Do you know what's going on here

I will try later all this things you are telling me. I just have words to thank you Smile

Regards.

Fernando Aramburu





Re: Internet Explorer Extension Development Injecting Script tag into current document

IECUSTOMIZER

Hi Fernando,

There are security considerations with IE6/7 SP2.... You may have to use the MTW (mark of the web), but that should result in an 'access denied' error.

My recommendation is to download and install the debugbar.... let your bho insert the script and then use the debug bars scripting console to execute the inserted functions.

It also sounds like you dont have MS script debugging installed

http://www.microsoft.com/downloads/details.aspx familyid=2f465be0-94fd-4569-b3c4-dffdf19ccd99&displaylang=en

I am sorry that I cannot be more specific, as it would take me considerable time to set up a workbench to give you definitive answers. I trust that I can provide you with knowledgable hints as to which path you should follow.

Regards.






Re: Internet Explorer Extension Development Injecting Script tag into current document

Fernando Aramburu

IECustomizer,

Again, lot of thanks for your time. Big Smile

I have everything installed (VS.NET Orcas version also support Script debugging) anyway thanks for the link.

I don't understand that "security considerations with IE" that you mention IE has, MS will have change some things I guess. Firefox let you do whatever you want.

Don't worry about being specific. You have been a great great help for me.

Fernando