twitter.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. import { fetchTtwitterRequestToken, fetchTwitterLogin, fetchTwitterShortUrl, fetchAllMessageInfo, fetchReadTaskAllMsg, getDiscordUserInfo } from '@/logic/background/fetch/twitter.js'
  2. import { LANDING_PAGE, LANDING_PAGE_MID, setChromeStorage, setChromeCookie, getChromeCookie ,getChromeStorage, removeChromeCookie} from '@/uilts/chromeExtension.js'
  3. import { guid } from '@/uilts/help.js'
  4. import { pageUrl, discordAuthRedirectUri } from '@/http/configAPI'
  5. let authToken = ''
  6. export function twitterPinLoginToken() {
  7. // 1.判断是否登陆了
  8. getChromeStorage('userInfo', (res) => {
  9. // 没有登陆
  10. if (!res) {
  11. fetchTtwitterRequestToken().then((res) => {
  12. authToken = res.data.authToken
  13. chrome.tabs.create({
  14. url: `https://api.twitter.com/oauth/authorize?oauth_token=${res.data.authToken}`
  15. })
  16. })
  17. }
  18. })
  19. }
  20. export function twitterPinLoginCode(port,code) {
  21. port.postMessage({
  22. state: "BACK_TWITTER_LOGIN_SUCCESS",
  23. });
  24. // 关闭code页面
  25. chrome.tabs.query({}, (tab) => {
  26. for (let i in tab) {
  27. console.log(tab[i])
  28. if (tab[i].url == 'https://api.twitter.com/oauth/authorize') {
  29. chrome.tabs.remove(tab[i].id)
  30. }
  31. }
  32. })
  33. chrome.cookies.getAll(LANDING_PAGE, (e = []) => {
  34. let _str = '[]'
  35. if (e.length > 0) {
  36. _str = e[0].value
  37. }
  38. let _arr = JSON.parse(decodeURIComponent(_str))
  39. let receivedIds = []
  40. if (_arr.length > 0) {
  41. for (let i in _arr) {
  42. receivedIds.push(_arr[i].receivedId)
  43. }
  44. }
  45. // 发送请求
  46. // token,code
  47. fetchTwitterLogin(authToken, code, receivedIds).then(res => {
  48. if (res.code == 0) {
  49. setChromeStorage({ userInfo: JSON.stringify(res.data) })
  50. // 获取全局消息数据
  51. setMessageCount()
  52. chrome.cookies.remove(LANDING_PAGE)
  53. }
  54. })
  55. }
  56. )
  57. }
  58. let discordAuthWindowId = '';
  59. export function saveDiscordAuthWindowId (params) {
  60. let {windowId = ''} = params.data || {};
  61. discordAuthWindowId = windowId;
  62. }
  63. export function discordLoginCode({code}) {
  64. if(discordAuthWindowId) {
  65. chrome.windows.remove(
  66. discordAuthWindowId,
  67. function () {
  68. // 发送请求
  69. getDiscordUserInfo({
  70. authCode: code,
  71. redirectUrl: discordAuthRedirectUri
  72. }).then(res => {
  73. if (res.code == 0) {
  74. setTimeout(() => {
  75. sendActivetabMessage({
  76. actionType: 'BACK_DISCORD_LOGIN_SUCCESS'
  77. });
  78. })
  79. }
  80. })
  81. }
  82. )
  83. }
  84. }
  85. export function twitterShortUrl(url) {
  86. return new Promise(function (resolve, reject) {
  87. fetchTwitterShortUrl(url).then(res => {
  88. let _str_arr = res.match(/denetme.net\/([\s\S]*?)"/) || []
  89. let _post_id = _str_arr[1] || ''
  90. console.log('_str_arr_post_id', _post_id)
  91. if (!_post_id) {
  92. return
  93. }
  94. // 解析
  95. let _obj = {
  96. url,
  97. post_id: _post_id
  98. // tweet_id
  99. }
  100. getChromeStorage('sortLink', item => {
  101. if (item) {
  102. for (let i in item) {
  103. if (item[i].url == _obj.url) {
  104. item[i] = _obj
  105. }
  106. // else{
  107. // delete item[i].tweet_id
  108. // }
  109. }
  110. setChromeStorage({ sortLink: JSON.stringify(item) })
  111. } else {
  112. setChromeStorage({ sortLink: JSON.stringify([_obj]) })
  113. }
  114. resolve({
  115. post_id: _post_id
  116. })
  117. })
  118. })
  119. })
  120. }
  121. // 安装插件后获取mid
  122. export function onInstalledMid() {
  123. getChromeCookie(LANDING_PAGE_MID, (res_arr) => {
  124. // 没有cookie
  125. if (res_arr && res_arr.length) {
  126. setChromeStorage({ mid: JSON.stringify(res_arr[0]) })
  127. } else {
  128. let _params = {
  129. mid: guid()
  130. }
  131. setChromeCookie(LANDING_PAGE, { 'mid': _params.mid })
  132. setChromeStorage({ mid: JSON.stringify(_params) })
  133. }
  134. })
  135. }
  136. export function onInstalledUserSet() {
  137. chrome.action.getUserSettings().then(res => {
  138. setChromeStorage({ userSettings: JSON.stringify({ res }) })
  139. // 无刷新插入js
  140. chrome.tabs.query({}, (tab) => {
  141. for (let i in tab) {
  142. if (tab[i].url.indexOf('twitter.com') >= 0) {
  143. chrome.scripting.executeScript({
  144. target: { tabId: tab[i].id },
  145. files: ['js/content.js'],
  146. }, () => {
  147. setTimeout(() => {
  148. setChromeStorage({ executeScript: JSON.stringify({ executeScript: 1 }) })
  149. }, 2000);
  150. })
  151. }
  152. }
  153. })
  154. })
  155. }
  156. /**
  157. * 检查是否pined 显示tips
  158. */
  159. export function checkPined() {
  160. chrome.action.getUserSettings(res => {
  161. let { isOnToolbar } = res;
  162. if (!isOnToolbar) {
  163. sendActivetabMessage({
  164. actionType: 'BG_SHOW_PIN_TIPS'
  165. });
  166. }
  167. })
  168. }
  169. function sendActivetabMessage(message = {}) {
  170. chrome.tabs.query({
  171. active: true,
  172. currentWindow: true
  173. }, (tabs) => {
  174. chrome.tabs.sendMessage(tabs[0].id, message, res => {
  175. console.log(res)
  176. })
  177. })
  178. }
  179. /**
  180. * 安装后打开新标签页
  181. */
  182. export function onInstalledCreateTab() {
  183. let cookiesParams = {
  184. name: 'pickup_info',
  185. url: pageUrl
  186. }
  187. getChromeCookie(cookiesParams, (res) => {
  188. let { postNickName, srcContentId } = res;
  189. if (res && postNickName && srcContentId) {
  190. let url = `https://twitter.com/${postNickName}/status/${srcContentId}`
  191. chrome.tabs.create({
  192. url
  193. });
  194. removeChromeCookie(cookiesParams)
  195. } else {
  196. chrome.tabs.create({
  197. url: "https://twitter.com",
  198. });
  199. }
  200. })
  201. }
  202. /**
  203. * 在popop重新发送
  204. * @param {*} req
  205. */
  206. export function popupRePublish(req) {
  207. setChromeStorage({
  208. popupShowPublishDialog: JSON.stringify({
  209. show: true,
  210. srcContent: req.data.srcContent,
  211. postId: req.data.postId
  212. }),
  213. });
  214. chrome.tabs.create({
  215. url: "https://twitter.com",
  216. });
  217. }
  218. export function setBadgeInfo(params) {
  219. let {text = '', color = '#DF3535'} = params.data || {};
  220. chrome.action.setBadgeText({text: text});
  221. chrome.action.setBadgeBackgroundColor({color: color });
  222. }
  223. export function hideBadge() {
  224. chrome.action.setBadgeText({text: ''});
  225. chrome.action.setBadgeBackgroundColor({color: [0, 0, 0, 0]});
  226. }
  227. export async function setMessageCount () {
  228. const { accessToken: token = '', uid = '' } = await getChromeStorage('userInfo') || {}
  229. if(token) {
  230. getMessageInfo();
  231. setInterval(() => {
  232. getMessageInfo();
  233. }, 60000);
  234. }
  235. }
  236. export function getMessageInfo () {
  237. fetchAllMessageInfo().then(res => {
  238. if(res.code == 0) {
  239. let {unReadCountTotal = 0 } = res.data;
  240. if(unReadCountTotal > 0) {
  241. let text = unReadCountTotal > 99 ? '99+' : unReadCountTotal+'';
  242. setBadgeInfo({data: {text}});
  243. } else {
  244. hideBadge();
  245. }
  246. }
  247. })
  248. }
  249. export function readTaskAllMsg(cb) {
  250. fetchReadTaskAllMsg({
  251. msgType: 1 // 1:任务红包 2:钱包明细
  252. }).then(res => {
  253. if(res.code == 0) {
  254. cb && cb();
  255. }
  256. });
  257. }