var g_tolItem = 0; document.addEventListener('DOMContentLoaded', function () { console.log('我被执行了!'); chrome.runtime.sendMessage({ from: "content_scripts" }, function (response) { console.log(response); }); }); 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 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; 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 getUnReadList() { var doc = document.getElementsByClassName('tab-pane active'); 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") { /* 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); }); if (j >= 2) { return j; } } } return 0; } } 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 == "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); 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 + "次尝试获取待阅消息条数"); }); } 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); }); } } );