在Firefox中未完成browser.tabs.query

时间:2018-10-22 20:44:24

标签: javascript firefox-addon

我创建了一个firefox扩展,其中包含一个内容脚本,即带有以下代码:

function onGridClick(e) {
    if (logToConsole) console.log("Grid icon got clicked:" + e.type);
    let id = e.target.parentNode.id;
    if (logToConsole) console.log("Search engine clicked:" + id);
    let nav = document.getElementById("cs-grid");
    nav.style.display = "none";
    nav.removeEventListener("click", onGridClick);
    nav.removeEventListener("mouseleave", onLeave);
    nav = null;
    // Get the tab position of the active tab in the current window
    let tabIndex = 0;
    browser.tabs.query({active:true}).then(function (tab){
        if (logToConsole) console.log(tab);
        tabIndex = tab.index;
        sendMessage("doSearch", {"id": id, "index": tabIndex});
    }, onError);
}

完整的内容脚本可以在github here上找到。

前两个console.logs的执行没有任何问题,但browser.tabs.query之后的第三个console.log从未输出到控制台。我尝试用browser.tabs.getCurrent替换browser.tabs.query,但无济于事!

sendMessage是一个将消息发送到后台脚本的函数。

我在这里做错什么了吗?

1 个答案:

答案 0 :(得分:0)

我找到了答案here

内容脚本只能使用有限数量的WebExtension API,而选项卡不是其中之一!

此信息适用于Firefox,但是我在适用于Chrome的Stackoverflow上发现了this helpful link