0

这是一个简单的扩展程序,可在延迟一段时间后发送聊天线路。从控制台日志 K 可以看到 popups,js 应该执行了 chrom.tabs.executeScript 语句,但是通道没有打开。

控制台日志:

14:19:41 试图连接 popup.js:26
14:19:41 在 PM 窗口中运行脚本 popup.js:24
端口错误:无法建立连接。接收端不存在。miscellaneous_bindings:235
未捕获的错误:尝试使用断开连接的端口对象 miscellaneous_bindings:59

清单文件

{
  "manifest_version": 2,

  "name": "autoChat",
  "description": "This extension demonstrates an auto chat",
  "version": "2.0",
  "content_scripts": [
    {
      "matches": ["http://www.myfreecams.com/mfc2/static/pm.html*"],
      "js": ["popup.js"]
    }
  ],
  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
  },
  "permissions": [
    "tabs",
    "http://www.myfreecams.com/mfc2/static/pm.html*"
  ]
}

popup.js

// JavaScript source code
var urlPM = 'http://www.myfreecams.com/mfc2/static/pm.html';
var urlTab;
var domPM;
var tabId;
var lines = [];
var port;
var portStatus;
var x;
var timeId;
var curDate;
var curTime;

function autoChat() {    
    chrome.windows.getAll({ populate: true }, function (windows) {
        for (var w = 0; w < windows.length; w++) {
          for (var t = 0; t < windows[w].tabs.length; t++) {
            urlTab = windows[w].tabs[t].url;
            if (urlTab.substring(0, urlPM.length) != urlPM)
              continue;
            else {
                 tabId = windows[w].tabs[t].id;
                 chrome.tabs.executeScript(tabId, { file: chrome.extension.getURL("background.js") }, function () {
                        console.log(getTime() + " running script in PM window");
                    });
                    console.log(getTime() + " trying to connect");
                    setTimeout(function () { makeConnection() }, 30000);
                }

            }
        }
    }
    );
}

function makeConnection() {
    port = chrome.tabs.connect(tabId, ({ name: "chat" }));
    port.postMessage({ connect: "Can you hear me now?" });
    x = 0;
    setInterval(function () {
        if (x < lines.length)
            port.postMessage(lines[x++]);
    }, 20000)
}

function getTime() {
    var curDate = new Date();
    curTime = curDate.getHours() + ":"
                + curDate.getMinutes() + ":"
                + curDate.getSeconds();
    return (curTime);
}

    document.addEventListener('DOMContentLoaded', function () {
        document.getElementById('Button1').addEventListener('click', autoChat);
        var str = document.getElementById('Chat').value;
        lines = str.split("\n");
    });

背景.js

 // JavaScript source code
var port;
var curDate;
var curTime;

try {
    chrome.runtime.onConnect.addListener(function (port) {
       console.log(getTime() + " " + port);
       port.onMessage.addListener(function (msg) {
           if (msg.connect == "Can you hear me now?")
               console.log(getTime() + " " + "I heard you");
           else
               console.log(getTime() + " " + msg.line);
            document.getElementById("message_input").value = msg.line;
            var buttons = document.getElementsByTagName('button');
            for (var i = 0; i < buttons.Length; i++) {
                if (buttons[i].value == "Send")
                    alert("clicking")'
                    buttons[i].click();
            }
        });
    });
}
catch(err)
{
    txt="There was an error on this page.\n\n";
    txt+="Error description: " + err.message + "\n\n";
    console.log(txt);
}
function getTime() {
    var curDate = new Date();
    curTime = curDate.getHours() + ":"
                + curDate.getMinutes() + ":"
                + curDate.getSeconds();
    return (curTime);
}
4

0 回答 0