使用上下文菜单和端口 [英] Working with context-menu and port

查看:166
本文介绍了使用上下文菜单和端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过使用内容脚本可以使用端口与上下文菜单,但下面的代码给我一个错误:cm.port是未定义的。当我发出一个事件时,相同的代码与 require(panel)一起工作,但是与上下文菜单没有关系。什么是做错了?

I read at working with content scripts that one can use port with context-menu, but the following code gives me an error: cm.port is undefined. The same code works with require("panel") when I emit an event, but not with context menu. What am doing wrong?

这是main.js

const data = require('self').data;
var cm = require("context-menu").Item({
label: "asdasd",
  contentScriptFile: data.url("panel.js")
});

cm.port.emit("myEvent", "panel is showing");

这个panel.js

this panel.js

console.log("entering the panel.js file...");
self.on("click", function(node,data) {
self.port.emit("asd");
});

self.port.on("myEvent", function(data) {
    console.log(data);
});


推荐答案

引用文档


面板和页面工作者对象直接集成了工作者API。因此,要接收与您使用panel.port.on()的面板相关联的内容脚本中的事件,请使用panel.port.on()

The panel and page-worker objects integrate the worker API directly. So to receive events from a content script associated with a panel you use panel.port.on()

面板也不是页面工作者但是上下文菜单。而上下文菜单包不允许与内容脚本进行双向通信。同样,如果您查看文档:您只能接收由内容脚本发送的消息,但不能向其发送消息。相反,消息 context 点击会在适当的情况下自动发送到内容脚本。

What you are using is neither panel nor page-worker but context-menu. And the context-menu package doesn't allow bi-directional communication with the content script. Again, if you look at the documentation: you can only receive messages sent by the content script but not send messages to it. Instead, the messages context and click are sent to the content script automatically in appropriate situations.

这篇关于使用上下文菜单和端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