firefox-addonjpm

Detect double click in firefox extension button


I need to detect a double click on the button of my extension and open a different website and I need that website to be opened in the current tab.

index.js:

var buttons = require('sdk/ui/button/action');
var tabs = require("sdk/tabs");

var button = buttons.ActionButton({
  id: "mozilla-link",
  label: "Visit Mozilla",
  icon: {
    "16": "./icon-16.png",
    "32": "./icon-32.png",
    "64": "./icon-64.png"
  },
  onClick: handleClick
});

function handleClick(state) {
  tabs.open("http://www.mozilla.org/");
}

Solution

  • I have a similar requirement in one of my extensions. Here's the code I use to determine if the button click is a single or a double:

        var clickCnt = 0;   // Click counter
        var delay = 250;    // Maximum time (milliseconds) between clicks to be considered a double-click
        var timer;
        chrome.browserAction.onClicked.addListener(function(tab){
            clickCnt++; 
            if(clickCnt > 1){
                // Double-click detected
                chrome.tabs.executeScript({
                    code:   '// Code to execute on double-click'
                });
                clickCnt = 0;
                clearTimeout(timer)
            }else{
                timer = setTimeout(function(){  
                    // No clicked detected within (delay)ms, so consider this a single click 
                    chrome.tabs.executeScript({
                        code:   '// Code to execute on single-click'
                    });
                    clickCnt = 0;
                }, delay);
            }
            return true;
        });