I figured it out. The issue was not related to NPAPI, or JS multithreading at all.
I was calling a method on NPAPI plugin object in a HTML form.
<form>
<input id="whatever-button" type="submit" value="Whatever"></input>
</form>
<script type="text/javascript">
$('#whatever-button').click(function(e) {
var plugin = document.getElementById('plugin');
plugin.whatever();
});
</script>
What I have not considered is the page reload - which is the default action after a click on a button. Page reload then triggered plugin object recreation, and it was fast enough, so I have not noticed it. The problem is solved by simply adding e.preventDefault();
to the callback function. This is how the working code looks like.
<embed id="plugin" type="application/my-npapi-plugin" width="640" height="480">
<form>
<input id="whatever-button" type="submit" value="Whatever"></input>
</form>
<script type="text/javascript">
$('#whatever-button').click(function(e) {
e.preventDefault();
var plugin = document.getElementById('plugin');
plugin.whatever();
});
</script>
UPD: Actually, setting input type to button
, instead of submit
is easier way to solve the issue. It also does not require e.preventDefault();
.