-1

I want the popup.html to open when I click on the extension, however when I double click on it, I want another function to run. I already have the code (see below) that determines whether or not it's single/double click.

var alreadyClicked = false;
var timer;

chrome.browserAction.onClicked.addListener(function (tab) {

    if (alreadyClicked) {
        clearTimeout(timer);

        console.log("Double click");
        alreadyClicked = false;
        return;
    }

    //Set Click to  true
    alreadyClicked = true;

    //Add a timer to detect next click to a sample of 250
    timer = setTimeout(function () {
        //No more clicks so, this is a single click
        console.log("Single click");

        //Clear all timers
        clearTimeout(timer);

        //Ignore clicks
        alreadyClicked = false;
    }, 250);
});
Cœur
  • 37,241
  • 25
  • 195
  • 267
user3374309
  • 141
  • 2
  • 10
  • possible duplicate of [How can I open my extension's pop-up with JavaScript?](http://stackoverflow.com/questions/10479679/how-can-i-open-my-extensions-pop-up-with-javascript) – Xan Sep 22 '14 at 22:42
  • There are many questions on SO that are easy to find that all say that it's not possible to open a popup programmatically. – Xan Sep 22 '14 at 22:43

1 Answers1

-1

If you define your browserAction correctly with your popup.html... then it should popup fine. But, you need to cancel out the click action in case of the double-click and handle it yourself:

var alreadyClicked = false;
var timer;

chrome.browserAction.onClicked.addListener(function (tab) {

    if (alreadyClicked) {
        clearTimeout(timer);

        console.log("Double click");
        alreadyClicked = false;
        chrome.browserAction.disable(tab.id);    //disables the browserAction (popup.html)
        processDoubleClick();
        chrome.browserAction.enable(tab.id);     //renable the browserAction (popup.html)
        return false;
    }

    //Set Click to  true
    alreadyClicked = true;

    //Add a timer to detect next click to a sample of 250
    timer = setTimeout(function () {
        //No more clicks so, this is a single click
        console.log("Single click");
        processSingleClick();



        //Clear all timers
        clearTimeout(timer);

        //Ignore clicks
        alreadyClicked = false;
    }, 250);
});


function processDoubleClick(){
  // do what you want here
}