background.js 3.0 KB

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