background.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. if (tempData && (tempData.time + 1 * 0 * 1000) > nowTime) {
  45. console.log('使用缓存返回!', tempData)
  46. sendResponse(clearData(tempData.data))
  47. } else {
  48. const serverUrl = 'https://going.run/assistAll'
  49. fetch(`${serverUrl}?route=search`, {
  50. method: 'POST',
  51. body: JSON.stringify({
  52. "edition": 5,
  53. "url": message.url
  54. }),
  55. redirect: 'follow'
  56. }).then(data => data.json()).then(dataTemp => {
  57. tempData = {
  58. time: nowTime,
  59. data: dataTemp
  60. }
  61. sendResponse(clearData(dataTemp))
  62. })
  63. }
  64. break;
  65. case 'clear':
  66. tempData = null
  67. fetch(`http://cunchu.site/data/blockList.json`).then(data => data.json()).then(dataTemp => {
  68. blockListTemp = dataTemp
  69. lanjie(dataTemp)
  70. sendResponse()
  71. })
  72. default:
  73. break;
  74. }
  75. return true
  76. })
  77. var blockListTemp = null
  78. if (blockListTemp != null) {
  79. lanjie(blockListTemp)
  80. } else {
  81. fetch(`http://cunchu.site/data/blockList.json`).then(data => data.json()).then(dataTemp => {
  82. blockListTemp = dataTemp
  83. lanjie(dataTemp)
  84. })
  85. }
  86. function lanjie (blockList) {
  87. // 拦截请求
  88. chrome.webRequest.onBeforeRequest.addListener(
  89. function(details) {
  90. for (const key in blockList) {
  91. if (details.url.includes(key) || new RegExp(key).test(details.url)) {
  92. console.log(details.url)
  93. return {
  94. redirectUrl: blockList[key]
  95. }
  96. }
  97. }
  98. return {
  99. cancel: false
  100. };
  101. },
  102. {urls: ["<all_urls>"]},
  103. ["blocking"]
  104. )
  105. }
  106. // function logResponse(responseDetails) {
  107. // console.log(responseDetails);
  108. // }
  109. // chrome.webRequest.onCompleted.addListener(
  110. // logResponse,
  111. // {urls: ["<all_urls>"]}
  112. // );