background.js 2.8 KB

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