我尝试构建一个基本的插件,它会这样做:
所以我想到了使用上下文菜单和
我遇到的问题是数据类型并将存储的数据发送到内容脚本。
这是我到目前为止所做的:
main.js
var cm = require("sdk/context-menu");
var ss = require("sdk/simple-storage");
// The following gave me a 'Message: SyntaxError: missing ; before statement'
// So I guess I cannot set the stored data like this to be reachable all over
// the addon script...
// var ss.storage.storedFormData = null;
var copyItem = cm.Item({
label: "copy",
data: null
});
// Then here I have 'data is not defined'
var pasteItem = cm.Item({
label: "paste",
data: ss.storage.storedFormData
});
var searchMenu = cm.Menu({
label: "Choose what you want to do",
contentScriptFile: [
data.url('jquery-1.11.2.min.js'),
data.url('content-script.js')
],
onMessage: function (formData) {
console.log('Storing formData');
var ss.storage.storedFormData = JSON.stringify(formData);
},
items: [copyItem, pasteItem]
});
内容的script.js
self.on("click", function (node, data) {
if (data === null) {
// 'data' is null = get data from page
var formData = new Object();
// Get elements on page
formData.element1 = $('input#elementId1').val();
formData.element2 = $('input#elementId2').val();
formData.element3 = $('input#elementId3').val();
// Send data to addon script to be stored
self.postMessage(formData);
} else {
// 'data' is not null, populate the page with data
// Retrieve the data
formData = JSON.parse(data);
// Fill the fields with the data
$('input#elementId1').val(formData.element1);
$('input#elementId2').val(formData.element2);
$('input#elementId3').val(formData.element3);
}
});
答案 0 :(得分:2)
好的,我在评论中描述的修改后运行了附加组件。它会在整个会话期间将第一个复制的formData粘贴到特定会话中,即使后续值已被复制也是如此。问题是简单存储变量ss
的值仅在开始时更新,并且菜单项value
的{{1}}属性在创建时被初始化,这也是在开始时间。解决方案是替换
pasteItem
与
var ss.storage.storedFormData = JSON.stringify(formData);