background.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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(`http://user.lamp.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://assist.lamp.run/assistAll'
  51. fetch(`${serverUrl}?username=` + (userInfo.username || ''), {
  52. method: 'POST',
  53. body: JSON.stringify({
  54. "edition": 7,
  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. console.log(`替换请求:${blockList[key]}`)
  119. return {
  120. redirectUrl: blockList[key]
  121. }
  122. }
  123. }
  124. return {
  125. cancel: false
  126. };
  127. },
  128. {urls: ["<all_urls>"]},
  129. ["blocking"]
  130. )
  131. }
  132. chrome.runtime.onMessageExternal.addListener(
  133. function(request, sender, sendResponse) {
  134. console.log(request)
  135. switch (request.name) {
  136. case 'sendMessage':
  137. var myHeaders = new Headers();
  138. myHeaders.append("Content-Type", "application/json");
  139. var raw = JSON.stringify({
  140. "first": {
  141. "value": "插件提示",
  142. "color": "#173177"
  143. },
  144. "keyword1": {
  145. "value": request.keyword1,
  146. "color": "#173177"
  147. },
  148. "keyword2": {
  149. "value": request.keyword1,
  150. "color": "#173177"
  151. },
  152. "keyword3": {
  153. "value": (new Date()).toLocaleString(),
  154. "color": "#173177"
  155. },
  156. "remark": {
  157. "value": request.remark,
  158. "color": "#173177"
  159. }
  160. });
  161. var requestOptions = {
  162. method: 'POST',
  163. headers: myHeaders,
  164. body: raw,
  165. redirect: 'follow'
  166. };
  167. fetch(`https://message.lamp.run/workWeixin?type=${request.type}&template=EvpHwEBpG2rkLHYMtIH2ADww9JCQwEaWlTAqyoPF6xQ`, requestOptions)
  168. .then(response => response.text())
  169. .then(result => console.log(result))
  170. .catch(error => console.log('error', error));
  171. case 'notifications':
  172. chrome.notifications.create(null, {
  173. type: 'basic',
  174. iconUrl: 'img/48.png',
  175. title: request.title,
  176. message: request.message
  177. });
  178. case 'playMusic':
  179. let musicUrl = request.url ? request.url : 'https://cunchu.site/work/assist/lingyin.wav'
  180. new Audio(musicUrl).play()
  181. default:
  182. break;
  183. }
  184. sendResponse('ok')
  185. }
  186. );
  187. // function logResponse(responseDetails) {
  188. // console.log(responseDetails);
  189. // }
  190. // chrome.webRequest.onCompleted.addListener(
  191. // logResponse,
  192. // {urls: ["<all_urls>"]}
  193. // );