background.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. console.log("hello world background todo something~");
  2. import { pageUrl } from '@/http/configAPI'
  3. import {
  4. backTwitterPinLoginToken,
  5. backTwitterPinLoginCode,
  6. backHttpTwitterShortUrl
  7. } from "../logic/twitter.js";
  8. import { setChromeStorage, getChromeCookie, LANDING_PAGE, setChromeCookie, removeChromeCookie, LANDING_PAGE_MIND } from "@/uilts/chromeExtension";
  9. import { guid } from '@/uilts/help.js'
  10. chrome.runtime.onInstalled.addListener(onInstalledMethod);
  11. chrome.runtime.onMessage.addListener(onMessageMethod);
  12. function onInstalledMethod() {
  13. let cookiesParams = {
  14. name: 'pickup_info',
  15. url: pageUrl
  16. }
  17. getChromeCookie(cookiesParams, (res) => {
  18. let { postNickName, srcContentId } = res;
  19. if (res && postNickName && srcContentId) {
  20. let url = `https://twitter.com/${postNickName}/status/${srcContentId}`
  21. chrome.tabs.create({
  22. url
  23. });
  24. removeChromeCookie(cookiesParams)
  25. } else {
  26. chrome.tabs.create({
  27. url: "https://twitter.com",
  28. });
  29. }
  30. })
  31. getChromeCookie(LANDING_PAGE_MIND, (res_arr) => {
  32. // 没有cookie
  33. if (res_arr && res_arr.length) {
  34. setChromeStorage({ mid: JSON.stringify(res_arr[0]) })
  35. } else {
  36. let _params = {
  37. mid: guid()
  38. }
  39. setChromeCookie(LANDING_PAGE, { 'mid': _params.mid })
  40. setChromeStorage({ mid: JSON.stringify(_params) })
  41. }
  42. })
  43. chrome.action.getUserSettings().then(res => {
  44. setChromeStorage({ userSettings: JSON.stringify({ res }) })
  45. // 无刷新插入js
  46. chrome.tabs.query({}, (tab) => {
  47. for (let i in tab) {
  48. if (tab[i].url.indexOf('twitter.com') >= 0) {
  49. chrome.scripting.executeScript({
  50. target: { tabId: tab[i].id },
  51. files: ['js/content.js'],
  52. }, () => {
  53. setTimeout(() => {
  54. setChromeStorage({ executeScript: JSON.stringify({ executeScript: 1 }) })
  55. }, 2000);
  56. })
  57. }
  58. }
  59. })
  60. })
  61. }
  62. function onMessageMethod(req, sender, sendResponse) {
  63. if (req) {
  64. switch (req.method) {
  65. case "POPUP_LOGIN":
  66. popupLogin(sendResponse);
  67. break;
  68. case "POPUP_PUBLISH_TWITTER_RED_PACK":
  69. setChromeStorage({
  70. popupShowPublishDialog: JSON.stringify({
  71. show: true,
  72. srcContent: req.data.srcContent,
  73. postId: req.data.postId
  74. }),
  75. });
  76. chrome.tabs.create({
  77. url: "https://twitter.com",
  78. });
  79. break;
  80. }
  81. }
  82. return true; // remove this line to make the call sync!
  83. }
  84. //
  85. function popupLogin() {
  86. backTwitterPinLoginToken();
  87. }
  88. // 消息通讯
  89. chrome.runtime.onConnect.addListener(function (port) {
  90. port.onMessage.addListener(function (res) {
  91. switch (res.state) {
  92. case "CONTENT_SEND_CODE":
  93. port.postMessage({
  94. state: "BACK_TWITTER_LOGIN_SUCCESS",
  95. });
  96. backTwitterPinLoginCode(res.code);
  97. break;
  98. case "CONTENT_TWITTER_LOGIN":
  99. backTwitterPinLoginToken();
  100. break;
  101. case "CONTENT_TWITTER_SHORT_LINK":
  102. backHttpTwitterShortUrl(res.url,res.tweet_id).then((item) => {
  103. // port.postMessage({
  104. // state: "BACK_TWITTER_SHORT_LINK",
  105. // post_id: item.post_id,
  106. // tweet_id: res.tweet_id
  107. // });
  108. })
  109. break
  110. }
  111. });
  112. });
  113. chrome.runtime.onMessage.addListener((req, sender, sendResponse) => {
  114. sendResponse('');
  115. switch (req.actionType) {
  116. case 'CONTENT_GET_PINED':
  117. chrome.action.getUserSettings(res => {
  118. let { isOnToolbar } = res;
  119. console.log('isOnToolbar', isOnToolbar)
  120. if (!isOnToolbar) {
  121. sendActivetabMessage({
  122. actionType: 'BG_SHOW_PIN_TIPS'
  123. });
  124. }
  125. })
  126. break;
  127. }
  128. })
  129. function sendActivetabMessage(message = {}) {
  130. chrome.tabs.query({
  131. active: true,
  132. currentWindow: true
  133. }, (tabs) => {
  134. chrome.tabs.sendMessage(tabs[0].id, message, res => {
  135. console.log(res)
  136. })
  137. })
  138. }