I’m trying to port an extension from Firefox to IE. All the extension’s code is in JS (no C++ components), so I figured I would reuse the code and only trouble myself with the mozilla specific parts of it (which is no doubt a torture too, but I think it’s less painful than rewriting it from the scratch).
The FF extension is wrapped in a JS object that gets initialized during the “load” event. The “business” code of the extension is then fired on “DOMContentLoaded” event.
My original plan was to create a WSC (Window Scripting Component) and listen for onLoad event there and basically do the same thing that the original code does. However, I found out there’s no implicit global window object in my JavaScript scripting component,
My plan B was to listen for the “onBeforeNavigate2” event in my C++ BHO code and call the JS WSC code that handles the extension object initialization there. So I decided to pass the browser window object as a “window” parameter to the onBeforeNavigate JS function and rewrite the original extension code to be using the explicit “window” variable instead of relying on the implicit global object.
However, when I handle the “OnDocumentComplete” event (once again listening for it in the BHO and passing the browser window object as an argument to a JS function in the WSC), I get a “Can’t execute code from a freed script” error.
The WSC code looks like this:
<?xml version="1.0"?>
<component>
<registration
...
>
</registration>
<public>
<method name="OnBeforeNavigate">
<parameter name="win" />
</method>
<method name="OnDocumentComplete">
<parameter name="win" />
</method>
</public>
<script src="sharedjs/foo.js" />
<script language="JScript">
<![CDATA[
var window;
function OnBeforeNavigate(win)
{
window = win.document.parentWindow.top;
window.myNS.extObject = initExtensionObject(...);
}
function OnDocumentComplete(win)
{
window = win.document.parentWindow.top;
var obj = window.myNS.extObject;
obj.doTheBusinessStuff();
}
]]>
</script>
</component>
I assume that’s because the WSC is unloaded after the handler function returns. However, there is a reference to the JS extension object kept in the browser window object, so I would expect the browser to keep the code.
So – what’s my mistake here, please?
Cheers,
Tom
PS: WSC stands for Windows Scripting Component
It sounds like your reference to the window object is no longer valid when the JS finally executes – What causes the error "Can't execute code from a freed script"
A couple of things you could try:
instead of
win.document.parentWindow.top, just usewin.document.parentWindowwhich should point to a valid window reference. As where “top” could point to a parent frame or window and you end up with one of the causes in the provided link.Check to make sure the reference to
myNS.extObjectisn’t being destroyed in the bho. You should probably try catch each statement inOnDocumentCompletebecause either could be giving you the error you’re seeing and both have different resolutions.Finally, if you can, execute the extension using the Context Menu in IE – http://msdn.microsoft.com/en-us/library/bb735853(v=vs.85).aspx
Then you would have access to the window object via the
external.menuArgumentsobject in javascript land.