background.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // 此文件不要写具体逻辑,只调用函数
  2. import { pageUrl } from '@/http/configAPI'
  3. import {
  4. twitterShortUrl,
  5. twitterPinLoginToken,
  6. twitterPinLoginCode,
  7. onInstalledMid,
  8. onInstalledUserSet
  9. } from "@/logic/background/twitter";
  10. import { setChromeStorage, getChromeCookie , removeChromeCookie } from "@/uilts/chromeExtension";
  11. chrome.runtime.onInstalled.addListener(onInstalledMethod);
  12. chrome.runtime.onMessage.addListener(onMessageMethod);
  13. function onInstalledMethod() {
  14. let cookiesParams = {
  15. name: 'pickup_info',
  16. url: pageUrl
  17. }
  18. getChromeCookie(cookiesParams, (res) => {
  19. let { postNickName, srcContentId } = res;
  20. if (res && postNickName && srcContentId) {
  21. let url = `https://twitter.com/${postNickName}/status/${srcContentId}`
  22. chrome.tabs.create({
  23. url
  24. });
  25. removeChromeCookie(cookiesParams)
  26. } else {
  27. chrome.tabs.create({
  28. url: "https://twitter.com",
  29. });
  30. }
  31. })
  32. onInstalledMid()
  33. onInstalledUserSet()
  34. }
  35. function onMessageMethod(req, sender, sendResponse) {
  36. if (req) {
  37. switch (req.method) {
  38. case "POPUP_LOGIN":
  39. popupLogin(sendResponse);
  40. break;
  41. case "POPUP_PUBLISH_TWITTER_RED_PACK":
  42. setChromeStorage({
  43. popupShowPublishDialog: JSON.stringify({
  44. show: true,
  45. srcContent: req.data.srcContent,
  46. postId: req.data.postId
  47. }),
  48. });
  49. chrome.tabs.create({
  50. url: "https://twitter.com",
  51. });
  52. break;
  53. }
  54. }
  55. return true; // remove this line to make the call sync!
  56. }
  57. //
  58. function popupLogin() {
  59. twitterPinLoginToken();
  60. }
  61. // 消息通讯
  62. chrome.runtime.onConnect.addListener(function (port) {
  63. port.onMessage.addListener(function (res) {
  64. switch (res.state) {
  65. case "CONTENT_SEND_CODE":
  66. twitterPinLoginCode(port,res.code);
  67. break;
  68. case "CONTENT_TWITTER_LOGIN":
  69. twitterPinLoginToken();
  70. break;
  71. case "CONTENT_TWITTER_SHORT_LINK":
  72. twitterShortUrl(res.url)
  73. break
  74. }
  75. });
  76. });
  77. chrome.runtime.onMessage.addListener((req, sender, sendResponse) => {
  78. sendResponse('');
  79. switch (req.actionType) {
  80. case 'CONTENT_GET_PINED':
  81. chrome.action.getUserSettings(res => {
  82. let { isOnToolbar } = res;
  83. console.log('isOnToolbar', isOnToolbar)
  84. if (!isOnToolbar) {
  85. sendActivetabMessage({
  86. actionType: 'BG_SHOW_PIN_TIPS'
  87. });
  88. }
  89. })
  90. break;
  91. }
  92. })
  93. function sendActivetabMessage(message = {}) {
  94. chrome.tabs.query({
  95. active: true,
  96. currentWindow: true
  97. }, (tabs) => {
  98. chrome.tabs.sendMessage(tabs[0].id, message, res => {
  99. console.log(res)
  100. })
  101. })
  102. }