I try to build a basic addon which would do that:
- copy the values of a few fields (already filled) on a page (not very important which one for now)
- save the value locally
- paste the saved values on a similar form (same fields with same names etc) on another page (also not very important which page it is for now).
So I thought about using context menus and having
- one item to copy data, which does this:
- collects the value from the page with jQuery
- sends then to the addon script for storage
- one item to paste data
- receives the stored data, sent as data
- fills the empty fields with the data
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);
}
});
varbeforess.storage.storedFormData = JSON.stringify(formData);- Loridatais undefined, so add a line likevar data = require("sdk/self").data;- Lori