This is a very simple example of how to use [native messaging](https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Native_messaging) to exchange messages between a WebExtension and a native application.
The WebExtension, which can be found under "add-on", connects to the native application and listens to messages from it. It then sends a message to the native application when the user clicks on the WebExtension's browser action. The message payload is just "ping".
The native application, which can be found under "app", listens for messages from the WebExtension. When it receives a message, the native application sends a response message whose payload is just "pong". The native application is written in Python.
## Setup ##
To get this working, there's a little setup to do.
### Mac OS/Linux setup ###
1. Check that the [file permissions](https://en.wikipedia.org/wiki/File_system_permissions) for "ping_pong.py" include the `execute` permission.
2. Edit the "path" property of "ping_pong.json" to point to the location of "ping_pong.py" on your computer.
3. copy "ping_pong.json" to the correct location on your computer. See [App manifest location ](https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Native_messaging#App_manifest_location) to find the correct location for your OS.
### Windows setup ###
1. Check you have Python installed, and that your system's PATH environment variable includes the path to Python. See [Using Python on Windows](https://docs.python.org/2/using/windows.html). You'll need to restart the web browser after making this change, or the browser won't pick up the new environment variable.
2. Edit the "path" property of "ping_pong.json" to point to the location of "ping_pong_win.bat" on your computer. Note that you'll need to escape the Windows directory separator, like this: `"path": "C:\\Users\\MDN\\native-messaging\\app\\ping_pong_win.bat"`.
3. Edit "ping_pong_win.bat" to refer to the location of "ping_pong.py" on your computer.
4. Add a registry key containing the path to "ping_pong.json" on your computer. See [App manifest location ](https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Native_messaging#App_manifest_location) to find details of the registry key to add.
## Testing the example ##
Then just install the add-on as usual, by visiting about:debugging, clicking "Load Temporary Add-on", and selecting the add-on's "manifest.json".
You should see a new browser action icon in the toolbar. Open the console ("Tools/Web Developer/Browser Console" in Firefox), and click the browser action icon. You should see output like this in the console:
Sending: ping
Received: pong
If you don't see this output, see the [Troubleshooting guide](https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Native_messaging#Troubleshooting) for ideas.
没有合适的资源?快使用搜索试试~ 我知道了~
使用 WebExtensions API 创建的 Firefox 附加组件示例
共383个文件
js:90个
png:65个
json:59个
0 下载量 181 浏览量
2023-10-08
12:13:18
上传
评论
收藏 2.13MB ZIP 举报
温馨提示
Web扩展是编写浏览器扩展的一种方式:即程序 安装在 Web 浏览器中,用于修改浏览器的行为或 浏览器加载的网页。它们建立在一组 跨浏览器API,因此为Google Chrome或Opera编写的WebExtensions将 在大多数情况下,也可以在Firefox或Edge中运行。“webextensions-examples”存储库是一个简单但完整的集合 和可安装的网络扩展。您可以使用示例来了解如何使用 WebExtensions API,并作为您自己的 WebExtensions 的起点
资源推荐
资源详情
资源评论
收起资源包目录
使用 WebExtensions API 创建的 Firefox 附加组件示例 (383个子文件)
.babelrc 158B
ping_pong_win.bat 47B
bootstrap.min.css 118KB
bootstrap.min.css 118KB
quicknote.css 1KB
style.css 997B
bgpicker.css 498B
panel.css 481B
options.css 464B
history.css 451B
choose_ua.css 332B
choose_beast.css 316B
options.css 256B
context.css 186B
options.css 185B
tabs.css 184B
window.css 106B
cookies.css 89B
popup.css 81B
page.css 62B
.gitignore 90B
.gitignore 60B
.gitignore 14B
left-pad.html 1KB
tabs.html 1KB
options.html 1KB
page.html 1KB
window.html 1KB
popup.html 688B
latest_download.html 633B
bgpicker.html 614B
sites.html 582B
quicknote.html 495B
options.html 425B
history.html 418B
cookies.html 384B
choose_ua.html 354B
choose_beast.html 350B
preview.html 345B
options.html 333B
panel.html 271B
options.html 270B
options.html 270B
context.html 254B
popup.html 244B
popup.html 209B
options.html 202B
popup.html 180B
popup.html 168B
popup.html 168B
popup.html 168B
popup.html 168B
popup.html 168B
sidebar.html 160B
my-page.html 120B
snake.jpg 233KB
frog.jpg 229KB
turtle.jpg 138KB
browser-polyfill.min.js 8KB
tabs.js 6KB
quicknote.js 5KB
emojiMap.js 4KB
background.js 4KB
substitute.js 3KB
background.js 3KB
history.js 2KB
window.js 2KB
background.js 2KB
background.js 2KB
authorize.js 2KB
background.js 2KB
background.js 2KB
latest_download.js 2KB
bgpicker.js 2KB
karma.conf.js 2KB
background.js 2KB
proxy-handler.js 2KB
context.js 2KB
options.js 2KB
panel.js 1KB
webpack.config.js 1KB
choose_beast.js 1KB
page.js 1KB
background.js 1KB
cookies.js 1KB
auth.js 1KB
options.js 1KB
beastify.js 975B
background.js 935B
popup.js 926B
addon-ui.js 913B
background.js 876B
options.js 851B
background.js 834B
popup.js 801B
content-script.js 799B
bootstrap.js 799B
left-pad.js 794B
clipboard-helper.js 736B
user-data-storage.js 716B
共 383 条
- 1
- 2
- 3
- 4
资源评论
Java程序员-张凯
- 粉丝: 1w+
- 资源: 6656
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功