| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- var userInfo = null
- var username = localStorage.getItem('owoUsername')
- var password = localStorage.getItem('owoPassword')
- var tempData = null
- var openList = {}
- if (localStorage.getItem('owoOpenList')) {
- openList = JSON.parse(localStorage.getItem('owoOpenList'))
- }
- function saveOpenList () {
- localStorage.setItem('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 + 1 * 0 * 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) {
- // 拦截请求
- 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: ["<all_urls>"]},
- ["blocking"]
- )
- }
- // function logResponse(responseDetails) {
- // console.log(responseDetails);
- // }
- // chrome.webRequest.onCompleted.addListener(
- // logResponse,
- // {urls: ["<all_urls>"]}
- // );
|