background.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. var userInfo = null
  2. var username = localStorage.getItem('owoUsername')
  3. var password = localStorage.getItem('owoPassword')
  4. var tempData = null
  5. var openList = {}
  6. function saveUser (username, password) {
  7. localStorage.setItem('owoUsername', username)
  8. localStorage.setItem('owoPassword', password)
  9. }
  10. if (username && password) {
  11. fetch(`http://going.run/userServer?route=login`, {
  12. method: 'POST',
  13. headers: {
  14. "Content-Type": "application/json"
  15. },
  16. body: JSON.stringify({
  17. type: "assist",
  18. username: username,
  19. password: password
  20. })
  21. }).then((response) => {return response.json();}).then((res) => {
  22. if (res.err === 0) {
  23. userInfo = res.data
  24. }
  25. })
  26. }
  27. // 对数据进行处理
  28. function clearData (data) {
  29. let returnData = []
  30. data.forEach(element => {
  31. if (openList[element.id]) {
  32. returnData.push(element)
  33. }
  34. })
  35. return returnData
  36. }
  37. // 监听消息
  38. chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
  39. switch (message.name) {
  40. case 'getData':
  41. const nowTime = Date.parse(new Date())
  42. if (tempData && (tempData.time + 1 * 60 * 1000) > nowTime) {
  43. console.log('使用缓存返回!', tempData)
  44. sendResponse(clearData(tempData.data))
  45. } else {
  46. const serverUrl = 'https://going.run/assistAll'
  47. fetch(`${serverUrl}?route=search`, {
  48. method: 'POST',
  49. body: JSON.stringify({
  50. "edition": 2,
  51. "url": message.url
  52. }),
  53. redirect: 'follow'
  54. }).then(data => data.json()).then(dataTemp => {
  55. tempData = {
  56. time: nowTime,
  57. data: dataTemp
  58. }
  59. sendResponse(clearData(dataTemp))
  60. })
  61. }
  62. break;
  63. default:
  64. break;
  65. }
  66. })