Имам два скрипта за съдържание:
- annotationEditor (панел)
- editField (pageMod)
Добавката първо иска от потребителя да въведе текст в изскачащ панел, създаден от annotationEditor. Това работи перфектно. Текстът се връща в променливата annotationText
. annotationText
се съхранява като глобалната променлива savedText
, така че editField да има достъп до нея.
След това искам editField да вземе savedText
и да направи нещо с него (да го добави към поле за въвеждане). Проблемът е, че имам, че събитието pageMod onAttach се извиква, когато страницата се зареди, а не след "текстът е запазен". Също така не мога да намеря други събития за PageMod
Модул. Така че worker.port.emit("editel", [this.annotationAnchor, savedText])
също не работи.
var annotationEditor = panels.Panel({
width: 220,
height: 220,
contentURL: data.url('editor/annotation-editor.html'),
contentScriptFile: data.url('editor/annotation-editor.js'),
onMessage: function(annotationText) {
if (annotationText) {
// handleNewAnnotation(annotationText, this.annotationAnchor);
savedText = annotationText;
annotationEditor.hide();
}
annotationEditor.hide();
},
onShow: function() {
this.postMessage('focus');
}
});
var editField = pageMod.PageMod({
include: ['*'],
contentScriptWhen: 'ready',
contentScriptFile: [data.url('jquery-1.8.3.min.js'), data.url('edit.js')],
onAttach: function(worker) {
worker.port.emit("editel", [this.annotationAnchor, savedText]);
}
});
Опитайте 2: Това по-близо ли е? Изглежда, че все още не работи правилно. Не получавам никакви съобщения в скрипта за съдържание edit.js
.
const tabs = require('sdk/tabs');
var editors = [];
function getEditor(tab) {
for (var i = editors.length; i--;)
if (editors[i].tab===tab) return editors[i];
}
var annotationEditor = panels.Panel({
width: 220,
height: 220,
contentURL: data.url('editor/annotation-editor.html'),
contentScriptFile: data.url('editor/annotation-editor.js'),
onMessage: function(annotationText) {
if (annotationText) {
var currentEditor = getEditor(tabs.activeTab);
console.log(currentEditor)
currentEditor.port.emit("editel",[this.annotationAnchor, annotationText]);
annotationEditor.hide();
}
annotationEditor.hide();
},
onShow: function() {
this.postMessage('focus');
}
});
var editField = pageMod.PageMod({
include: ['*'],
contentScriptWhen: 'ready',
contentScriptFile: [data.url('jquery-1.8.3.min.js'),
data.url('edit.js')],
onAttach: function(worker) {
editors.push(worker);
worker.on('detach', function () {
detachWorker(this, selectors);
});
}
});
Това регистрира currentEditor
като:
constructor {"contentScriptFile":["resource://jid1-kpzxxdxlwq8qkg-at-jetpack/widget/data/jquery-1.8.3.min.js","resource://jid1-kpzxxdxlwq8qkg-at-jetpack/widget/data/edit.js"],"contentScript":null,"port":{}}
Правилно ли изглежда?
Тук е и скриптът за съдържание - edit.js
. Очевидно в момента това не прави много. Първата стъпка е да го накарате да улови съобщението от main.js
.
self.port.on("editEl", function(el) {
$(el[1]).val(el[0]);
});