AutoReadFile/js/background.js

143 lines
5.8 KiB
JavaScript

var g_tabArray = new Array();
var g_isRunCmd = false;
var g_totalItems = 0;
var g_readItems = 0;
chrome.runtime.onInstalled.addListener(function () {
chrome.declarativeContent.onPageChanged.removeRules(undefined, function () {
chrome.declarativeContent.onPageChanged.addRules([
{
conditions: [
new chrome.declarativeContent.PageStateMatcher({ pageUrl: { urlContains: 'oa.komect.com' } })
],
actions: [new chrome.declarativeContent.ShowPageAction()]
}
]);
});
setInterval(function () {
if (g_tabArray.length > 0) {
//console.log("%o", g_tabArray);
for (var i = 0; i < g_tabArray.length && g_isRunCmd; i++) {
var tabId = g_tabArray[i].tab.id;
if (g_tabArray[i].status == "new") {
chrome.tabs.update(tabId, { active: true });
g_tabArray[i].status = "opened";
}
var now = new Date().getTime();
//console.log("++++++++++++++++ Process Page %d", i);
if (g_tabArray[i].lastClick == 0 || now - g_tabArray[i].stamp > 5 * 1000) {
//console.log("++++++++++++++++ Send Read Page");
chrome.tabs.sendMessage(tabId, {
from: 'background', to: 'content_scripts', action: 'cs_read_page', tabId: tabId
}, function (response) {
console.log(response);
});
g_tabArray[i].lastClick = now;
}
//console.log("%o", g_tabArray[i]);
/* if(g_tabArray[i].lastClick > g_tabArray[i].stamp && now - g_tabArray[i].lastClick > 3 * 1000) {
chrome.tabs.remove(tabId, function(){
console.log("Close Table: %d", tabId);
});
} */
if (now - g_tabArray[i].stamp > 120 * 1000) {
console.log("timeout: %d, %d", now, g_tabArray[i].stamp);
chrome.tabs.remove(tabId);
g_tabArray.splice(i, 1);
}
}
}
/*
if(g_readItems >= g_totalItems && g_tabArray.length == 0 && g_readItems != 0) {
g_isRunCmd = false;
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
chrome.tabs.sendMessage(tabs[0].id, { from: 'background', to: 'content_scripts', action: 'cs_stop' }, function (response) {
console.log(response);
});
});
}*/
}, 1000);
});
chrome.tabs.onRemoved.addListener(function (tabId, removeInfo) {
for (var i = 0; i < g_tabArray.length; i++) {
if (g_tabArray[i].tab.id == tabId) {
g_tabArray.splice(i, 1);
console.log("Remove Page[%d]:", tabId, removeInfo);
}
/* if (g_tabArray.length == 0) {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
chrome.tabs.sendMessage(tabs[0].id, { from: 'background', to: 'content_scripts', action: 'cs_get_list' }, function (response) {
console.log(response);
console.log("++++++++++++++++++++++ restart next process");
});
});
} */
}
});
chrome.extension.onMessage.addListener(function (request, sender, sendResponse) {
var msg = "Recv Commond [" + request.action + "] from (" + request.from + ")";
sendResponse(msg);
console.log(msg);
if (request.action == "bg_run_cmd") {
g_isRunCmd = true;
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
chrome.tabs.sendMessage(tabs[0].id, { from: 'background', to: 'content_scripts', action: 'cs_get_list' }, function (response) {
console.log(response);
});
});
} else if (request.action == "bg_idle") {
} else if (request.action == "bg_ready") {
g_totalItems = request.tolItems;
/* chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
chrome.tabs.sendMessage(tabs[0].id, { from: 'background', to: 'content_scripts', action: 'cs_get_list' }, function (response) {
console.log(response);
});
}); */
} else if (request.action == "bg_stop_cmd") {
g_isRunCmd = false;
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
chrome.tabs.sendMessage(tabs[0].id, { from: 'background', to: 'content_scripts', action: 'cs_stop' }, function (response) {
console.log(response);
});
});
} else if (request.action == "bg_create_table") {
for (var i = 0; i < g_tabArray.length; i++) {
if (g_tabArray[i].url == request.url) {
return;
}
}
chrome.tabs.create({ url: request.url, active: false }, function (tb) {
g_tabArray.push({ tab: tb, stamp: new Date().getTime(), status: "new", url: request.url, lastClick: 0 })
console.log("%o", tb);
console.log("Create %d tables", g_tabArray.length);
});
} else if (request.action == "bg_click_table") {
g_tabArray.some(v => {
if (v.tab.id == request.tabId) {
g_readItems++;
v.lastClick = new Date().getTime();
console.log("Find Exist Item: %o", v);
return true;
}
});
/* for (var i = 0; i < g_tabArray.length; i++) {
if (g_tabArray[i].tab.id == request.tabId) {
g_tabArray[i].lastClick = new Date().getTime();
console.log("Tab %d Click On %d", tabId, g_tabArray[i].lastClick);
}
} */
}
});