var userInfo = null var username = '' var password = '' var tempData = null var openList = {} chrome.storage.local.get(["owoOpenList"], (result) => { if (result.owoOpenList) { openList = JSON.parse(result.owoOpenList) } }); function saveOpenList () { chrome.storage.local.set({owoOpenList: JSON.stringify(openList)}) } if (username && password) { fetch(`http://going.run/userServer?route=login`, { method: 'POST', headers: { "Content-Type": "application/json" }, body: JSON.stringify({ type: "assist", username: username, password: password }) }).then((response) => {return response.json();}).then((res) => { if (res.err === 0) { userInfo = res.data } }) } // 对数据进行处理 function clearData (data) { let returnData = [] data.forEach(element => { if (openList[element.id] || element.type == 'autoRun') { returnData.push(element) } }) return returnData } // 监听消息 chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) { switch (message.name) { case 'getData': const nowTime = Date.parse(new Date()) // 缓存 if (tempData && (tempData.time + 60 * 60 * 1000) > nowTime) { // console.log('使用缓存返回!', tempData) sendResponse(clearData(tempData.data)) } else { const serverUrl = 'https://going.run/assistAll' fetch(`${serverUrl}?route=search`, { method: 'POST', body: JSON.stringify({ "edition": 5, "url": message.url }), redirect: 'follow' }).then(data => data.json()).then(dataTemp => { tempData = { time: nowTime, data: dataTemp } sendResponse(clearData(dataTemp)) }) } break; case 'clear': tempData = null fetch(`http://cunchu.site/data/blockList.json`).then(data => data.json()).then(dataTemp => { blockListTemp = dataTemp lanjie(dataTemp) sendResponse() }) default: break; } return true }) var blockListTemp = null if (blockListTemp != null) { lanjie(blockListTemp) } else { fetch(`http://cunchu.site/data/blockList.json`).then(data => data.json()).then(dataTemp => { blockListTemp = dataTemp lanjie(dataTemp) }) } function lanjie (blockList) { // 待修复 return // 拦截请求 chrome.webRequest.onBeforeRequest.addListener( function(details) { for (const key in blockList) { if (details.url.includes(key) || new RegExp(key).test(details.url)) { // console.log(details.url) return { redirectUrl: blockList[key] } } } return { cancel: false }; }, {urls: [""]}, ["blocking"] ) } // function logResponse(responseDetails) { // console.log(responseDetails); // } // chrome.webRequest.onCompleted.addListener( // logResponse, // {urls: [""]} // );