我正在尝试使用新的 WebExtension API 将 Chrome 扩展程序转换为 Firefox。
除了在网页中使用 chrome.runtime.sendMessage() 之外,一切正常。目标是与插件通信并传递一些数据。
为此,我使用属性“externally_connectable”,如下所示: can-a-site-invoke-a-browser-extension
背景.js
chrome.runtime.onMessageExternal.addListener(
function(request, sender, sendResponse) {
sendResponse({
success: true,
message: 'ok'
});
return true; // Bug chrome, close channel otherwise
});
Run Code Online (Sandbox Code Playgroud)
在网站上
chrome.runtime.sendMessage(EXTENSION_ID, {type: 'show', data: 'test'}, function(response) {
if (response.success && !response.success) {
console.log(response.message);
}
Run Code Online (Sandbox Code Playgroud)
});
在 Chrome 中,通信工作正常,但在 Firefox 中,网页中执行的代码不起作用:“chrome 未定义”。
是否有另一个 var 可以使用而不是“chrome”或者它没有实现?
我在网上没有找到任何关于此的信息:( 谢谢
小智 5
Web 扩展不支持 externally_connectable 网站脚本,但您可以在网站脚本和扩展脚本之间进行通信,如本示例所示 https://github.com/mdn/webextensions-examples/tree/master/page-to-extension-messaging
| 归档时间: |
|
| 查看次数: |
2769 次 |
| 最近记录: |