惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google Online Security Blog
Google Online Security Blog
博客园_首页
酷 壳 – CoolShell
酷 壳 – CoolShell
Jina AI
Jina AI
博客园 - Franky
大猫的无限游戏
大猫的无限游戏
Hugging Face - Blog
Hugging Face - Blog
博客园 - 司徒正美
V
V2EX
雷峰网
雷峰网
云风的 BLOG
云风的 BLOG
V
Visual Studio Blog
F
Full Disclosure
Y
Y Combinator Blog
V
V2EX - 技术
Attack and Defense Labs
Attack and Defense Labs
S
Security @ Cisco Blogs
Schneier on Security
Schneier on Security
Microsoft Azure Blog
Microsoft Azure Blog
SecWiki News
SecWiki News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
The GitHub Blog
The GitHub Blog
量子位
PCI Perspectives
PCI Perspectives
S
Secure Thoughts
D
Darknet – Hacking Tools, Hacker News & Cyber Security
AWS News Blog
AWS News Blog
Blog — PlanetScale
Blog — PlanetScale
爱范儿
爱范儿
K
Kaspersky official blog
B
Blog
A
Arctic Wolf
Hacker News: Ask HN
Hacker News: Ask HN
L
LangChain Blog
T
Tor Project blog
P
Privacy & Cybersecurity Law Blog
Recent Announcements
Recent Announcements
宝玉的分享
宝玉的分享
The Register - Security
The Register - Security
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
L
Lohrmann on Cybersecurity
D
Docker
A
About on SuperTechFans
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Google DeepMind News
Google DeepMind News
The Last Watchdog
The Last Watchdog
S
Security Affairs
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
P
Privacy International News Feed
Simon Willison's Weblog
Simon Willison's Weblog

博客园 - LeeWenjie

【转发】NPAPI学习(Firefox和Chrome扩展开发 ) 【转发】网易邮箱前端技术分享之javascript编码规范 【转发】揭秘Facebook 的系统架构 【转发】淘宝网架构分享总结 【转发】淘宝网采用什么技术架构来实现网站高负载的 【转发】浅析淘宝网首页信息架构的变迁 【转发】Html5 File Upload with Progress 【转载】使用JavaScript实现Motion Detection 【转发】WEB前端开发规范文档 【转发】响应式Web设计?怎样进行? 【转发】开源中最好的Web开发的资源 【原创】PDA DataGrid的滚动条事件处理 【原创】PDA 实现DataGrid可编辑 【转发】SharePoint 2010——ListData.svc故障排除 【转发】谈论 SharePoint 2010中的沙盒解决方案(Sandboxed Solution) 【原创】NET4.0在SharePoint 2010 IIS下出现导演 C#实现多语言 【转载】互联网产品开发中的“快”字诀 【转载】SHAREPOINT TimeJob定时器开发
【转发】NPAPI开发详解,Windows版
LeeWenjie · 2013-12-11 · via 博客园 - LeeWenjie

NPAPI开发详解,Windows版

本文通过多图组合,详细引导初学者开发NPAPI的浏览器插件。

如需测试开发完成的插件请参考http://mozilla.com.cn/kb/dev/A.88/

1. 准备工作

开发工具

本例使用的是visual studio 2008 英文版,下图是关于信息关于信息

Windows SDK

本例使用Windows7操作系统 这里下载SDK

NPAPISDK

本例使用的是Firefox4.0.1提供的SDK。

首先,从这里下载mozilla源码。然后,解压firefox-4.0.1.source.tar.bz2文件。

将 \firefox-4.0.1.source\mozilla-2.0\modules\plugin 目录解压缩出来,里面有我们开发NPAPI插件所需的所有资源。

为了方便大家使用,--这里--提供plugin.rar的下载。

本例将plugin目标解压到D:\code\下(后面统一使用绝对路径,以避免异意)

2. 创建Plugin

本着“有图有真相”的原则,下面将连续多图并配文字一步步创建、调试Plugin。图中画红圈的代表需要填写或者需要选择的地方。

创建项目

新建项目 alt text

