background.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. getSysMessage,
  25. checkShowPublishDialog
  26. } from "@/logic/background/twitter";
  27. import Report from "@/log-center/log"
  28. import { PingPong, httpNetWork, httpContentToBack } from "@/logic/background/help";
  29. import { appVersionCode } from '@/http/configAPI.js'
  30. import { getChromeStorage, setChromeStorage } from '@/uilts/chromeExtension.js'
  31. import {
  32. facebookShareSuccess
  33. } from "@/logic/background/facebook";
  34. //加载bg.js 执行
  35. setMessageCount();
  36. // 消息通讯
  37. chrome.runtime.onConnect.addListener(function (port) {
  38. port.onDisconnect.addListener(function () {
  39. onDisconnectHandler(port);
  40. });
  41. });
  42. // 当有可用更新时触发
  43. chrome.runtime.onUpdateAvailable.addListener(() => {
  44. try {
  45. Report.reportLog({
  46. objectType: Report.objectType.background_function_try,
  47. funcName: 'onUpdateAvailable'
  48. });
  49. chrome.runtime.reload()
  50. } catch (error) {
  51. Report.reportLog({
  52. objectType: Report.objectType.background_function_catch,
  53. funcName: 'onUpdateAvailable',
  54. errMsg: error.message
  55. });
  56. }
  57. })
  58. chrome.runtime.onInstalled.addListener(onInstalledMethod);
  59. chrome.runtime.onMessage.addListener(onMessageMethod);
  60. // 定时器
  61. chrome.alarms.onAlarm.addListener(function (alarm) {
  62. // console.log("onAlarm-", alarm);
  63. switch (alarm.name) {
  64. case 'denetChromeAlarm':
  65. getMessageInfo();
  66. break
  67. case 'PingPong':
  68. PingPong()
  69. break;
  70. case 'LuckMessage':
  71. getSysMessage()
  72. break
  73. }
  74. });
  75. chrome.action.onClicked.addListener(function (tab) {
  76. injectExtensionPopup(tab);
  77. });
  78. chrome.tabs.onActivated.addListener(function (activeInfo) {
  79. setPopupConfig(activeInfo);
  80. })
  81. function thenInstalledMethod() {
  82. try {
  83. onInstalledCreateTab()
  84. onInstalledMid()
  85. onInstalledUserSet()
  86. // pingpang
  87. chrome.alarms.create('PingPong', {
  88. //1分鐘之後開始(該值不能小於1)
  89. delayInMinutes: 2,
  90. //開始後每一分鐘執行一次(該值不能小于1)
  91. periodInMinutes: 4
  92. });
  93. chrome.alarms.create('LuckMessage', {
  94. //1分鐘之後開始(該值不能小於1)
  95. delayInMinutes: 1,
  96. //開始後每一分鐘執行一次(該值不能小于1)
  97. periodInMinutes: 1
  98. });
  99. setTimeout(() => {
  100. // 安装成功埋点
  101. Report.reportLog({
  102. objectType: Report.objectType.background_function_try,
  103. funcName: 'onInstalledMethod'
  104. });
  105. }, 5000);
  106. } catch (error) {
  107. Report.reportLog({
  108. objectType: Report.objectType.background_function_catch,
  109. funcName: 'onInstalledMethod',
  110. errMsg: error.message
  111. });
  112. }
  113. }
  114. function onInstalledMethod() {
  115. // 版本更新判断
  116. getChromeStorage('baseInfo', (info) => {
  117. if (!info || !info.appVersionCode) {
  118. setChromeStorage({ baseInfo: JSON.stringify({ appVersionCode }) })
  119. thenInstalledMethod()
  120. } else if (appVersionCode != info.appVersionCode) {
  121. setChromeStorage({ baseInfo: JSON.stringify({ appVersionCode }) }, () => {
  122. chrome.runtime.reload()
  123. })
  124. } else {
  125. thenInstalledMethod()
  126. }
  127. })
  128. }
  129. function onMessageMethod(req, sender, sendResponse) {
  130. try {
  131. if (req) {
  132. switch (req.actionType) {
  133. case "POPUP_LOGIN":
  134. twitterPinLoginToken();
  135. break;
  136. case "POPUP_PUBLISH_TWITTER_RED_PACK":
  137. popupRePublish(req);
  138. break;
  139. case "POPUP_SHOW_DENET_PUBLISH_DIALOG":
  140. checkShowPublishDialog();
  141. break;
  142. case 'CONTENT_GET_PINED':
  143. checkPined();
  144. break;
  145. case 'CONTENT_SET_BADGE':
  146. setBadgeInfo(req);
  147. break;
  148. case 'CONTENT_HIDE_BADGE':
  149. hideBadge();
  150. break
  151. case 'CONTENT_SEND_CODE':
  152. twitterPinLoginCode(sender, req.code);
  153. break;
  154. case 'CONTENT_TWITTER_LOGIN':
  155. if (req.data) {
  156. twitterPinLoginToken()
  157. }
  158. break
  159. case "CONTENT_SEND_DISCORD_AUTH_CODE":
  160. discordLoginCode(req, sender);
  161. break
  162. case 'RED_PACKET_SAVE_DISCORD_AUTH_WINDOW_ID':
  163. saveDiscordAuthWindowId(req);
  164. break;
  165. case 'CONTENT_FACEBOOK_SHARE_SUCCESS':
  166. facebookShareSuccess(req, sender);
  167. break;
  168. case 'CONTENT_PONG':
  169. console.log('CONTENT_PONG')
  170. break
  171. case 'CONTENT_WINDOW_LOADED_SET_POPUP_PAGE':
  172. // windwoLoadSetPopupPage(req, sender);
  173. break;
  174. case 'CONTENT_SET_POPUP_CONFIG':
  175. setActionPopup(req, sender);
  176. break;
  177. case 'CONTENT_GET_TWITTER_NFT_POST_PRE':
  178. getTwitterNftPostPre(req.data, sender)
  179. break
  180. case 'CONTENT_NFT_TXT_PUBLISH':
  181. nftTxtPublish(req.data, sender)
  182. break
  183. case 'CONTENT_HTTP_NET_WORK':
  184. httpNetWork(req.funcName, req.data, sender)
  185. break
  186. case 'HTTP_CONTENT_TO_BACK':
  187. httpContentToBack(req, sender)
  188. break
  189. case 'CONTENT_TWITTER_SHORT_LINK':
  190. sendResponse('ok')
  191. req.arr_url.forEach(item => {
  192. if (item) {
  193. twitterShortUrl(sender, item)
  194. }
  195. });
  196. break
  197. }
  198. }
  199. } catch (error) {
  200. Report.reportLog({
  201. objectType: Report.objectType.background_function_catch,
  202. funcName: 'onMessageMethod',
  203. errMsg: error.message
  204. });
  205. }
  206. }