天天看點

閱讀了解FireFox浏覽器插件開發文檔(一)

昨天白天用官方示例在主機安裝了FF插件,然而晚上回寝室用VS2013并沒有成功,不禁想起了那句話“程式員都是好男人,他們每天都會檢討自己,我又錯在哪了”。

言歸正傳,今天的目标是閱讀了解FF插件官方文檔,我這菜雞一般的英語水準勉強能應付。

一、Initialization

當一個插件加載了,在建立第一個執行個體之前就會調用

NPError NP_Initialize(void) {};

配置設定記憶體資源給所有執行個體使用。

對應的NP_Shutdown函數是在最後一個執行個體銷毀糊調用,用來釋放NP_Initialize配置設定的記憶體資源

/* Define global variable to hold the user agent string. */
static char* userAgent = NULL;

/* Initialize function. */
NPError NP_Initialize(void)
{
  /* Get the user agent from the browser. */
  char* result = NPN_UserAgent();
  if (result == NULL) return NPERR_OUT_OF_MEMORY_ERROR;

  /* Allocate some memory so that you can keep a copy of it. */
  userAgent = (char*) NPN_MemAlloc(strlen(result) + );
  if (userAgent == NULL) return NPERR_OUT_OF_MEMORY_ERROR;

  /* Copy the string to your memory. */
  strcpy(userAgent, result);

  return NPERR_NO_ERROR;
}

/* Shutdown function */
NPError NP_Shutdown(void)
{
  /* Delete the memory you allocated. */
  if (userAgent != NULL)
    NPN_MemFree(userAgent);

  return NPERR_NO_ERROR;
}
           

userAgent 是什麼,“使用者代理”?算了暫時不管了,反正它是個全局靜态指針變量。指的是NPN_UserAgent()得到的資源。

二、MIMEType

在初始化的時候,浏覽器會去數MIMEType類型的已被注冊的插件。接着看看怎麼注冊插件吧。

“A MIME type is made up of a major type (such as application or image) and a minor type, for example, image/jpeg.If you define a new MIME type for a plug-in, you must register it with IETF (Internet Engineering Task Force). Until your new MIME type is registered, preface its name with “x-“, for example, image/x-nwim.”

IETF看不懂。我的FF浏覽器application已經存在,是以我昨天修改的MIMEType沒有加“x-”。

緊接着往後看我就炸了!

When a Gecko-based browser starts up, it checks certain directories for plug-ins, in this order:

Windows

1、Directory pointed to by MOZ_PLUGIN_PATH environment variable.

2、%APPDATA%\Mozilla\plugins, where %APPDATA% denotes per-user Application Data directory.

3、Plug-ins within toolkit bundles.

4、Profile directory\plugins, where Profile directory is a user profile directory.

5、Directories pointed to by HKEY_CURRENT_USER\Software\MozillaPlugins*\Path registry value, where * can be replaced by any name.

6、Directories pointed to by HKEY_LOCAL_MACHINE\Software\MozillaPlugins*\Path registry value, where * can be replaced by any name.

奇怪啊,最開始我在

HKEY_LOCAL_MACHINE\Software\MozillaPlugins\添加@mozilla.com.cn/test可是浏覽器并沒有檢測到我的插件啊,反而在HKEY_CURRENT_USER\Software\MozillaPlugins\添加時才有效果。現在再試一次,還是如此!

算了不較真了,除了最後一條,其他的在我電腦上都好用。

Mac OS X

1、~/Library/Internet Plug-Ins.

2、/Library/Internet Plug-Ins.

3、/System/Library/Frameworks/JavaVM.framework/Versions/Current/Resources.

4、Plug-ins within toolkit bundles.

5、Profile directory/plugins, where Profile directory is a user profile directory.

Linux

1、 Directory pointed to by MOZ_PLUGIN_PATH environment variable. For example:

#!/bin/bash
export MOZ_PLUGIN_PATH=/usr/lib64/mozilla/plugins
exec /usr/lib64/firefox/firefox

Which /usr/lib64/mozilla/plugins this is path for folder with plugins,  /usr/lib64/firefox/firefox  this is path for firefox (binary file). 
           

2、 ~/.mozilla/plugins.

3、 /usr/lib/mozilla/plugins (on 64-bit systems, /usr/lib64/mozilla/plugins is used instead).

繼續閱讀