background.js 5.5 KB

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