From 32d06134c84b7769ce2d8eb104aa4076585207a9 Mon Sep 17 00:00:00 2001 From: HuangXin Date: Tue, 10 Sep 2019 17:32:17 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E7=89=88=E6=9C=AC=E5=A4=87?= =?UTF-8?q?=E4=BB=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- js/background.js | 90 +++++++++++++++++------ js/oa.js | 188 ++++++++++++++++++++++++++++++----------------- js/popup.js | 19 ++++- js/read.js | 15 ++++ manifest.json | 7 +- popup.html | 4 +- 6 files changed, 226 insertions(+), 97 deletions(-) diff --git a/js/background.js b/js/background.js index 8d2bcfc..7ac27f8 100644 --- a/js/background.js +++ b/js/background.js @@ -1,5 +1,7 @@ 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 () { @@ -16,31 +18,47 @@ chrome.runtime.onInstalled.addListener(function () { setInterval(function () { if (g_tabArray.length > 0) { //console.log("%o", g_tabArray); - for (var i = 0; i < g_tabArray.length; i++) { + 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(g_tabArray[i].tab.id, { active: true }); + chrome.tabs.update(tabId, { 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 + 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]); + //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(g_tabArray[i].tab.id); + 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); }); @@ -51,14 +69,14 @@ chrome.tabs.onRemoved.addListener(function (tabId, removeInfo) { console.log("Remove Page[%d]:", tabId, removeInfo); } - if (g_tabArray.length == 0) { + /* 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) { + 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"); }); }); - } + } */ } }); @@ -66,29 +84,55 @@ 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") { + if (request.action == "bg_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); - } + 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; + } } - } 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 }) + g_tabArray.push({ tab: tb, stamp: new Date().getTime(), status: "new", url: request.url, lastClick: 0 }) console.log("%o", tb); }); - } else if (request.action == "click_table") { - for (var i = 0; i < g_tabArray.length; i++) { + } 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); } - } + } */ } }); \ No newline at end of file diff --git a/js/oa.js b/js/oa.js index a73c00d..cf0ce5e 100644 --- a/js/oa.js +++ b/js/oa.js @@ -1,11 +1,6 @@ -var g_tolItem = 0; - -document.addEventListener('DOMContentLoaded', function () { - console.log('我被执行了!'); - chrome.runtime.sendMessage({ from: "content_scripts" }, function (response) { - console.log(response); - }); -}); +var g_isRun = false; +var g_unReadArray = new Array(); +var g_tolReadFiles = -1; function sleep(time) { return new Promise((resolve) => setTimeout(resolve, time)); @@ -24,14 +19,28 @@ function queryUnReadItems() { return 0; } +function queryUnReadDOM() { + var doc = document.getElementById('mould_2_64'); + var tbl = doc.getElementsByClassName('nav nav-pills'); + + for (var i = 0; i < tbl[0].childElementCount; i++) { + var obj = tbl[0].children[i]; + + if (obj.children[0].innerText == "我的待阅" + && obj.childElementCount > 1 + && parseInt(obj.children[1].innerText, 10) > 0) { + console.log("我的待阅: %d", parseInt(obj.children[1].innerText, 10)); + return parseInt(obj.children[1].innerText, 10); + } + } +} + function flushUnReadList(id, nItems) { var doc = document.getElementById('document-filter'); doc.children[id].firstElementChild.click(); var cob = document.getElementById('listpagination'); var selectObj = cob.children[1].firstElementChild; - console.log("Parent[1] = %o", cob.children[1]); - console.log("Parent[2] = %o", cob.children[1].firstElementChild); var n = 0;// nItems / 10; @@ -49,34 +58,43 @@ function flushUnReadList(id, nItems) { console.log("Refresh Web Pages"); } +function urlParser(url, key) { + var regex = new RegExp(key + "=([^&]*)"); + return url.match(regex)[1]; +} + function getUnReadList() { var doc = document.getElementsByClassName('tab-pane active'); + var count = 0; for (var i = 0; i < doc.length; i++) { - if (doc[i].id == 'toread') { + if (doc[i].id == 'toread') { var tbl = doc[i].children[0].children[0].children[1]; for (var j = 0; j < tbl.childElementCount; j++) { if (tbl.children[j].className != "request") { - /* console.log("item(%d): %s %s", j, - tbl.children[j].children[0].innerText, - tbl.children[j].children[1].firstElementChild.href); */ - //tbl.children[j].children[1].firstElementChild.click(); - chrome.runtime.sendMessage({ - from: "content_scripts", - action: 'create_table', - url: tbl.children[j].children[1].firstElementChild.href - }, function (response) { - g_tolItem = g_tolItem + 1; - console.log(response); + count++; + var url = tbl.children[j].children[1].firstElementChild.href; + var tm = tbl.children[j].children[2].innerText; + var title = tbl.children[j].children[1].innerText; + var type = urlParser(url, "type"); + + var isIn = g_unReadArray.some(v => { + if (v.tm == tm && v.type == type && v.title == title) { + console.log("Find Exist Item: %o", v); + return true; + } }); - if (j >= 2) { - return j; + if (!isIn) { + g_unReadArray.push({ + url: url, tm: tm, title: title, type: type, + createTime: new Date().getTime(), openTime: 0 + }); } } } - return 0; + return count; } } @@ -88,53 +106,85 @@ chrome.extension.onMessage.addListener( var msg = "Recv Commond [" + request.action + "] from (" + request.from + ")"; sendResponse(msg); console.log(msg); - if (request.action == "run_cmd") { - if(g_tolItem >= 4) { - console.log("------------ Cancel do"); - return; - } - for (var i = 0; i < 10; i++) { - var obj = queryUnReadItems(); - if (obj.nItem > 0) { - var msg = "待阅处理: [" + obj.nItem + "] 条"; - console.log(msg); - flushUnReadList(obj.index, obj.nItem); + if (request.action == "cs_stop") { + g_isRun = false; + } else if (request.action == "cs_jump_page") { + var n = queryUnReadDOM(); - sleep(3000).then(() => { - var j = 0, n = 0; - do { - n = getUnReadList(); - console.log("Retry:%d --> %d", j++, n); - } while (n == 0); - - console.log("Finish getUnReadList"); - return; - }); - - return; - } - - sleep(1000).then(() => { - console.log("第" + i + "次尝试获取待阅消息条数"); + if (n > 0) { + chrome.runtime.sendMessage({ from: "content_scripts", to: "background", action: 'bg_ready', tolItems: n }, function (response) { + console.log(response); + }); + } else { + chrome.runtime.sendMessage({ from: "content_scripts", to: "background", action: 'bg_idle' }, function (response) { + console.log(response); }); } - - console.log("没有待阅消息需要处理"); - } else if (request.action == "get_items") { - getUnReadList(); - } else if (request.action == "tab_info") { - chrome.runtime.sendMessage({ from: "content_scripts", to: "background", action: 'run_table' }, function (response) { - console.log(response); - }); - } else if (request.action == "read_page") { - sendResponse(""); - var rd = document.getElementById('toRead'); - rd.click(); - console.log("Click Button:%o", rd); - chrome.runtime.sendMessage({ from: "content_scripts", to: "background", action: 'click_table', tabId: request.tabId }, function (response) { - console.log(response); - }); + } else if (request.action == "cs_get_list") { + g_isRun = true; } } -); \ No newline at end of file +); + +const STEP_GET_UNREADITEMS = 0; +const STEP_GET_UNREADLIST = 1; +const STEP_OPEN_TABLE = 2; + +var g_proStep = STEP_GET_UNREADITEMS; + +document.addEventListener('DOMContentLoaded', function () { + console.log('我被执行了!'); + + setInterval(function () { + if (g_isRun) { + console.log("Current Step: %d", g_proStep); + + switch (g_proStep) { + case STEP_GET_UNREADITEMS: + var nUnRead = queryUnReadItems(); + if (nUnRead.nItem > 0) { + if (g_tolReadFiles == -1) { + g_tolReadFiles = nUnRead.nItem; + chrome.runtime.sendMessage({ from: "content_scripts", to: "background", action: 'bg_ready', tolItems: g_tolReadFiles }, function (response) { + console.log(response) + }); + } + var msg = "待阅处理: [" + nUnRead.nItem + "] 条"; + console.log(msg); + flushUnReadList(nUnRead.index, nUnRead.nItem); + g_proStep = STEP_GET_UNREADLIST; + } + break; + case STEP_GET_UNREADLIST: + var nList = getUnReadList(); + if (nList > 0) { + g_proStep = STEP_OPEN_TABLE; + } + break; + case STEP_OPEN_TABLE: + if (g_unReadArray.length > 0) { + for (var i = 0; i < g_unReadArray.length; i++) { + var tmNow = new Date().getTime(); + if (g_unReadArray[i].openTime == 0) { + g_unReadArray[i].openTime = tmNow; + chrome.runtime.sendMessage({ + from: "content_scripts", to: "background", action: 'bg_create_table', + url: g_unReadArray[i].url + }, function (response) { + console.log(response); + }); + g_unReadArray.splice(i, 1); + } else if (tmNow - g_unReadArray[i].openTime > 60 * 1000) { + g_unReadArray.splice(i, 1); + } + } + } else { + g_proStep = STEP_GET_UNREADITEMS; + } + break; + } + + } + }, 3000); +}); \ No newline at end of file diff --git a/js/popup.js b/js/popup.js index 6426213..efa778f 100644 --- a/js/popup.js +++ b/js/popup.js @@ -34,9 +34,9 @@ var runButton = document.getElementById('checkboxThreeInput'); runButton.addEventListener('click', function () { console.log('debug tab:'); - var cmd = "stop_cmd"; + var cmd = "bg_stop_cmd"; if (runButton.checked) { - cmd = "run_cmd" + cmd = "bg_run_cmd" } chrome.runtime.sendMessage({ from: 'popup', to: 'content_scripts', action: cmd }, function (response) { @@ -71,6 +71,21 @@ window.onload = function () { }, 100); } */ +//var prog = 0; + window.onload = function () { runButton.checked = bg.g_isRunCmd; + /* var tolProgress = document.getElementById('runProgress'); + var prgTimer = setInterval(function () { + tolProgress.style.width = prog + "%"; + console.log("Progress: %d", prog); + + if(bg.g_totalItems > 0) { + prog = Math.floor(bg.g_readItems * 100 / bg.g_totalItems); + } + + if(prog >= 100) { + clearInterval(prgTimer); + } + }, 1000); */ } diff --git a/js/read.js b/js/read.js index e69de29..d4afb6d 100644 --- a/js/read.js +++ b/js/read.js @@ -0,0 +1,15 @@ +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 == "cs_read_page") { + var rd = document.getElementById('toRead'); + rd.click(); + chrome.runtime.sendMessage({ from: "content_scripts", to: "background", action: 'bg_click_table', tabId: request.tabId }, function (response) { + console.log(response); + }); + } + } +); diff --git a/manifest.json b/manifest.json index 148f2da..14a47e3 100644 --- a/manifest.json +++ b/manifest.json @@ -16,9 +16,14 @@ }, "content_scripts": [ { - "matches": ["https://oa.komect.com/oa*"], + "matches": ["https://oa.komect.com/oa/oa*"], "js": ["js/oa.js"], "run_at": "document_start" + }, + { + "matches": ["https://oa.komect.com/oa/document*"], + "js": ["js/read.js"], + "run_at": "document_start" } ], "permissions": [ diff --git a/popup.html b/popup.html index d45bd73..27ef2a5 100644 --- a/popup.html +++ b/popup.html @@ -23,11 +23,11 @@ -