context_menu.Item({
label: "Search",
context: context_menu.SelectionContext(),
contentScriptFile: [data.url("my_scripts/post_messages.js")],
onMessage: function(msg) {
if (msg == 'context') {
var text = get_selected_text();
if (text.length > 2) {
this.label = 'Search "' + text + '"';
} else {
this.label = "";
}
}
if (msg == 'click') {
var filename = 'selected_word.txt';
var str = get_selected_text();
saveText(filename, str);
}
},
});
以上是我的代码的一部分。问题是当this.label设置为“”时,右键单击菜单显示一个空标签,但我不希望显示它。我知道如果self.on('context',...)可以做到这一点; function返回false,但我不知道如何向内容脚本发送消息。
顺便说一句,使用“sdk / selection”在addon脚本中更好地定义了get_selected_text()函数,因此它没有在内容脚本中定义。
那么如何将消息(选择)发送到内容脚本,还是可能的? 或者在self.on('context',...)之后究竟发生了什么;函数返回false以使标签不显示?
答案 0 :(得分:1)
我提出了一个解决方案,但并不完美,但对我来说已经足够了。
var selection_workers = [];
tabs.on('ready', function(tab) {
var worker = tab.attach({
contentScriptFile: [data.url("my_scripts/receive_post_messages.js"),],
});
selection_workers.push(worker);
});
selection.on('select', function() {
var obj = {
text: get_selected_text(),
url: tabs.activeTab.url,
};
for (var i in selection_workers) {
var worker = selection_workers[i];
worker.port.emit('selected_text', obj);
}
});
然后,将所选文本保存为正文属性,这样我就能做我想做的事。
答案 1 :(得分:0)
尝试使用此代码。每当找到具有唯一值dont show this label unique ya ya
的标签时,我都会使用样式表来显示无。
现在,不要将其设置为空白,而是将其设置为labelValToHide
。
var {Cc, Ci} = require('chrome');
var sss = Cc['@mozilla.org/content/style-sheet-service;1'].getService(Ci.nsIStyleSheetService);
var labelValToHide = "dont show this label unique ya ya";
var css = "label[value='" + tempHold + "'] { display:none; }";
var newURIParam = {
aURL: 'data:text/css,' + encodeURIComponent(css),
aOriginCharset: null,
aBaseURI: null
}
cssUri = Services.io.newURI(newURIParam.aURL, newURIParam.aOriginCharset, newURIParam.aBaseURI);
sss.loadAndRegisterSheet(cssUri, myServices.sss.AUTHOR_SHEET);
context_menu.Item({
label: "Search",
context: context_menu.SelectionContext(),
contentScriptFile: [data.url("my_scripts/post_messages.js")],
onMessage: function(msg) {
if (msg == 'context') {
var text = get_selected_text();
if (text.length > 2) {
this.label = 'Search "' + text + '"';
} else {
this.label = labelValToHide;
}
}
if (msg == 'click') {
var filename = 'selected_word.txt';
var str = get_selected_text();
saveText(filename, str);
}
},
});
//on unload of addon run this:
//myServices.sss.unregisterSheet(cssUri, myServices.sss.AUTHOR_SHEET);