Question

I have a NPAPI plugin embedded in a HTML page. When I call a method on it from browser's developer console, everything is ok - methods call lands on the right plugin instance and is properly executed. Anyway, when I call the same method from JS callback, like a button click, for instance, plugin is destroyed and the recreated again, while NPP.pdata is set to null.

Can it be somehow related to JS multithreading model and how can it be solved?

Was it helpful?

Solution

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();.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top