简体   繁体   中英

IE extension - Injecting Javascript file

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM