gpt4 book ai didi

javascript - onMessage 事件永远不会被调用

转载 作者:行者123 更新时间:2023-11-30 18:23:33 26 4
gpt4 key购买 nike

所以我正在为 Firefox 开发我的下一个附加组件,但是,在阅读 ContextMenus 的文档时,似乎永远不会调用 onMessage 事件,这是我的代码:

exports.main = function(){
var addonTab = require('addon-page');
var data = require('self').data;
var tabs = require('tabs');
var cm = require("context-menu");

cm.Item({
label: "Tag This Image",
context: cm.URLContext("*"),
contextSelector: "img",
contentScript:
'self.on("click", function(node, data){' +
' self.postMessage(node);' +
'});',

// nothing works...
onMessage: function(node){
openImageEditor(node.src);
alert("Message? It worked? No way!");
onTagImage(node);
}
});

var onTagImage = function(node){
alert("Image tagged!");
};

tabs.open({url: data.url('index.html'), isPinned:true});
};

所以我看了一下错误的控制台,但这可能是我见过的最糟糕的错误格式......

Timestamp: 14/07/2012 3:21:44 Error: An exception occurred. Traceback (most recent call last): File "resource://jid0-dxglsws2k0cubycbcn7cw5tcyqk-at-jetpack/addon-kit/lib/context-menu.js", line 1310, in CMP_handleEvent this.handleClick(event.target); File "resource://jid0-dxglsws2k0cubycbcn7cw5tcyqk-at-jetpack/addon-kit/lib/context-menu.js", line 1339, in CMP_handleClick this.browserWin.fireClick(topLevelItem, popupNode, item.data); File "resource://jid0-dxglsws2k0cubycbcn7cw5tcyqk-at-jetpack/addon-kit/lib/context-menu.js", line 1162, in BW_fireClick worker.fireClick(popupNode, clickedItemData); File "resource://jid0-dxglsws2k0cubycbcn7cw5tcyqk-at-jetpack/addon-kit/lib/context-menu.js", line 663, in CMW_fireClick this._contentWorker.emitSync("click", popupNode, clickedItemData); File "resource://jid0-dxglsws2k0cubycbcn7cw5tcyqk-at-jetpack/api-utils/lib/content/worker.js", line 71, in emitSync return this._emitToContent(Array.slice(arguments)); File "resource://jid0-dxglsws2k0cubycbcn7cw5tcyqk-at-jetpack/api-utils/lib/content/content-worker.js", line 96, in onChromeEvent return emit.apply(null, args); File "resource://jid0-dxglsws2k0cubycbcn7cw5tcyqk-at-jetpack/api-utils/lib/content/content-worker.js", line 45, in onEvent results.push(callback.apply(null, args)); File "javascript:self.on("click", function(node, data){
self.postMessage(node);});", line 1, in null File "resource://jid0-dxglsws2k0cubycbcn7cw5tcyqk-at-jetpack/api-utils/lib/content/content-worker.js", line 81, in onEvent let str = JSON.stringify(args, replacer); [Exception... "Component returned failure code: 0x80040111 (NS_ERROR_NOT_AVAILABLE) [nsIImageLoadingContent.loadingEnabled]" nsresult: "0x80040111 (NS_ERROR_NOT_AVAILABLE)" location: "JS frame :: resource://jid0-dxglsws2k0cubycbcn7cw5tcyqk-at-jetpack/api-utils/lib/cuddlefish.js -> resource://jid0-dxglsws2k0cubycbcn7cw5tcyqk-at-jetpack/api-utils/lib/sandbox.js -> resource://jid0-dxglsws2k0cubycbcn7cw5tcyqk-at-jetpack/api-utils/lib/content/content-worker.js :: onEvent :: line 81" data: no]

这是实际的行,但这对我来说没有任何意义:

File "javascript:self.on("click", function(node, data){self.postMessage(node);});", line 1, in null

问题:这个错误是什么意思,或者说的行到底有什么问题?

最佳答案

您的上下文菜单实现在某些方面有点古怪,请参见这个固定示例:

https://builder.addons.mozilla.org/addon/1065630/latest/

  1. 内容脚本的postMessage调用不能发送html节点,只能发送JSON数据。

  2. 不要在 main.js 或从 ./lib/加载的其他模块中使用警报 - 此处未定义警报。请改用 console.log。

  3. 你需要的上下文是context: cm.SelectorContext('img')

关于javascript - onMessage 事件永远不会被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11480301/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com