2011-04-16 97 views
1

我正在开发FF扩展和插件,并行工作。我的扩展将npapi插件注入到html中,并在事件发生后调用插件的某些方法。可编脚本NPAPI插件不能与Firefox兼容

这里是我使用的注射用代码:

if (window.content.document.getElementById("rondyoHookMessageElement") == null) { 
      var element = window.content.document.createElement("object"); 
      element.type = "application/x-hook-msg"; 
      element.id = "rondyoHookMessageElement"; 
      element.width = 0; 
      element.height = 0; 
      window.content.document.body.appendChild(element); 
} 

当我需要使用插件的方法,我做了以下内容:

var element = window.content.document.getElementById("rondyoHookMessageElement"); 
element.SomeFunc(); 

我确认元素被发现,但是记录element.SomeFunc返回undefined

如果我注入手动NPAPI插件:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" > 
<head> 
</head> 
<body> 
<object id="plugin" type="application/plugin-mime" width=200 height=200 border=5></object> 
<script type="text/javascript"> 
    var plugin = document.getElementById("plugin"); 
    dump(plugin.SomeFunc + "\n"); 
</script> 
</body> 
</html> 

它返回function SomeFunc() { [native code] }

操作系统:Mac OS X 10.6.7

FF:3.6.13

回答

1

如果你这样做这在FireFox 4中有一个很好的崩溃浏览器的机会(该错误已被记录,但尚未修复)。在将对象标记注入到DOM之前设置对象标记的类型并不是一个好主意;你会在每个浏览器上获得不同的行为。等到你把这个对象放到dom中然后注入它。

另一个可能的问题是,在插件可访问之前,它有时会将浏览器注入DOM后一段时间,因此如果您使用setTimeout等待半秒左右,那么它可能会在该点开始工作。

+0

已经有一些一时间到。我认为这是一个安全问题:扩展的JS代码不能使用注入页面html的NPAPI插件。但我不确定,我还没有测试过。 – Kentzo 2011-04-17 03:53:46

1

我解决了通过扩展脚本的问题,这使SomeFunc的呼叫:

if (window.content.document.getElementById("rondyoHookMessageElement") == null) { 
      var element = window.content.document.createElement("object"); 
      element.type = "application/x-hook-msg"; 
      element.id = "rondyoHookMessageElement"; 
      element.width = 0; 
      element.height = 0; 
      window.content.document.body.appendChild(element); 

      var script = doc.createElement("script"); 
      script.type = "text/javascript"; 
      script.innerHTML = 'function f(doc, messageId, data) { document.getElementById("rondyoHookMessageElement").SomeFunc(doc, messageId, data); };'; 
      doc.body.appendChild(script); 
} 

当我需要从分机电话拨打这个功能我做的:

window.content.document.defaultView.wrappedJSObject.f(null, mes, false);