FireFox浏览器扩展开发的详细介绍可参考https://blog.csdn.net/CityBird/article/details/115013539?spm=1001.2014.3001.5501,以下单独介绍Windows平台用VB.Net如何实现。
1、manifest.json
{
"description":"Firefox extension demo description",
"name": "Firefox extension demo",
"manifest_version":2,
"version":"1.0",
"icons":{"48": "ICONS/ICON_48.png"},
"applications":{"gecko":{"id": "demo@demo.com","strict_min_version": "50.0"}},
"background":{"scripts":["background.js"]},
"browser_action":{"default_icon":{"48": "ICONS/ICON_48.png"}},
"permissions":["nativeMessaging"]
}
2、background.js
function onResponse(response) {
console.log("background Received " + JSON.stringify(response));
}
function one rror(error) {
console.log(`background Error: ${error}`);
}
/*
On a click on the browser action, send the app a message.
*/
browser.browserAction.onClicked.addListener(() => {
var JsonObj = {"name":"张三","sex":"男"};
console.log("send:"+JSON.stringify(JsonObj));
var sending = browser.runtime.sendNativeMessage("Demo",JsonObj);
sending.then(onResponse, one rror);
});
3、demo.json
{
"name": "Demo",
"description": "Demo app",
"path": "Demo.exe",
"type": "stdio",
"allowed_extensions": [ "demo@demo.com" ]
}
4、demo.exe
Visual Studio里用VB新建一个控制台应用(.Net Framework),Module1.vb源码如下:
Module Module1
Private isDebug As Boolean = False '是否debug模式
Private currentDir As String = "" '当前程序所在目录
Sub Main()
If Not IsNothing(System.Configuration.ConfigurationManager.AppSettings("debug")) Then '读debug设置
If System.Configuration.ConfigurationManager.AppSettings("debug").ToString.ToLower = "true" Then
isDebug = True
Else
isDebug = False
End If
End If
currentDir = System.Environment.CurrentDirectory '取当前程序目录
Dim str As String = ""
Do
str = GetMessage()
WriteMessage(str)
Loop While (False)
End Sub
''' <summary>输入</summary>
''' <returns>接收到的内容</returns>
Private Function GetMessage() As String
Try
Dim inputStream As System.IO.Stream = Console.OpenStandardInput
Dim inputLen As Integer = 0, tmp As Byte
For xx As Integer = 0 To 3
tmp = inputStream.ReadByte
inputLen = inputLen + tmp * Math.Pow(255, xx)
Next
WriteLog(" InLength:" & inputLen)
Dim inputBuff(inputLen - 1) As Byte
For xx As Integer = 0 To inputLen - 1
inputBuff(xx) = inputStream.ReadByte
Next
inputStream.Close()
inputStream.Dispose()
inputStream = Nothing
WriteLog(" InMessage :" & System.Text.UTF8Encoding.UTF8.GetString(inputBuff))
Return System.Text.UTF8Encoding.UTF8.GetString(inputBuff)
Erase inputBuff
Catch ex As Exception
WriteLog(" Error :" & ex.Message)
Return ""
End Try
End Function
''' <summary>输出</summary>
''' <param name="strMessage">需要输出的内容,前4个字节代表输出内容的长度</param>
Private Sub WriteMessage(ByVal strMessage As String)
Try
Dim arr() As Byte = System.Text.Encoding.UTF8.GetBytes(strMessage)
Dim len As Integer = arr.Length
WriteLog(" OutLength :" & len)
Dim outputstream As System.IO.Stream = Console.OpenStandardOutput
outputstream.WriteByte(len Mod 255)
len = len \ 255
outputstream.WriteByte(len Mod 255)
len = len \ 255
outputstream.WriteByte(len Mod 255)
len = len \ 255
outputstream.WriteByte(len)
outputstream.Write(arr, 0, arr.Length)
Erase arr
outputstream.Close()
outputstream.Dispose()
outputstream = Nothing
WriteLog(" OutMessage :" & strMessage)
Catch ex As Exception
WriteLog(" Error :" & ex.Message)
End Try
End Sub
''' <summary>写日志</summary>
''' <param name="strLog">需要写入日志的内容</param>
Private Sub WriteLog(ByVal strLog As String)
If isDebug Then
Dim myStreamWrite As New System.IO.StreamWriter(currentDir & "\Log.txt", True)
myStreamWrite.WriteLine(Now.ToString("yyyy-MM-dd HH:mm:ss") & strLog)
myStreamWrite.Close()
myStreamWrite.Dispose()
myStreamWrite = Nothing
End If
End Sub
End Module
App.config源码如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<appSettings>
<add key="debug" value="true"/>
</appSettings>
</configuration>
5、注册表
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Mozilla\NativeMessagingHosts\Demo]
@="请使用demo.json文件的完整路径替换"