NPAPI插件開發詳細記錄:實用功能——獲取插件路徑、頁面路徑、資源路徑

http://blog.csdn.net/z6482/article/details/8301823

獲取插件路徑

該功能不復雜,不過使用了windows提供的API故只適用於windows平臺。代碼如下:

  1. LPTSTR moduleName = new TCHAR[100];  
  2. GetModuleFileName(GetModuleHandle(_T("name")),moduleName,100);  
  3. std::string mPath = std::string(moduleName);  
	LPTSTR moduleName = new TCHAR[100];
	GetModuleFileName(GetModuleHandle(_T("name")),moduleName,100);
	std::string mPath = std::string(moduleName);

GetModuleHandle(_T("name"))的name即你的插件名稱,如:npdemo.dll。TCHAR的長度視具體情況而定,用於保存得到的路徑。注意需要include tchar.h和string(是string不是string.h)。當然路徑其實已經保存在moduleName中了,如果不用string可以不要最後一句(鑑於字符串處理的繁瑣,推薦使用string)。

獲取頁面路徑、資源路徑

要獲取插件頁面的路徑,可以參考:https://developer.mozilla.org/en-US/docs/Getting_the_page_URL_in_NPAPI_plugin。其中提到了三種方式,但我感覺比較靠譜的方式是第一種,下面是簡單的實現代碼:

  1.        NPObject *pluginObj;  
  2. NPN_GetValue(m_pNPInstance,NPNVWindowNPObject,&pluginObj);  
  3. NPIdentifier  n=NPN_GetStringIdentifier("location");  
  4.         NPVariant rval;  
  5. NPN_GetProperty(m_pNPInstance,pluginObj,n,&rval);  
  6. NPObject* locationObj = rval.value.objectValue;  
  7. n=NPN_GetStringIdentifier("href");  
  8. NPN_GetProperty(m_pNPInstance,locationObj,n,&rval);  
  9. std::string  pageURL = std::string(rval.value.stringValue.UTF8Characters);  
        NPObject *pluginObj;
	NPN_GetValue(m_pNPInstance,NPNVWindowNPObject,&pluginObj);
	NPIdentifier  n=NPN_GetStringIdentifier("location");
         NPVariant rval;
	NPN_GetProperty(m_pNPInstance,pluginObj,n,&rval);
	NPObject* locationObj = rval.value.objectValue;
	n=NPN_GetStringIdentifier("href");
	NPN_GetProperty(m_pNPInstance,locationObj,n,&rval);
	std::string  pageURL = std::string(rval.value.stringValue.UTF8Characters);


object標籤可以使用data屬性設置資源的URL,embed標籤使用src屬性設置資源URL。獲取資源路徑的代碼如下:

  1. NPObject *pluginObj;  
  2.        NPN_GetValue(m_pNPInstance,NPNVPluginElementNPObject,&pluginObj);  
  3.        NPIdentifier n=NPN_GetStringIdentifier("src");  
  4.        NPVariant rval;  
  5. NPN_GetProperty(m_pNPInstance, pluginObj, n, &rval);  
  6. if(NPVARIANT_IS_STRING(rval))  
  7.     m_pSrc = std::string(NPVARIANT_TO_STRING(rval).UTF8Characters);  
  8. else{  
  9.     n=NPN_GetStringIdentifier("data");  
  10.     NPN_GetProperty(m_pNPInstance, pluginObj, n, &rval);  
  11.     if(NPVARIANT_IS_STRING(rval))  
  12.         m_pSrc = std::string(NPVARIANT_TO_STRING(rval).UTF8Characters);  
  13. }  
	NPObject *pluginObj;
        NPN_GetValue(m_pNPInstance,NPNVPluginElementNPObject,&pluginObj);
        NPIdentifier n=NPN_GetStringIdentifier("src");
        NPVariant rval;
	NPN_GetProperty(m_pNPInstance, pluginObj, n, &rval);
	if(NPVARIANT_IS_STRING(rval))
		m_pSrc = std::string(NPVARIANT_TO_STRING(rval).UTF8Characters);
	else{
		n=NPN_GetStringIdentifier("data");
		NPN_GetProperty(m_pNPInstance, pluginObj, n, &rval);
		if(NPVARIANT_IS_STRING(rval))
			m_pSrc = std::string(NPVARIANT_TO_STRING(rval).UTF8Characters);
	}



這樣,不管html中使用的是object標籤的data還是使用的embed標籤中的src設置資源URL,都可以將資源的完整URL保存到m_pSrc中。



Via window location

From Robert Platt:

// Get the Dom window object.
NPN_GetValue( m_pNPInstance, NPNVWindowNPObject, &sWindowObj );
// Create a "location" identifier.
NPIdentifier identifier = NPN_GetStringIdentifier( "location" );
// Declare a local variant value.
NPVariant variantValue;
// Get the location property from the window object (which is another object).
bool b1 = NPN_GetProperty( m_pNPInstance, sWindowObj, identifier, &variantValue );
// Get a pointer to the "location" object.
NPObject *locationObj = variantValue.value.objectValue;
// Create a "href" identifier.
identifier = NPN_GetStringIdentifier( "href" );
// Get the location property from the location object.
bool b2 = NPN_GetProperty( m_pNPInstance, locationObj, identifier, &variantValue );

This code is just a rough example. Remember to release any references after using them.


發佈了7 篇原創文章 · 獲贊 6 · 訪問量 17萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章