这是一个简单的扩展程序,在延迟一段时间后发送聊天行。从控制台日志K可以看到弹出窗口,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");
});
background.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);
}