background.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. var userInfo = null
  2. var username = ''
  3. var password = ''
  4. var tempData = null
  5. var openList = {}
  6. chrome.storage.local.get(["owoOpenList"], (result) => {
  7. if (result.owoOpenList) {
  8. openList = JSON.parse(result.owoOpenList)
  9. }
  10. });
  11. function saveOpenList () {
  12. chrome.storage.local.set({owoOpenList: JSON.stringify(openList)})
  13. }
  14. if (username && password) {
  15. fetch(`http://going.run/userServer?route=login`, {
  16. method: 'POST',
  17. headers: {
  18. "Content-Type": "application/json"
  19. },
  20. body: JSON.stringify({
  21. type: "assist",
  22. username: username,
  23. password: password
  24. })
  25. }).then((response) => {return response.json();}).then((res) => {
  26. if (res.err === 0) {
  27. userInfo = res.data
  28. }
  29. })
  30. }
  31. // 对数据进行处理
  32. function clearData (data) {
  33. let returnData = []
  34. data.forEach(element => {
  35. if (openList[element.id] || element.type == 'autoRun') {
  36. returnData.push(element)
  37. }
  38. })
  39. return returnData
  40. }
  41. // 监听消息
  42. chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
  43. switch (message.name) {
  44. case 'getData':
  45. const nowTime = Date.parse(new Date())
  46. // 缓存
  47. if (tempData && (tempData.time + 60 * 60 * 1000) > nowTime) {
  48. // console.log('使用缓存返回!', tempData)
  49. sendResponse(clearData(tempData.data))
  50. } else {
  51. const serverUrl = 'https://going.run/assistAll'
  52. fetch(`${serverUrl}?route=search`, {
  53. method: 'POST',
  54. body: JSON.stringify({
  55. "edition": 5,
  56. "url": message.url
  57. }),
  58. redirect: 'follow'
  59. }).then(data => data.json()).then(dataTemp => {
  60. tempData = {
  61. time: nowTime,
  62. data: dataTemp
  63. }
  64. sendResponse(clearData(dataTemp))
  65. })
  66. }
  67. break;
  68. case 'clear':
  69. tempData = null
  70. fetch(`http://cunchu.site/data/blockList.json`).then(data => data.json()).then(dataTemp => {
  71. blockListTemp = dataTemp
  72. lanjie(dataTemp)
  73. sendResponse()
  74. })
  75. default:
  76. break;
  77. }
  78. return true
  79. })
  80. var blockListTemp = null
  81. if (blockListTemp != null) {
  82. lanjie(blockListTemp)
  83. } else {
  84. fetch(`http://cunchu.site/data/blockList.json`).then(data => data.json()).then(dataTemp => {
  85. blockListTemp = dataTemp
  86. lanjie(dataTemp)
  87. })
  88. }
  89. function lanjie (blockList) {
  90. // 待修复
  91. return
  92. // 拦截请求
  93. chrome.webRequest.onBeforeRequest.addListener(
  94. function(details) {
  95. for (const key in blockList) {
  96. if (details.url.includes(key) || new RegExp(key).test(details.url)) {
  97. // console.log(details.url)
  98. return {
  99. redirectUrl: blockList[key]
  100. }
  101. }
  102. }
  103. return {
  104. cancel: false
  105. };
  106. },
  107. {urls: ["<all_urls>"]},
  108. ["blocking"]
  109. )
  110. }
  111. // function logResponse(responseDetails) {
  112. // console.log(responseDetails);
  113. // }
  114. // chrome.webRequest.onCompleted.addListener(
  115. // logResponse,
  116. // {urls: ["<all_urls>"]}
  117. // );