将 FF 扩展移植到 IE:JS WSC 中没有全局窗口对象

发布于 2024-10-17 06:14:16 字数 1506 浏览 3 评论 0原文

我正在尝试将扩展从 Firefox 移植到 IE。所有扩展的代码都是用 JS 编写的(没有 C++ 组件),所以我想我会重用这些代码,只用它的 mozilla 特定部分来麻烦自己(这无疑也是一种折磨,但我认为这比重写它更痛苦)从头开始)。

FF 扩展被包装在一个 JS 对象中,该对象在“load”事件期间被初始化。然后,扩展的“业务”代码在“DOMContentLoaded”事件上触发。

我最初的计划是创建一个 WSC(窗口脚本组件)并在那里侦听 onLoad 事件,基本上执行与原始代码相同的操作。然而,我发现我的 JavaScript 脚本组件中没有隐式全局窗口对象,

我的 B 计划是监听 C++ BHO 代码中的“onBeforeNavigate2”事件,并调用处理扩展对象初始化的 JS WSC 代码。因此,我决定将浏览器窗口对象作为“window”参数传递给 onBeforeNavigate JS 函数,并重写原始扩展代码以使用显式“window”变量,而不是依赖隐式全局对象。

但是,当我处理“OnDocumentComplete”事件(再次在 BHO 中侦听它并将浏览器窗口对象作为参数传递给 WSC 中的 JS 函数)时,我收到“无法从释放的脚本中执行代码” “ 错误。

WSC 代码如下所示:

<?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>

我认为这是因为处理函数返回后 WSC 被卸载。但是,浏览器窗口对象中保留了对 JS 扩展对象的引用,因此我希望浏览器保留代码。

那么 - 我的错误是什么?

干杯, Tom

PS:WSC 代表 Windows 脚本组件

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小嗲 2024-10-24 06:14:16

听起来当 JS 最终执行时,您对 window 对象的引用不再有效 - 是什么原因导致错误“无法从已释放的脚本执行代码”

您可以尝试以下几件事:

而不是 win.document.parentWindow.top,只需使用 win.document.parentWindow 即可,它应指向有效的窗口引用。由于“顶部”可能指向父框架或窗口,因此您最终会在提供的链接中找到原因之一。

检查以确保对 myNS.extObject 的引用没有在 bho 中被破坏。您可能应该尝试捕获 OnDocumentComplete 中的每个语句,因为其中任何一个都可能给您带来您所看到的错误,并且两者都有不同的解决方案。

最后,如果可以的话,请使用 IE 中的上下文菜单执行扩展 - http://msdn.microsoft.com/en-us/library/bb735853(v=vs.85).aspx

然后您就可以通过外部访问窗口对象javascript 领域中的 .menuArguments 对象。

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 use win.document.parentWindow which 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.extObject isn't being destroyed in the bho. You should probably try catch each statement in OnDocumentComplete because 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.menuArguments object in javascript land.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文