我的Firefox插件开发之旅(5)——编译和测试第一个Plugin例子:npruntime

 

前几天一直在忙乎着研究和阅读XUL和FF的Extension相关资料,今天突然发现,我要做得事情似乎不是用Extension来实现的。因为原来的应用是一个ActiveX,网页会将一些参数通过ActiveX控件传递进来,而ActiveX接到这些参数以后,会和服务器进行一些数据交互,最后调起本地的执行程序。似乎我走了一些弯路,方向不对。我要做得事情应该是编写一个Plugin,而非Extension。

及时修正了下一步的研究计划,那么就让我们先来编译和测试Mozilla提供的一个小例子吧。原文连接在这里:

https://developer.mozilla.org/En/Compiling_The_npruntime_Sample_Plugin_in_Visual_Studio。文章中是以VS2003举例,我这里以VC6来进行测试,并且将步骤中需要注意的地方特别说明了一下。

1、先到这里下载相关源码:http://mxr.mozilla.org/seamonkey/source/modules/plugin/samples/npruntime/。不知道为什么Mozilla只提供了页面形式,没有打个包,害得我得一个个文件下载和保存,郁闷。

2、在VC6中新建一个Win32 Dynamic-Link Library工程,取名“nprt”。注意:建议工程以小写np开头,并且长度不超过8个字符,这样以后就会省事很多。

3、下一步,选择“An empty DLL project”,然后“Finish”。

4、将刚才下载的源码添加到工程中来。test.html不用添加,nprt.def建议也添加进来。

5、现在还不能编译,否则会出很多错误。打开Project Settings,转到C/C++标签页,在Category中选择Preprocessor,在Preprocessor Definitions中最后面添加:,XP_WIN32,MOZILLA_STRICT_API,XPCOM_GLUE,XP_WIN,_X86_。注:如果你使用VS 2005,需要添加全部的定义:WIN32;_WINDOWS;XP_WIN32;MOZILLA_STRICT_API;XPCOM_GLUE;XP_WIN;_X86_;NPSIMPLE_EXPORTS
   _DEBUG

6、在Additional include directories下面添加XULRunner SDK的include路径,可以是绝对路径,也可以是相对路径,看你当初怎么解压的了。例如:../../xulrunner-sdk/include

7、在Category中选择Precompiled Headers,选择Not using precompiled headers,OK,关闭Project Settings对话框。打开npp_gate.cpp,将下面的代码注释掉:
jref NPP_GetJavaClass (void)
{
  return NULL;
}

8、编译!……我靠,有错:Fatal error C1083: Cannot open include file: 'npapi.h': No such file or directory。我可是严格按照教程上面写的去做得啊。

9、在xulrunner-sdk目录下搜索了一下npapi.h,原来在xulrunner-sdk/sdk/include下面,这好办,多加一条包含路径不就欧了?仿照第6步,添加该目录;

10.修改  plugin.cpp 裡面的 Invoke method 改成下面這樣,

否則當 firefox 呼叫你的 plugin 時, 會 當掉.

------------------------------------------------------------------

bool ScriptablePluginObject::Invoke(NPIdentifier name, const NPVariant *args,
                               uint32_t argCount, NPVariant *result) {
  if (name == sFoo_id) {
    printf ("foo called!/n");
    MessageBox(NULL,L"foo 被呼叫 ",L"Java Script 呼叫範例",MB_OK);
    
    return PR_TRUE;
  }

  return PR_FALSE;
}

11、编译!OK,过了。

12、到输出目录下将nprt.dll复制到FF安装目录的plugins目录下,例如:C:/Program Files/Mozilla Firefox/plugins/

13、如果此时FF开着也没关系,不用关(这点很不错)。在地址栏键入about:plugins,看看是不是有一个npruntime scriptable example plugin,如果有,那就表示欧了,FF认出了我们的插件;

14、在FF中打开test.html,嗯?怎么还提示安装缺失插件?用EditPlus打开test.html,噢,原来有这么一句:<embed type="application/x-java-mozilla-npruntime-scriptable-plugin" style="display: block; width: 50%; height: 100px;">。因为我们刚才没有添加名字为x-java-mozilla-npruntime-scriptable-plugin的插件,所以当然认不出来啦。

15、随便点一下页面上的按钮,看看简单的交互行为。

16.

測試:

     Step 1: 把 nprt.dll 放到 firefox 的 plugins 目錄底下

     Step 2: 開啟 firefox 在網址輸入

                    about:plugins

                   看看你的 plugins 是否在裡面.

                   長相應該是這樣.

      Step 3: 執行 測試 test.html

       注意 1: 你下載的 test.html 已經嚴重過期了. 所以我的作法是自己寫一個

/*************************

<HTML>
<HEAD>
<TITLE>Scriptable Plug-in Test</TITLE>
</HEAD>
<BODY id="bodyId">

<center>
<h1>Sample Scriptable Plug-in </h1>
</center>

<br><br>

<center>
<script>
function bar(arg)
{
  document.getElementById("result").innerHTML += "<p>" + "function bar(" + arg + ") called!" + "</p>";

  return 4;
}
</script>

<div id="result">
<p>results go here:</p>
</div>

<embed id="embed1"
    name="kk"
       type="application/mozilla-npruntime-scriptable-plugin"
    width=600 height=40>
    <br>


<script>
var embed1 = document.getElementById('embed1');
</script>

<br>
<a href="javascript:;" onclick='document.kk.foo()'>kk test</a>

<form name="formname">
<input type=button value="alert(embed1.foo())" onclick='alert(embed1.foo())'>
</form>
</center>

</BODY>
</HTML>

*************************/

OK,第一个例子还算顺利,比想象中的要顺利。下面就该研究一下代码了,看看FF的插件和ActiveX的区别到底有多大。

附一些参考资料:
http://xinsync.xju.edu.cn/index.php/archives/2124
http://mqjing.blogspot.com/2008/09/plugin-firefox-plugin.html

 

延伸參考資訊

[1] 主要 NPAPI API 參考文件

[2] Scripting Plugins 說明文件

[3] Gecko_SDK 下載地點

[4] 一堆 NPAPI 範例

 

部分内容来自http://mqjing.blogspot.com/2008/09/plugin-firefox-plugin.html

你可能感兴趣的:(测试,plugins,extension,mozilla,Firefox插件,preprocessor)