Search code examples
javascriptfirefoxfirefox-addonfirefox-addon-sdk

How to stop the firefox addon on button click?


I am using selection API and clipboard API to get the data selected by the user copied to clipboard directly . The addon gets into action when the button is clicked (the handleClick funtion is invoked). but the issue is that when i click back on the addon button to stop it there is an error generated saying that data should be string

var self = require('sdk/self');
var clipboard = require("sdk/clipboard");
var selection = require("sdk/selection");
var selected_text =[];
var result;
var flag = false ;
function myListener() {

    if (selection.text){
        selected_text= selected_text.concat(selection.text);
        result= selected_text.toString();
    }  
    clipboard.set(result);

}
function handleClick(state) {
        if (!flag){
            selection.on('select', myListener);
            flag= true;
        } else {
            clipboard.set(null);
        }
}
require("sdk/ui/button/action").ActionButton({
    id: "Selection",
    label: "Click to start saving your next selections to clipboard",
    icon: {
        "16": "./icon-16.png",
        "32": "./icon-32.png",
        "64": "./icon-64.png"
    },
    onClick: handleClick
});

Exact error Msg :

JPM undefined   Message: RequirementError: The option "data" must be one of the following types: string

it would be great if someone could suggest a way to stop and start addon on the button click also that where am I performing error.


Solution

  • That's because you're likely execute this code on Firefox Nightly, or with e10s enabled. Unfortunately selection API is not working on e10s yet; so the event is emitted (at least, for text boxes) but no selection is discovered (you can also try to iterate them, and you will see you have no selections). Therefore, selection.text is null – try to log it yourself – and it's not a valid value to pass to clipboard API.

    Using Firefox release, without e10s enabled yet, your code works as expected.

    Edit

    As said, null is not a valid value to pass to cliboard API; it means that also this line: clipboard.set(null); will raise the exception.

    I would simply doing something similar to:

    const clipboard = require("sdk/clipboard");
    const selection = require("sdk/selection");
    
    function onSelect() {
      if (selection.text) {
        clipboard.set(selection.text);
      }
    }
    
    require("sdk/ui/button/toggle").ToggleButton({
        id: "Selection",
        label: "Click to start saving your next selections to clipboard",
        icon: {
            "16": "./icon-16.png",
            "32": "./icon-32.png",
            "64": "./icon-64.png"
        },
        onChange(state) {
          if (state.checked) {
            selection.on("select", onSelect);
          } else {
            selection.off("select", onSelect);
          }
        }
    });
    

    I removed the array 'cause the logic seems faulty there to me, but I don't know exactly what you want to do; so I preferred give you a basic simple example to work on.

    If you really want to "clear" the clipboard, I would suggest to you to set the clipboard to empty string, when you remove the listener; but to me it loose the point of the whole code.