一个包含用于webextensions的基本本地消息传递api的包
nativemessaging的Python项目详细描述
#native messaging
一个python包,用于在webextensions中与本机消息交互
[参见mdn上的本机消息传递](https://developer.mozilla.org/en-us/docs/mozilla/add-ons/webextensions/native-messaging)
mdn](https://developer.mozilla.org/en-us/docs/mozilla/add-ons/webextensions/native_messaging_app_side)和[mdn上的本机消息/webextension示例](https://github.com/mdn/webextensions-examples/tree/master/native-messaging)(MPL 2.0许可证)
`pip3 install nativemessaging`
##` get_message()`
`nativemessaging.get_message()`将从浏览器轮询消息。
如果使用了[`runtime.connectnative`](https://developer.mozilla.org/en-us/docs/mozilla/add-ons/webextensions/api/runtime/connectnative),则必须在循环中重复调用“get_message()”来轮询消息。
如果使用了[`runtime.sendNativeMessage`](https://developer.mozilla.org/en-us/docs/mozilla/add-ons/webextensions/api/runtime/sendNativeMessage),则只需调用一次“get戋u message()”。
“encode戋u message(message戋content)”`
“nativeMessaging.encode戋message()”只需要一个参数,即要编码的消息。
返回要返回到浏览器的消息的编码版本。与“send_u message()”一起使用。
\send_u message(encoded_u message)`
`nativemessaging.send_u message()`接受一个参数,即来自“encode_u message()”的编码消息。向浏览器返回消息。
示例
浏览器端:
``javascript
函数onreceived(response){
console.log(response);
}
//runtime.connectnative
var port=browser.runtime.connectnative(“application_name”);
port.onMessage.addListener(onReceived);
port.postmessage(“hello”);
//runtime.sendNativeMessage
browser.runtime.sendNativeMessage(“应用程序名”,“你好”)。然后(onReceived);
```
app-side:
``python
import nativemessaging
while true:
message=nativemessaging.get_message()
if message==“hello”:
nativemessaging.send_message(nativemessaging.encode_message(“world”))
````
nativemessaging install
`nativemessaging install`是一个命令行脚本与软件包一起提供。
\arguments
`nativemessaging install browser[--manifest manifest]`
*`browser`-位置参数,一个或多个参数。必须是“chrome”或“firefox”。
*`--manifest`-要用于安装的清单文件的路径。
除非传递了“-manifest”。
格式必须与chrome或firefox的本机清单格式相似,但有两个主要区别:
*`path`必须是相对于当前工作目录的本机应用程序的相对路径。
*必须在清单中同时包含“允许的扩展名”和“允许的源代码”才能与chrome一起使用以及火狐。
``json
{
“name”:“application_-name”,
“description”:“description”,
“path”:“application_-name.py”,
“type”:“stdio”,
“allowed_扩展名”:“extension@id”],
“allowed_起源名”:“chrome extension://extension id”]
````
我是说,它将在与“<;path>;”相同的目录中创建“<;application\u name>;\u firefox.json”和“<;application\u name>;\u chrome.json”。
还将为windows上的python应用程序创建批处理文件。
在“hkey_current_user\software\google\chrome\nativemessaginghosts”<;application_name>;`或“hkey_current_user\software\mozilla\nativemessaginghosts”<;application_name>;`
在Linux上,它将创建` ~/.config/google chrome/nativemessaginghosts/<;application_name>;.json或`~/.mozilla/native messaging hosts/<;application_name>;.json `
在Mac上,它将创建e`~/library/application-support/google/chrome/nativemessaginghosts/<;application-name>;.json`或`~/library/application-support/mozilla/nativemessaginghosts/<;application-name>;.json`
;另请参见:
*[chrome文档的本地消息](https://developer.chrome.com/extensions/native messaging)
一个python包,用于在webextensions中与本机消息交互
[参见mdn上的本机消息传递](https://developer.mozilla.org/en-us/docs/mozilla/add-ons/webextensions/native-messaging)
mdn](https://developer.mozilla.org/en-us/docs/mozilla/add-ons/webextensions/native_messaging_app_side)和[mdn上的本机消息/webextension示例](https://github.com/mdn/webextensions-examples/tree/master/native-messaging)(MPL 2.0许可证)
`pip3 install nativemessaging`
##` get_message()`
`nativemessaging.get_message()`将从浏览器轮询消息。
如果使用了[`runtime.connectnative`](https://developer.mozilla.org/en-us/docs/mozilla/add-ons/webextensions/api/runtime/connectnative),则必须在循环中重复调用“get_message()”来轮询消息。
如果使用了[`runtime.sendNativeMessage`](https://developer.mozilla.org/en-us/docs/mozilla/add-ons/webextensions/api/runtime/sendNativeMessage),则只需调用一次“get戋u message()”。
“encode戋u message(message戋content)”`
“nativeMessaging.encode戋message()”只需要一个参数,即要编码的消息。
返回要返回到浏览器的消息的编码版本。与“send_u message()”一起使用。
\send_u message(encoded_u message)`
`nativemessaging.send_u message()`接受一个参数,即来自“encode_u message()”的编码消息。向浏览器返回消息。
示例
浏览器端:
``javascript
函数onreceived(response){
console.log(response);
}
//runtime.connectnative
var port=browser.runtime.connectnative(“application_name”);
port.onMessage.addListener(onReceived);
port.postmessage(“hello”);
//runtime.sendNativeMessage
browser.runtime.sendNativeMessage(“应用程序名”,“你好”)。然后(onReceived);
```
app-side:
``python
import nativemessaging
while true:
message=nativemessaging.get_message()
if message==“hello”:
nativemessaging.send_message(nativemessaging.encode_message(“world”))
````
nativemessaging install
`nativemessaging install`是一个命令行脚本与软件包一起提供。
\arguments
`nativemessaging install browser[--manifest manifest]`
*`browser`-位置参数,一个或多个参数。必须是“chrome”或“firefox”。
*`--manifest`-要用于安装的清单文件的路径。
除非传递了“-manifest”。
格式必须与chrome或firefox的本机清单格式相似,但有两个主要区别:
*`path`必须是相对于当前工作目录的本机应用程序的相对路径。
*必须在清单中同时包含“允许的扩展名”和“允许的源代码”才能与chrome一起使用以及火狐。
``json
{
“name”:“application_-name”,
“description”:“description”,
“path”:“application_-name.py”,
“type”:“stdio”,
“allowed_扩展名”:“extension@id”],
“allowed_起源名”:“chrome extension://extension id”]
````
我是说,它将在与“<;path>;”相同的目录中创建“<;application\u name>;\u firefox.json”和“<;application\u name>;\u chrome.json”。
还将为windows上的python应用程序创建批处理文件。
在“hkey_current_user\software\google\chrome\nativemessaginghosts”<;application_name>;`或“hkey_current_user\software\mozilla\nativemessaginghosts”<;application_name>;`
在Linux上,它将创建` ~/.config/google chrome/nativemessaginghosts/<;application_name>;.json或`~/.mozilla/native messaging hosts/<;application_name>;.json `
在Mac上,它将创建e`~/library/application-support/google/chrome/nativemessaginghosts/<;application-name>;.json`或`~/library/application-support/mozilla/nativemessaginghosts/<;application-name>;.json`
;另请参见:
*[chrome文档的本地消息](https://developer.chrome.com/extensions/native messaging)