background.js 3.9 KB

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