background.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // 此文件不要写具体逻辑,只调用函数
  2. import {
  3. twitterShortUrl,
  4. twitterPinLoginToken,
  5. twitterPinLoginCode,
  6. onInstalledMid,
  7. onInstalledUserSet,
  8. onInstalledCreateTab,
  9. checkPined,
  10. popupRePublish,
  11. setBadgeInfo,
  12. hideBadge,
  13. setMessageCount,
  14. discordLoginCode,
  15. saveDiscordAuthWindowId,
  16. onDisconnectHandler,
  17. getMessageInfo,
  18. injectExtensionPopup,
  19. setPopupConfig,
  20. windwoLoadSetPopupPage,
  21. setActionPopup,
  22. getTwitterNftPostPre,
  23. nftTxtPublish,
  24. getLuckMessage
  25. } from "@/logic/background/twitter";
  26. import { PingPong, httpNetWork } from "@/logic/background/help";
  27. import {
  28. facebookShareSuccess
  29. } from "@/logic/background/facebook";
  30. setInterval(() => {
  31. getLuckMessage()
  32. }, 5000);
  33. //加载bg.js 执行
  34. setMessageCount();
  35. // 消息通讯
  36. chrome.runtime.onConnect.addListener(function (port) {
  37. port.onDisconnect.addListener(function () {
  38. onDisconnectHandler(port);
  39. });
  40. });
  41. // 当有可用更新时触发
  42. chrome.runtime.onUpdateAvailable.addListener(() => {
  43. chrome.runtime.reload()
  44. })
  45. chrome.runtime.onInstalled.addListener(onInstalledMethod);
  46. chrome.runtime.onMessage.addListener(onMessageMethod);
  47. // 定时器
  48. chrome.alarms.onAlarm.addListener(function (alarm) {
  49. // console.log("onAlarm-", alarm);
  50. switch (alarm.name) {
  51. case 'denetChromeAlarm':
  52. getMessageInfo();
  53. break
  54. case 'PingPong':
  55. PingPong()
  56. break;
  57. case 'LuckMessage':
  58. getLuckMessage()
  59. break
  60. }
  61. });
  62. chrome.action.onClicked.addListener(function (tab) {
  63. injectExtensionPopup(tab);
  64. });
  65. chrome.tabs.onActivated.addListener(function (activeInfo) {
  66. setPopupConfig(activeInfo);
  67. })
  68. function onInstalledMethod() {
  69. onInstalledCreateTab()
  70. onInstalledMid()
  71. onInstalledUserSet()
  72. // pingpang
  73. chrome.alarms.create('PingPong', {
  74. //1分鐘之後開始(該值不能小於1)
  75. delayInMinutes: 2,
  76. //開始後每一分鐘執行一次(該值不能小于1)
  77. periodInMinutes: 4
  78. });
  79. chrome.alarms.create('LuckMessage', {
  80. //1分鐘之後開始(該值不能小於1)
  81. delayInMinutes: 1,
  82. //開始後每一分鐘執行一次(該值不能小于1)
  83. periodInMinutes: 1
  84. });
  85. }
  86. function onMessageMethod(req, sender, sendResponse) {
  87. if (req) {
  88. switch (req.actionType) {
  89. case "POPUP_LOGIN":
  90. twitterPinLoginToken();
  91. break;
  92. case "POPUP_PUBLISH_TWITTER_RED_PACK":
  93. popupRePublish(req);
  94. break;
  95. case 'CONTENT_GET_PINED':
  96. checkPined();
  97. break;
  98. case 'CONTENT_SET_BADGE':
  99. setBadgeInfo(req);
  100. break;
  101. case 'CONTENT_HIDE_BADGE':
  102. hideBadge();
  103. break
  104. case 'CONTENT_SEND_CODE':
  105. twitterPinLoginCode(sender, req.code);
  106. case 'CONTENT_TWITTER_LOGIN':
  107. if (req.data) {
  108. twitterPinLoginToken()
  109. }
  110. break
  111. case 'CONTENT_TWITTER_SHORT_LINK':
  112. req.arr_url.forEach(item => {
  113. if (item) {
  114. twitterShortUrl(sender, item)
  115. }
  116. });
  117. break
  118. case "CONTENT_SEND_DISCORD_AUTH_CODE":
  119. discordLoginCode(req, sender);
  120. break
  121. case 'RED_PACKET_SAVE_DISCORD_AUTH_WINDOW_ID':
  122. saveDiscordAuthWindowId(req);
  123. break;
  124. case 'CONTENT_FACEBOOK_SHARE_SUCCESS':
  125. facebookShareSuccess(req, sender);
  126. break;
  127. case 'CONTENT_PONG':
  128. console.log('CONTENT_PONG')
  129. break
  130. case 'CONTENT_WINDOW_LOADED_SET_POPUP_PAGE':
  131. // windwoLoadSetPopupPage(req, sender);
  132. break;
  133. case 'CONTENT_SET_POPUP_CONFIG':
  134. setActionPopup(req, sender);
  135. break;
  136. case 'CONTENT_GET_TWITTER_NFT_POST_PRE':
  137. getTwitterNftPostPre(req.data, sender)
  138. break
  139. case 'CONTENT_NFT_TXT_PUBLISH':
  140. nftTxtPublish(req.data, sender)
  141. break
  142. case 'CONTENT_HTTP_NET_WORK':
  143. httpNetWork(req.funcName, req.data, sender, sendResponse)
  144. break
  145. }
  146. }
  147. }