background.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. sendResponse('ok')
  132. if (req) {
  133. switch (req.actionType) {
  134. case "POPUP_LOGIN":
  135. twitterPinLoginToken();
  136. break;
  137. case "POPUP_PUBLISH_TWITTER_RED_PACK":
  138. popupRePublish(req);
  139. break;
  140. case "POPUP_SHOW_DENET_PUBLISH_DIALOG":
  141. checkShowPublishDialog();
  142. break;
  143. case 'CONTENT_GET_PINED':
  144. checkPined();
  145. break;
  146. case 'CONTENT_SET_BADGE':
  147. setBadgeInfo(req);
  148. break;
  149. case 'CONTENT_HIDE_BADGE':
  150. hideBadge();
  151. break
  152. case 'CONTENT_SEND_CODE':
  153. twitterPinLoginCode(sender, req.code);
  154. break;
  155. case 'CONTENT_TWITTER_LOGIN':
  156. if (req.data) {
  157. twitterPinLoginToken()
  158. }
  159. break
  160. case "CONTENT_SEND_DISCORD_AUTH_CODE":
  161. discordLoginCode(req, sender);
  162. break
  163. case 'RED_PACKET_SAVE_DISCORD_AUTH_WINDOW_ID':
  164. saveDiscordAuthWindowId(req);
  165. break;
  166. case 'CONTENT_FACEBOOK_SHARE_SUCCESS':
  167. facebookShareSuccess(req, sender);
  168. break;
  169. case 'CONTENT_PONG':
  170. console.log('CONTENT_PONG')
  171. break
  172. case 'CONTENT_WINDOW_LOADED_SET_POPUP_PAGE':
  173. // windwoLoadSetPopupPage(req, sender);
  174. break;
  175. case 'CONTENT_SET_POPUP_CONFIG':
  176. setActionPopup(req, sender);
  177. break;
  178. case 'CONTENT_GET_TWITTER_NFT_POST_PRE':
  179. getTwitterNftPostPre(req.data, sender)
  180. break
  181. case 'CONTENT_NFT_TXT_PUBLISH':
  182. nftTxtPublish(req.data, sender)
  183. break
  184. case 'CONTENT_HTTP_NET_WORK':
  185. httpNetWork(req.funcName, req.data, sender)
  186. break
  187. case 'HTTP_CONTENT_TO_BACK':
  188. httpContentToBack(req, sender)
  189. break
  190. case 'CONTENT_TWITTER_SHORT_LINK':
  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. }