background.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. getMessageInfo,
  15. readTaskAllMsg
  16. } from "@/logic/background/twitter";
  17. //加载bg.js 执行
  18. setMessageCount();
  19. // 消息通讯
  20. chrome.runtime.onConnect.addListener(function (port) {
  21. port.onMessage.addListener(function (res) {
  22. switch (res.state) {
  23. case "CONTENT_SEND_CODE":
  24. twitterPinLoginCode(port,res.code);
  25. break;
  26. case "CONTENT_TWITTER_LOGIN":
  27. twitterPinLoginToken();
  28. break;
  29. case "CONTENT_TWITTER_SHORT_LINK":
  30. twitterShortUrl(res.url)
  31. break
  32. }
  33. });
  34. if (port.name === "popup") {
  35. port.onDisconnect.addListener(function() {
  36. readTaskAllMsg(() => {
  37. getMessageInfo();
  38. })
  39. });
  40. }
  41. });
  42. chrome.runtime.onInstalled.addListener(onInstalledMethod);
  43. chrome.runtime.onMessage.addListener(onMessageMethod);
  44. function onInstalledMethod() {
  45. onInstalledCreateTab()
  46. onInstalledMid()
  47. onInstalledUserSet()
  48. }
  49. function onMessageMethod(req, sender, sendResponse) {
  50. sendResponse('');
  51. if (req) {
  52. switch (req.actionType) {
  53. case "POPUP_LOGIN":
  54. twitterPinLoginToken();
  55. break;
  56. case "POPUP_PUBLISH_TWITTER_RED_PACK":
  57. popupRePublish(req);
  58. break;
  59. case 'CONTENT_GET_PINED':
  60. checkPined();
  61. break;
  62. case 'CONTENT_SET_BADGE':
  63. setBadgeInfo(req);
  64. break;
  65. case 'CONTENT_HIDE_BADGE':
  66. hideBadge();
  67. break;
  68. }
  69. }
  70. }