Extensión IE - Inyectando archivo Javascript

I am developing an IE extension which works on sites opened in Internet Explorer. It is designed to work the same way as a chrome extension. I am trying to implement the Background function of chrome extension using c++ and the content script by injecting JS into the current web page. The content script, I am trying to load via IHTMLWindow2 execScript on Document load event. Now that I need to inject JS files directly I tried the following.

Had the JS file under a folder inside the Project destination and tried to inject using physical path.

std::wstring filePath(_T("d:/xx/xxx/x/x/Content/myFile.js"));
scriptText = scriptText+ filePath + endScript;
VARIANT vrt = {0};
HRESULT hrexec = ifWnd->execScript(SysAllocString(scriptText.c_str()),L"javascript", &vrt);

The scriptText has some javascript code to create script element with type and src attributes. The filePath holds the physical path towards the js file.[Also tried relative path but it was a no go]

The above was not working correctly in IE9 due to mixed content issue, upon which I researched to figure out that IE9 expects the js file to be retrieved from a server rather than local physical path. The console throws me the below exception.

SEC7111: HTTPS security is compromised by file:<filepath>
SCRIPT16388: Operation aborted

I am pretty much not sure is there any round about for injecting Javascript to the current DOM from the physical path. Please help me on this.

Also let me know is there any other possibility of injecting the JS file from the current working directory into the DOM.

preguntado el 04 de marzo de 14 a las 11:03

al execScript method I know, is the COM method from the IHTMLWindow2 interface, and that method takes a string containing some JavaScript, not a JavaScript file name. -

mauell, I m trying to do the same, writing a script containing javascript. 'scriptText' would contain javascript content such as creating a head element and creating a script element and i m appending the src for the script element in the 'filePath' and the later script in 'endScript'. -

Let me know if you managed to solve your problem with my answer. -

But what i actually wanted is to insert a file rather than using the script directly -

Lo que tú deseado is one thing, and what IE will let you do is another. Don't forget to notify me if, some day, you find a way of bypassing security restrictions. Anyway, I don't understand why you don't just read the file yourself and inject it as I explained in my answer. -

1 Respuestas

You don't have to inject a <SCRIPT> tag in the DOM.

If your js file contains:

var strHello = "Hello";
function SayHello() { alert( strHello ); }

you may just read the file into memory, construct a BSTR string with it, and pass that string to IHTMLWindow2::execScript.

Later, another call to execScript with the string SayHello(); will popup the alert box. The code you injected is still here.

respondido 07 mar '14, 11:03

My scripting lines extend to 100 lines :) I feel having all those script lines written inside execScript would not look good. So I'm using a file to hold the scripts and then inject using <script> tag. - naddy

There is NO PROBLEM in passing to execScript a BSTR containing hundreds of lines of code. I do it with more than 1K lines. Anyway, internally, the JS files are read and their content ends up injected in a similar way... Try it, it will solve your problem. - manuell

But what i actually wanted is to insert a file rather than using the script directly - naddy

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.