2013-04-04 203 views
-1

我試圖編寫一個NPAPI插件的hello world示例。我實現了所需的所有基本功能,並添加了一個Get_String()函數,該函數返回hello world字符串。通過Javascript調用的NPAPI函數

建立後,瀏覽器可以檢測到插件和所有相關的信息,但我不能從JavaScript調用我的Get_String()函數! 這裏的一些代碼: plugin.c

#define PLUGIN_NAME  "Name Plugin" 
#define PLUGIN_DESCRIPTION " Plugin Description" 
#define PLUGIN_VERSION  "1.0" 

static NPNetscapeFuncs* sBrowserFuncs = NULL; 

NP_EXPORT(NPError) 
NP_Initialize(NPNetscapeFuncs* bFuncs, NPPluginFuncs* pFuncs) 
{ 
    sBrowserFuncs = bFuncs; 

    if (pFuncs->size < (offsetof(NPPluginFuncs, setvalue) + sizeof(void*))) 
    return NPERR_INVALID_FUNCTABLE_ERROR; 

    pFuncs->newp = NPP_New; 
    pFuncs->destroy = NPP_Destroy; 

    return NPERR_NO_ERROR; 
} 


NP_EXPORT(char*) 
NP_GetPluginVersion() 
{ 
    return PLUGIN_VERSION; 
} 


NP_EXPORT(const char*) 
NP_GetMIMEDescription() 
{ 
    return "application/my-plugin::"; 
} 


NP_EXPORT(NPError) 
NP_GetValue(void* future, NPPVariable aVariable, void* aValue) { 
    switch (aVariable) { 
    case NPPVpluginNameString: 
     *((char**)aValue) = PLUGIN_NAME; 
     break; 
    case NPPVpluginDescriptionString: 
     *((char**)aValue) = PLUGIN_DESCRIPTION; 
     break; 
    default: 
     return NPERR_INVALID_PARAM; 
     break; 
    } 
    return NPERR_NO_ERROR; 
} 


NP_EXPORT(NPError) 
NP_Shutdown() 
{ 
    return NPERR_NO_ERROR; 
} 


NPError NPP_New(NPMIMEType pluginType, NPP instance, uint16_t mode, int16_t argc, char* argn[], char* argv[], NPSavedData* saved) 
{ 

    return NPERR_NO_ERROR; 
} 


NPError NPP_Destroy(NPP instance, NPSavedData** save) 
{ 

    return NPERR_NO_ERROR; 
} 

char* Get_String() 
{ 
    return "hello world from Get function" ; 
} 

void Set(NPObject object){} 

的test.html

<doctype html> 
<html> 
<head> 
<script> 
    var plugin = document.getElementById("plugin"); 
    console.log(plugin.Get_String()); 
</script> 
</head> 
<embed id="plugin" type="application/typemine-plugin"> 
<body> 
</body> 
</html> 
+0

您是否閱讀過[NPAPI教程](http://colonelpanic.net/2009/03/building-a-firefox-plugin-part-one/)或[documentation](https://developer.mozilla) .ORG/EN-US /文檔/ Gecko_Plugin_API_Reference/Scripting_plugins)?你知道更容易的選項,如[Firebreath](http://www.firebreath.org/)嗎? – 2013-04-04 16:03:47

+0

是的,我看了看它和其他人。但在我的情況下,我不需要它。我想用艱難的方式去做。 – 2013-04-04 16:10:48

+0

那麼,你有沒有閱讀教程和/或文檔?這聽起來像你缺少腳本化方法所需的所有設置,並期望一個普通的C函數只能從JS訪問。 – 2013-04-04 17:35:10

回答