开发者问题收集

后台页面中 chrome.tab.query 出现“TypeError:无法读取未定义的属性‘query’”

2016-08-26
4249

我正在使用此代码获取所有打开的选项卡 URL:

function setPhone(phone) {
    chrome.tabs.query({
    }, function (tabs) {
        var tabURL = tabs[0].url;
        console.log(tabURL);
    });
}

这是开始监听 TCP 套接字的扩展的一部分。建立连接并收到命令时,将调用 setPhone 函数。该函数在事件中调用:

function onAcceptCallback(tcpConnection, socketInfo) {
    var info = "[" + socketInfo.peerAddress + ":" + socketInfo.peerPort + "] Connection accepted!";
    log.output(info);
    console.log(socketInfo);
    tcpConnection.addDataReceivedListener(function (data) {
        var lines = data.split(/[\n\r]+/);
        for (var i = 0; i < lines.length; i++) {
            var line = lines[i];
            if (line.length > 0) {
                var info = "[" + socketInfo.peerAddress + ":" + socketInfo.peerPort + "] " + line;
                log.output(info);

                var cmd = line.split(/\s+/);
                try {
                    switch (cmd[0]) {
                        case 'Phone':
                            setPhone(cmd[1]);
                            tcpConnection.sendMessage('+OK-Phone');
                            break;
                        case 'SetClientPage':
                            clientEditPage = cmd[1];
                            tcpConnection.sendMessage('+OK-SetClientPage');
                            break;
                        case 'SetSearchPage':
                            clientSearchPage = cmd[1];
                            tcpConnection.sendMessage('+OK-SetSearchPage');
                            break;
                        default:
                            tcpConnection.sendMessage('+ERR-Comando No Encontrado');
                    }
                    //tcpConnection.sendMessage(Commands.run(cmd[0], cmd.slice(1)));
                } catch (ex) {
                    tcpConnection.sendMessage(ex);
                }
            }
        }
    });
};

最后,这是清单文件:

{
  "manifest_version": 2,

  "name": "Busca Cliente Lipigas",
  "description": "Esta extension permite recibir un numero de telefono desde el servidor DDE y realizar la busqueda del cliente en la pagina Web de Lipigas.",
  "version": "1.0",

  "permissions": [
    "tabs",
    "background",
    "system.network"
  ],

  "app": {
    "background": {
      "scripts": ["tcp-server.js", "main.js"],
      "persistent": true
    }
  },

  "sockets": {
    "tcpServer": {
      "listen": "*"
    }
  }
}

以上所有都产生错误:TypeError:无法读取未定义的属性“查询”

编辑:

为了测试,我在启动扩展时放置了查询方法,但出现相同的错误:

chrome.app.runtime.onLaunched.addListener(function () {

    chrome.tabs.query({
    }, function (tabs) {
        var tabURL = tabs[0].url;
        console.log(tabURL);
    });


    console.log('Aplicación iniciada.');
    startServer('127.0.0.1', 8888);
});

有什么帮助吗? Cheers Jaime

1个回答

您正在编写 chrome 应用,但 chrome.tabs.* api 只能通过 chrome 扩展程序访问。这就是您收到错误的原因。

Haibara Ai
2016-08-27