初始版本备份

This commit is contained in:
HuangXin 2019-09-10 17:32:17 +08:00
parent 079c8a86da
commit 32d06134c8
6 changed files with 226 additions and 97 deletions

View File

@ -1,5 +1,7 @@
var g_tabArray = new Array(); var g_tabArray = new Array();
var g_isRunCmd = false; var g_isRunCmd = false;
var g_totalItems = 0;
var g_readItems = 0;
chrome.runtime.onInstalled.addListener(function () { chrome.runtime.onInstalled.addListener(function () {
chrome.declarativeContent.onPageChanged.removeRules(undefined, function () { chrome.declarativeContent.onPageChanged.removeRules(undefined, function () {
@ -16,31 +18,47 @@ chrome.runtime.onInstalled.addListener(function () {
setInterval(function () { setInterval(function () {
if (g_tabArray.length > 0) { if (g_tabArray.length > 0) {
//console.log("%o", g_tabArray); //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") { 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"; g_tabArray[i].status = "opened";
} }
var now = new Date().getTime(); var now = new Date().getTime();
if (g_tabArray[i].lastClick == 0 || now - g_tabArray[i].stamp > 5 * 1000) { if (g_tabArray[i].lastClick == 0 || now - g_tabArray[i].stamp > 5 * 1000) {
chrome.tabs.sendMessage(g_tabArray[i].tab.id, { chrome.tabs.sendMessage(tabId, {
from: 'background', to: 'content_scripts', action: 'read_page', tabId: g_tabArray[i].tab.id from: 'background', to: 'content_scripts', action: 'cs_read_page', tabId: tabId
}, function (response) { }, function (response) {
console.log(response); console.log(response);
}); });
g_tabArray[i].lastClick = now; 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) { if (now - g_tabArray[i].stamp > 120 * 1000) {
console.log("timeout: %d, %d", now, g_tabArray[i].stamp); 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); 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); }, 1000);
}); });
@ -51,14 +69,14 @@ chrome.tabs.onRemoved.addListener(function (tabId, removeInfo) {
console.log("Remove Page[%d]:", 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.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(response);
console.log("++++++++++++++++++++++ restart next process"); 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 + ")"; var msg = "Recv Commond [" + request.action + "] from (" + request.from + ")";
sendResponse(msg); sendResponse(msg);
console.log(msg); console.log(msg);
if (request.action == "run_cmd") { if (request.action == "bg_run_cmd") {
g_isRunCmd = true; g_isRunCmd = true;
} else if (request.action == "stop_cmd") { chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
g_isRunCmd = false; chrome.tabs.sendMessage(tabs[0].id, { from: 'background', to: 'content_scripts', action: 'cs_get_list' }, function (response) {
} else if (request.action == "run_table") { console.log(response);
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 == "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) { 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); console.log("%o", tb);
}); });
} else if (request.action == "click_table") { } else if (request.action == "bg_click_table") {
for (var i = 0; i < g_tabArray.length; i++) {
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) { if (g_tabArray[i].tab.id == request.tabId) {
g_tabArray[i].lastClick = new Date().getTime(); g_tabArray[i].lastClick = new Date().getTime();
console.log("Tab %d Click On %d", tabId, g_tabArray[i].lastClick); console.log("Tab %d Click On %d", tabId, g_tabArray[i].lastClick);
} }
} } */
} }
}); });

188
js/oa.js
View File

@ -1,11 +1,6 @@
var g_tolItem = 0; var g_isRun = false;
var g_unReadArray = new Array();
document.addEventListener('DOMContentLoaded', function () { var g_tolReadFiles = -1;
console.log('我被执行了!');
chrome.runtime.sendMessage({ from: "content_scripts" }, function (response) {
console.log(response);
});
});
function sleep(time) { function sleep(time) {
return new Promise((resolve) => setTimeout(resolve, time)); return new Promise((resolve) => setTimeout(resolve, time));
@ -24,14 +19,28 @@ function queryUnReadItems() {
return 0; 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) { function flushUnReadList(id, nItems) {
var doc = document.getElementById('document-filter'); var doc = document.getElementById('document-filter');
doc.children[id].firstElementChild.click(); doc.children[id].firstElementChild.click();
var cob = document.getElementById('listpagination'); var cob = document.getElementById('listpagination');
var selectObj = cob.children[1].firstElementChild; 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; var n = 0;// nItems / 10;
@ -49,34 +58,43 @@ function flushUnReadList(id, nItems) {
console.log("Refresh Web Pages"); console.log("Refresh Web Pages");
} }
function urlParser(url, key) {
var regex = new RegExp(key + "=([^&]*)");
return url.match(regex)[1];
}
function getUnReadList() { function getUnReadList() {
var doc = document.getElementsByClassName('tab-pane active'); var doc = document.getElementsByClassName('tab-pane active');
var count = 0;
for (var i = 0; i < doc.length; i++) { 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]; var tbl = doc[i].children[0].children[0].children[1];
for (var j = 0; j < tbl.childElementCount; j++) { for (var j = 0; j < tbl.childElementCount; j++) {
if (tbl.children[j].className != "request") { if (tbl.children[j].className != "request") {
/* console.log("item(%d): %s %s", j, count++;
tbl.children[j].children[0].innerText, var url = tbl.children[j].children[1].firstElementChild.href;
tbl.children[j].children[1].firstElementChild.href); */ var tm = tbl.children[j].children[2].innerText;
//tbl.children[j].children[1].firstElementChild.click(); var title = tbl.children[j].children[1].innerText;
chrome.runtime.sendMessage({ var type = urlParser(url, "type");
from: "content_scripts",
action: 'create_table', var isIn = g_unReadArray.some(v => {
url: tbl.children[j].children[1].firstElementChild.href if (v.tm == tm && v.type == type && v.title == title) {
}, function (response) { console.log("Find Exist Item: %o", v);
g_tolItem = g_tolItem + 1; return true;
console.log(response); }
}); });
if (j >= 2) { if (!isIn) {
return j; 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 + ")"; var msg = "Recv Commond [" + request.action + "] from (" + request.from + ")";
sendResponse(msg); sendResponse(msg);
console.log(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) { if (request.action == "cs_stop") {
var msg = "待阅处理: [" + obj.nItem + "] 条"; g_isRun = false;
console.log(msg); } else if (request.action == "cs_jump_page") {
flushUnReadList(obj.index, obj.nItem); var n = queryUnReadDOM();
sleep(3000).then(() => { if (n > 0) {
var j = 0, n = 0; chrome.runtime.sendMessage({ from: "content_scripts", to: "background", action: 'bg_ready', tolItems: n }, function (response) {
do { console.log(response);
n = getUnReadList(); });
console.log("Retry:%d --> %d", j++, n); } else {
} while (n == 0); chrome.runtime.sendMessage({ from: "content_scripts", to: "background", action: 'bg_idle' }, function (response) {
console.log(response);
console.log("Finish getUnReadList");
return;
});
return;
}
sleep(1000).then(() => {
console.log("第" + i + "次尝试获取待阅消息条数");
}); });
} }
} else if (request.action == "cs_get_list") {
console.log("没有待阅消息需要处理"); g_isRun = true;
} 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);
});
} }
} }
); );
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);
});

View File

@ -34,9 +34,9 @@ var runButton = document.getElementById('checkboxThreeInput');
runButton.addEventListener('click', function () { runButton.addEventListener('click', function () {
console.log('debug tab:'); console.log('debug tab:');
var cmd = "stop_cmd"; var cmd = "bg_stop_cmd";
if (runButton.checked) { if (runButton.checked) {
cmd = "run_cmd" cmd = "bg_run_cmd"
} }
chrome.runtime.sendMessage({ from: 'popup', to: 'content_scripts', action: cmd }, function (response) { chrome.runtime.sendMessage({ from: 'popup', to: 'content_scripts', action: cmd }, function (response) {
@ -71,6 +71,21 @@ window.onload = function () {
}, 100); }, 100);
} */ } */
//var prog = 0;
window.onload = function () { window.onload = function () {
runButton.checked = bg.g_isRunCmd; 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); */
} }

View File

@ -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);
});
}
}
);

View File

@ -16,9 +16,14 @@
}, },
"content_scripts": [ "content_scripts": [
{ {
"matches": ["https://oa.komect.com/oa*"], "matches": ["https://oa.komect.com/oa/oa*"],
"js": ["js/oa.js"], "js": ["js/oa.js"],
"run_at": "document_start" "run_at": "document_start"
},
{
"matches": ["https://oa.komect.com/oa/document*"],
"js": ["js/read.js"],
"run_at": "document_start"
} }
], ],
"permissions": [ "permissions": [

View File

@ -23,11 +23,11 @@
<input type="checkbox" value="1" id="checkboxThreeInput" name="" /> <input type="checkbox" value="1" id="checkboxThreeInput" name="" />
<label for="checkboxThreeInput"></label> <label for="checkboxThreeInput"></label>
</div> </div>
<ul id="skill"> <!-- <ul id="skill">
<li><span class="bar graphic-design" id="runProgress"></span> <li><span class="bar graphic-design" id="runProgress"></span>
<h3>Graphic Design</h3> <h3>Graphic Design</h3>
</li> </li>
</ul> </ul> -->
<script src='js/popup.js'></script> <script src='js/popup.js'></script>
</div> </div>
</div> </div>