简体   繁体   中英

Firefox addon: Have content script retrieve data stored by addon script (using a context menu if possible)

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);
    }

});

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM