问题
I'm developing FF extension and plugin that work in tandem. My Extension injects npapi plugin into the html and calls some method of the plugin after an event occures.
Here is the code I use for injection:
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);
}
And when I need to use a method of the plugin I do the following:
var element = window.content.document.getElementById("rondyoHookMessageElement");
element.SomeFunc();
I confirm that element is found, but logging the element.SomeFunc
returns undefined
.
If I inject the npapi plugin manually:
<!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>
It returns function SomeFunc() { [native code] }
OS: Mac OS X 10.6.7
FF: 3.6.13
回答1:
If you do this in FireFox 4 you have a decent chance of crashing the browser (the bug has been logged, but not yet fixed). it's not a good idea to set the type of the object tag before injecting it into the DOM; you'll get different behavior on each browser. Wait until you've put the object into the dom and then inject it.
Another possible problem is that it sometimes takes the browser some time after injecting it into the DOM before the plugin is accessible, so if you use a setTimeout to wait for a half second or so it might start working at that point.
回答2:
I solved the problem by extending script which make a call of the 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);
}
When I need to call this function from the extension I do:
window.content.document.defaultView.wrappedJSObject.f(null, mes, false);
来源:https://stackoverflow.com/questions/5688904/scriptable-npapi-plugin-doesnt-work-with-firefox