I try to build a basic addon which would do that:
So I thought about using context menus and having
The problem that I have is with data types and having the stored data sent to the content script.
Here is what I have so far:
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]
});
content-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);
}
});
OK, I ran the add-on, after making modifications described in comments. It pasted the first copied formData in a particular session, throughout the session, even if subsequent values were copied. The problem is that the value of the simple storage variable ss
is updated only at start time, and the value
property of menu item pasteItem
is initialized when it is created, which is also at start time. The solution is to replace
var ss.storage.storedFormData = JSON.stringify(formData);
with
pasteItem.data = ss.storage.storedFormData = JSON.stringify(formData);