我正在使用context-menu
项目单击来显示一个对话框(使用 JQuery/Bootstrap),该对话框允许用户通过 AJAX 将他们选择的文本提交到 Web 服务(这一切都很好)。提交后,我打算显示一个附加 SDKnotification
来表示“感谢您的提交”。
现在,我知道我需要从内容脚本向附加脚本发送一条消息以显示通知,但是从回调函数发送时我的消息没有到达。
我的附加代码(摘录):
var pageMod = require("page-mod");
pageMod.PageMod({
include: ['*'],
contentScriptWhen: "end",
contentScriptFile: [ data.url("js/content.js") ],
onAttach: function onAttach( worker, mod) {
worker.port.on("submittedNotif", function(msg) {
console.log('Hello');
notifications.notify({ ... });
})
}
});
内容脚本如下。我已经指出了消息到达和没有到达的情况。
// Handle the context-menu Item's click and show the dialog
self.on("click", function(node,data) {
self.port.emit("submittedNotif", '*** DOES NOT ARRIVE ***');
showDialog( 'DialogName', function (response) { /* Do stuff */ });
})
self.port.emit("submittedNotif", '*** Arrives OK ***');
function showDialog( str, response) {
// Dialog stuff
self.port.emit("submittedNotif", '*** DOES NOT ARRIVE ***');
}
我被告知self
是一个全局对象,所以我当然不必将它作为参数传递。我只是不清楚如何尝试发送 viaself.port.emit
应该根据它在内容脚本中的使用位置而有所不同。例如,我不知道有任何线程问题。也许这是我的 JavaScript 知识的一个空白,但谁能告诉我我做错了什么?