background.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. var userInfo = {}
  2. var tempData = null
  3. var openList = {}
  4. if (localStorage.getItem('owoOpenList')) {
  5. openList = JSON.parse(localStorage.getItem('owoOpenList'))
  6. }
  7. function saveOpenList () {
  8. localStorage.setItem('owoOpenList', JSON.stringify(openList))
  9. }
  10. function reloadUser (callBack) {
  11. chrome.storage.sync.get('userInfo', function(data) {
  12. // alert(data)
  13. userInfoTemp = data.userInfo
  14. if (!userInfoTemp) return
  15. username = userInfoTemp.username
  16. password = userInfoTemp.password
  17. if (username && password) {
  18. fetch(`https://user.hanshu.run/login`, {
  19. method: 'POST',
  20. headers: {
  21. "Content-Type": "application/json"
  22. },
  23. body: JSON.stringify({
  24. type: "assist",
  25. username: username,
  26. password: password
  27. })
  28. }).then((response) => {return response.json();}).then((res) => {
  29. if (res.err === 0) {
  30. userInfo = res.data
  31. if (callBack) callBack()
  32. }
  33. })
  34. }
  35. })
  36. }
  37. reloadUser()
  38. // 对数据进行处理
  39. function clearData (data) {
  40. let returnData = []
  41. data.forEach(element => {
  42. if (openList[element.id] || element.type == 'autoRun') {
  43. returnData.push(element)
  44. }
  45. })
  46. return returnData
  47. }
  48. function reGetData (url, callBack) {
  49. const nowTime = Date.parse(new Date())
  50. const serverUrl = 'https://going.run/assistAll'
  51. fetch(`${serverUrl}?route=search&username=` + (userInfo.username || ''), {
  52. method: 'POST',
  53. body: JSON.stringify({
  54. "edition": 5,
  55. "url": url
  56. }),
  57. redirect: 'follow'
  58. }).then(data => data.json()).then(dataTemp => {
  59. tempData = {
  60. time: nowTime,
  61. data: dataTemp
  62. }
  63. if (callBack) callBack(dataTemp)
  64. })
  65. }
  66. // 监听消息
  67. chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
  68. console.log(message)
  69. switch (message.name) {
  70. case 'getData':
  71. const nowTime = Date.parse(new Date())
  72. // 缓存
  73. if (tempData && (tempData.time + 60 * 60 * 1000) > nowTime) {
  74. // console.log('使用缓存返回!', tempData)
  75. sendResponse(clearData(tempData.data))
  76. } else {
  77. reGetData(message.url, (dataTemp) => {
  78. sendResponse(clearData(dataTemp))
  79. })
  80. }
  81. break;
  82. case 'clear':
  83. tempData = null
  84. fetch(`http://cunchu.site/data/blockList.json`).then(data => data.json()).then(dataTemp => {
  85. blockListTemp = dataTemp
  86. chrome.storage.sync.set({userInfo: {}})
  87. userInfo = {}
  88. lanjie(dataTemp)
  89. sendResponse()
  90. })
  91. case 'reloadUser':
  92. setTimeout(() => {
  93. reloadUser(() => {
  94. reGetData('')
  95. })
  96. }, 100);
  97. default:
  98. break;
  99. }
  100. return true
  101. })
  102. var blockListTemp = null
  103. if (blockListTemp != null) {
  104. lanjie(blockListTemp)
  105. } else {
  106. fetch(`http://cunchu.site/data/blockList.json`).then(data => data.json()).then(dataTemp => {
  107. blockListTemp = dataTemp
  108. lanjie(dataTemp)
  109. })
  110. }
  111. function lanjie (blockList) {
  112. // 拦截请求
  113. chrome.webRequest.onBeforeRequest.addListener(
  114. function(details) {
  115. for (const key in blockList) {
  116. if (details.url.includes(key) || new RegExp(key).test(details.url) || details.url == key) {
  117. // console.log(details.url)
  118. return {
  119. redirectUrl: blockList[key]
  120. }
  121. }
  122. }
  123. return {
  124. cancel: false
  125. };
  126. },
  127. {urls: ["<all_urls>"]},
  128. ["blocking"]
  129. )
  130. }
  131. chrome.runtime.onMessageExternal.addListener(
  132. function(request, sender, sendResponse) {
  133. sendResponse('ok')
  134. switch (request.name) {
  135. case 'sendMessage':
  136. var myHeaders = new Headers();
  137. myHeaders.append("Content-Type", "application/json");
  138. var raw = JSON.stringify({
  139. "first": {
  140. "value": "插件提示",
  141. "color": "#173177"
  142. },
  143. "keyword1": {
  144. "value": request.keyword1,
  145. "color": "#173177"
  146. },
  147. "keyword2": {
  148. "value": request.keyword1,
  149. "color": "#173177"
  150. },
  151. "keyword3": {
  152. "value": (new Date()).toLocaleString(),
  153. "color": "#173177"
  154. },
  155. "remark": {
  156. "value": request.remark,
  157. "color": "#173177"
  158. }
  159. });
  160. var requestOptions = {
  161. method: 'POST',
  162. headers: myHeaders,
  163. body: raw,
  164. redirect: 'follow'
  165. };
  166. fetch(`https://hanshu.run/gzh?type=${request.type}&template=EvpHwEBpG2rkLHYMtIH2ADww9JCQwEaWlTAqyoPF6xQ`, requestOptions)
  167. .then(response => response.text())
  168. .then(result => console.log(result))
  169. .catch(error => console.log('error', error));
  170. default:
  171. break;
  172. }
  173. }
  174. );
  175. // function logResponse(responseDetails) {
  176. // console.log(responseDetails);
  177. // }
  178. // chrome.webRequest.onCompleted.addListener(
  179. // logResponse,
  180. // {urls: ["<all_urls>"]}
  181. // );