Search code examples
javascriptgoogle-chromedomnpapi

calling javascript function from NPAPI plugin


i have writing NPAPI plugin to access DOM of the current page. i am able to build plugin. now i want to call javascript function console.debug("hello from c++"); from NPAPI plugin. i have taken following code i am using helloworld sample code from google to build npapi plugin: code:

bool ScriptablePluginObject::Invoke(NPObject* obj, NPIdentifier methodName, const NPVariant* args,uint32_t argCount, NPVariant* result) 
{   
   // The message i want to send.
   char* message = "Hello from C++";

   // Get window object.
   NPObject* window = NULL;
   NPN_GetValue(npp_, NPNVWindowNPObject, &window);

   // Get console object.
   NPVariant consoleVar;
   NPIdentifier id = NPN_GetStringIdentifier("console");
   NPN_GetProperty(npp_, window, id, &consoleVar);
   NPObject* console = NPVARIANT_TO_OBJECT(consoleVar);

   // Get the debug object.
   id = NPN_GetStringIdentifier("debug");

   // Invoke the call with the message!
   NPVariant type;
   STRINGZ_TO_NPVARIANT(message, type);
   NPVariant args[] = { type };
   NPVariant voidResponse;
   NPN_Invoke(npp_, console, id, args,sizeof(args) / sizeof(args[0]),&voidResponse);

   // Cleanup all allocated objects, otherwise, reference count and
   // memory leaks will happen.
   NPN_ReleaseObject(window);
   NPN_ReleaseVariantValue(&consoleVar);
   NPN_ReleaseVariantValue(&voidResponse);
}

but after loading when i am calling test.html it is getting crashed. please let me know "am i calling this code at right place" and "how can i test this code".

thanks...


Solution

  • thanks taxilian...

    your suggestion helped. i have debugged chrome.exe and identified issue with npnfuncs structure. empty structure was causing the issue. so few code changes helped in resolving this issue. chrome can be debugged using (for others who might wanna try)

    C:\Program Files\Google\Chrome\Application>chrome.exe --plugin-startup-dialog --wait-for-debugger