views:

22

answers:

2

Hey I got a quick question:

I have got a chrome extension that adds a popup page to the toolbar. It accepts input from the user then calls a NPAPI dll which generates an XML file. I would like to be able to, after the NPAPI dll finishes its work, create a new tab which would open to the newly generated XML page.

Can anyone help me out with this?

Thanks.

A: 

Hi user483281,

You can let your NPAPI communicate to JavaScript via NPN_Invoke. NPN_Invoke will allow you to invoke a method on a given NPObject.

For example, you can construct your method NPN_GetStringIdentifier, and use that to execute a method in your popup.

You can refer to this article, on how to communicate back to JavaScript from NPAPI plugin. The example shown there is a simple console.debug("Hello from C++")

Update

I noticed you want to open the xml file right after. Did you save the XML file in the location of the extension folder?

C:\Users\[user]\AppData\Local\Google\Chrome\User Data\Default\Extensions\[extension_id]\[extension_version]

Then you can open it with chrome.extension.getURL([file]);

But, it would be great if you return the XML file and open it within the extension itself.

Mohamed Mansour
A: 

Ideally you don't put the logic for UI behavior into the plugin, but in the extension. The plugin should tell the extension where the XML file is located and the extension should handle the rest.

This could be done two ways:

Synchronously returning the location is easy: You do the work directly from the Invoke() implementation of your plugin and return a string containing the location of the file to the script.
This of course has the downside of blocking the main- (and GUI-)thread until processing is done and thus is no option when processing is not done quickly.

Asynchronously returning it is a bit more work: Ideally you allow the extension to specify a callback function that should be invoked when processing is done. This could be achieved by either handling addEventListener() accordingly or by allowing the script to pass a callback as a parameter when it initiates the XML processing. The XML processing function then starts or feeds another thread doing the actual work and returns immediately. When the processing is done you call NPN_InvokeDefault() on the stored function object to call it.
Beware: Calling into JavaScript always has to be done on the main thread.

Georg Fritzsche

related questions