var g_isRun = false; var g_unReadArray = new Array(); var g_tolReadFiles = -1; function sleep(time) { return new Promise((resolve) => setTimeout(resolve, time)); } function queryUnReadItems() { var doc = document.getElementById('document-filter'); for (var i = 0; i < doc.childElementCount; i++) { var ctx = doc.children[i].innerText.split(" "); if (ctx.length > 1 && ctx[0] == "我的待阅") { return { index: i, nItem: parseInt(ctx[1], 10) }; } } 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; var n = 0;// nItems / 10; if (n == 2) { selectObj.value = "20"; } else if (n == 3) { selectObj.value = "30"; } else if (n >= 4) { selectObj.value = "40"; } else { selectObj.value = "10"; } selectObj.dispatchEvent(new Event('change', { 'bubbles': true })); 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') { var tbl = doc[i].children[0].children[0].children[1]; for (var j = 0; j < tbl.childElementCount; j++) { if (tbl.children[j].className != "request") { 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 (!isIn) { g_unReadArray.push({ url: url, tm: tm, title: title, type: type, createTime: new Date().getTime(), openTime: 0 }); } } } return count; } } return 0; } 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_stop") { g_isRun = false; } else if (request.action == "cs_jump_page") { var n = queryUnReadDOM(); 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); }); } } else if (request.action == "cs_get_list") { g_isRun = true; } } ); 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; } else { g_isRun = false; } 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); });