var g_tabArray = new Array(); var g_isRunCmd = false; 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; i++) { if (g_tabArray[i].status == "new") { chrome.tabs.update(g_tabArray[i].tab.id, { active: true }); g_tabArray[i].status = "opened"; } var now = new Date().getTime(); if (g_tabArray[i].lastClick == 0 || now - g_tabArray[i].stamp > 5 * 1000) { chrome.tabs.sendMessage(g_tabArray[i].tab.id, { from: 'background', to: 'content_scripts', action: 'read_page', tabId: g_tabArray[i].tab.id }, function (response) { console.log(response); }); g_tabArray[i].lastClick = now; } console.log("%o", g_tabArray[i]); if (now - g_tabArray[i].stamp > 120 * 1000) { console.log("timeout: %d, %d", now, g_tabArray[i].stamp); chrome.tabs.remove(g_tabArray[i].tab.id); g_tabArray.splice(i, 1); } } } }, 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: 'run_cmd' }, 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 == "run_cmd") { g_isRunCmd = true; } else if (request.action == "stop_cmd") { g_isRunCmd = false; } else if (request.action == "run_table") { for (var i = 0; i < 10; i++) { chrome.tabs.query({ status: "complete", url: "https://oa.komect.com/oa/document*" }, function (tabs) { if (tabs.length > 0) { console.log("%o", tabs); } }); } } else if (request.action == "create_table") { chrome.tabs.create({ url: request.url, active: false }, function (tb) { g_tabArray.push({ tab: tb, stamp: new Date().getTime(), status: "new", lastClick: 0 }) console.log("%o", tb); }); } else if (request.action == "click_table") { 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); } } } });