Name项一定要以np开头,为了将来适应不同操作系统,最好全小写,不要太长,尽量控制在8字符内。
本例定义为npdemo
Location项定义到plugin\sdk\samples以便项目属性中用相对路径引用NPAPI的SDK
本例定义为d:\code\plugin\sdk\samples
alt text

向导
alt text

选择Application typeDLL
选择Empty project
alt text

添加文件

首先,添加NPAPI SDK中的Common文件
alt text

一共3个文件
alt text

然后,添加def文件
alt text

命名最好与项目一致
alt text

编辑npdemo.def为

1

2

3

4

5

6

LIBRARY "npdemo"

EXPORTS

NP_GetEntryPoints @1

NP_Initialize @2

NP_Shutdown @3



现在,添加资源
alt text

选择Version
alt text

自动生成了resource.hnpdemo.rc。由于要在版本信息中加项,所以手工npdemo.rc
alt text

选择“Y”
alt text

在图中的BLOCK中添加。注意!BLOCK 一定要是"040904e4"
VALUE "MIMEType", "application/demo-plugin"
这里顺便说一下,MIMEType是plugin的唯一标示,需要自己定义
通常的格式是"application/“+ [plugin name]
本例中定义为"application/demo-plugin"
alt text

下图是rc文件数据项与plugin数据项(about:plugins 中)的对应关系
alt text

下面添加最关键的部分:Plugin实现类
alt text
alt text

类名可以随便起,本例命名为CPlugin
但是一定要继承自nsPluginInstanceBace
alt text

修改Plugin.h

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

#pragma once

#include "pluginbase.h"

class CPlugin : public nsPluginInstanceBase

{

private:

NPP m_pNPInstance;

NPBool m_bInitialized;

public:

CPlugin(NPP pNPInstance);

~CPlugin();

NPBool init(NPWindow* pNPWindow) { m_bInitialized = TRUE; return TRUE;}

void shut() { m_bInitialized = FALSE; }

NPBool isInitialized() { return m_bInitialized; }

};



修改Plugin.cpp
其中实现了4个全局函数

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

#include "plugin.h"

NPError NS_PluginInitialize()

{

return NPERR_NO_ERROR;

}

void NS_PluginShutdown()

{

}

nsPluginInstanceBase * NS_NewPluginInstance(nsPluginCreateData * aCreateDataStruct)

{

if(!aCreateDataStruct)

return NULL;

CPlugin * plugin = new CPlugin(aCreateDataStruct->instance);

return plugin;

}

void NS_DestroyPluginInstance(nsPluginInstanceBase * aPlugin)

{

if(aPlugin)

delete (CPlugin *)aPlugin;

}

CPlugin::CPlugin(NPP pNPInstance) : nsPluginInstanceBase(),

m_pNPInstance(pNPInstance),

m_bInitialized(FALSE)

{

}

CPlugin::~CPlugin()

{

}


修改项目属性

打开项目属性 alt text

修改字符集设置为“Use Multi-Byte Character Set
alt text

添加搜索目录 “....\include”和“........\base\public
alt text

添加预编译宏 X86
alt text

现在可以编译了!

3、注册、测试

本例编译后,在D:\code\plugin\sdk\samples\npdemo\Debug生成npdemo.dll

打开注册表,在HKEY_CURRENT_USER\SOFTWARE\MozillaPlugins下新建子项@mozilla.com.cn/demo
并新建字符串数据“Path”设值为D:\code\plugin\sdk\samples\npdemo\Debug\npdemo.dll

alt text

打开火狐浏览器 在地址栏输入“about:plugins” 如果在plugin列表中有本例的npdemo.dll及说明我们的plugin示例已经成功完成
alt text

简单的测试页面: 

1

2

3

4

5

6

7

<HTML>

<HEAD>

</HEAD>

<BODY>

<embed type="application/demo-plugin">

</BODY>

</HTML>

特别注意

如果在实际部署中使用安装文件安装plugin,并用注册表的方式注册。那么就不需要重启火狐,只要在页面中执行 navigator.plugins.refresh(false); 然后刷新页面即可使用刚安装的plugin