初步完成数据交流.

This commit is contained in:
eson
2020-11-18 19:49:46 +08:00
parent e9cda54cb8
commit 93b9303ee1
14 changed files with 425 additions and 99 deletions

View File

@@ -1,94 +1,58 @@
var href = window.location.href;
var content = document.documentElement.innerHTML;
if (href.startsWith("https://playerduo.com") && content.startsWith('<head></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">')) {
SendContent();
chrome.runtime.onMessage.addListener(function (request) {
switch(request.type) {
case InjectMsgType.WAIT:
setTimeout(function(){
GetTask();
}, 6000);
break;
case InjectMsgType.NOTASK:
setTimeout(function(){
GetTask();
}, 4000);
break;
case InjectMsgType.FETCH:
setTimeout(function(){
GetTask();
}, 6000)
break;
default:
break;
}
// chrome.runtime.onMessage.removeListener(assignTextToTextareas); //optional
});
if(typeof chrome.app.isInstalled != undefined){
var href = window.location.href;
var content = document.documentElement.innerHTML;
if (href.startsWith("https://playerduo.com") && content.startsWith('<head></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">')) {
// SendContent();
Tell(BackgroundMsgType.CONTENT, content);
} else {
// NotWant()
Tell(BackgroundMsgType.NOTWANT, content)
}
} else {
GetTask()
console.log(chrome.app);
}
function SendContent() {
chrome.runtime.sendMessage({ type: BackgroundMsgType.CONTENT, content: content },
function (response) {
console.log(response);
GetTask();
})
function Tell(backgroundType, content) {
if(content == undefined) {
chrome.runtime.sendMessage({type: backgroundType});
} else {
chrome.runtime.sendMessage({type: backgroundType, content: content});
}
}
function GetTask() {
chrome.runtime.sendMessage({ type: BackgroundMsgType.GETTASK }, function (response) {
switch(response.type) {
case InjectMsgType.WAIT:
setTimeout(function(){
GetTask();
}, 1000);
default:
setTimeout(function(){
GetTask();
}, 6000)
}
})
Tell(BackgroundMsgType.GETTASK);
}
// var reset = {
// timeout: setTimeout(function () {
// chrome.runtime.sendMessage({ type: BackgroundMsgType.TIMEOUT }, function (response) {
// console.log(response);
// })
// GetTask(1000);
// }, 7000),
// isCancel: false,
// }
// function GetTask(timeout) {
// chrome.runtime.sendMessage({ type: BackgroundMsgType.GETTASK }, function (response) {
// console.log("GetTask:", response.type, response);
// switch (response.type) {
// case InjectMsgType.WAIT:
// if (!reset.isCancel) {
// clearTimeout(reset.timeout);
// reset.isCancel = true;
// }
// setTimeout(function () {
// GetTask()
// }, timeout) // 递归获取请求
// case InjectMsgType.FETCH:
// }
// })
// }
// var href = window.location.href;
// if (href.startsWith("https://playerduo.com")) {
// var content = document.documentElement.innerHTML;
// if (content.startsWith('<head></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">')) {
// chrome.runtime.sendMessage({ type: BackgroundMsgType.CONTENT, content: content },
// function (response) {
// console.log(response.type, response);
// if (response.type === BackgroundMsgType.GETTASK) {
// GetTask(1000); // 立即获取任务. 如果没有就等待
// };
// })
// }
// }
// chrome.runtime.onMessage.addListener(function (request) {
// if (request.type === BackgroundMsgType.CONTENT) {
// }
// // chrome.runtime.onMessage.removeListener(assignTextToTextareas); //optional
// });