以编程方式在chrome扩展中打开popup.html?

时间:2014-09-22 22:37:52

标签: javascript google-chrome-extension

我想在点击扩展名时打开popup.html,但是当我双击它时,我想要另一个函数运行。我已经有了代码(见下文)来确定它是否单击/双击。

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

1 个答案:

答案 0 :(得分:-1)

如果你使用popup.html正确定义你的browserAction ...那么它应该弹出正常。但是,您需要在双击的情况下取消单击操作并自行处理:

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
}