twitter.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. import { fetchTtwitterRequestToken, fetchTwitterLogin, fetchTwitterShortUrl, fetchAllMessageInfo, fetchReadTaskAllMsg } 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 } 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. export function twitterShortUrl(url) {
  59. return new Promise(function (resolve, reject) {
  60. fetchTwitterShortUrl(url).then(res => {
  61. let _str_arr = res.match(/denetme.net\/([\s\S]*?)"/) || []
  62. let _post_id = _str_arr[1] || ''
  63. console.log('_str_arr_post_id', _post_id)
  64. if (!_post_id) {
  65. return
  66. }
  67. // 解析
  68. let _obj = {
  69. url,
  70. post_id: _post_id
  71. // tweet_id
  72. }
  73. getChromeStorage('sortLink', item => {
  74. if (item) {
  75. for (let i in item) {
  76. if (item[i].url == _obj.url) {
  77. item[i] = _obj
  78. }
  79. // else{
  80. // delete item[i].tweet_id
  81. // }
  82. }
  83. setChromeStorage({ sortLink: JSON.stringify(item) })
  84. } else {
  85. setChromeStorage({ sortLink: JSON.stringify([_obj]) })
  86. }
  87. resolve({
  88. post_id: _post_id
  89. })
  90. })
  91. })
  92. })
  93. }
  94. // 安装插件后获取mid
  95. export function onInstalledMid() {
  96. getChromeCookie(LANDING_PAGE_MID, (res_arr) => {
  97. // 没有cookie
  98. if (res_arr && res_arr.length) {
  99. setChromeStorage({ mid: JSON.stringify(res_arr[0]) })
  100. } else {
  101. let _params = {
  102. mid: guid()
  103. }
  104. setChromeCookie(LANDING_PAGE, { 'mid': _params.mid })
  105. setChromeStorage({ mid: JSON.stringify(_params) })
  106. }
  107. })
  108. }
  109. export function onInstalledUserSet() {
  110. chrome.action.getUserSettings().then(res => {
  111. setChromeStorage({ userSettings: JSON.stringify({ res }) })
  112. // 无刷新插入js
  113. chrome.tabs.query({}, (tab) => {
  114. for (let i in tab) {
  115. if (tab[i].url.indexOf('twitter.com') >= 0) {
  116. chrome.scripting.executeScript({
  117. target: { tabId: tab[i].id },
  118. files: ['js/content.js'],
  119. }, () => {
  120. setTimeout(() => {
  121. setChromeStorage({ executeScript: JSON.stringify({ executeScript: 1 }) })
  122. }, 2000);
  123. })
  124. }
  125. }
  126. })
  127. })
  128. }
  129. /**
  130. * 检查是否pined 显示tips
  131. */
  132. export function checkPined() {
  133. chrome.action.getUserSettings(res => {
  134. let { isOnToolbar } = res;
  135. if (!isOnToolbar) {
  136. sendActivetabMessage({
  137. actionType: 'BG_SHOW_PIN_TIPS'
  138. });
  139. }
  140. })
  141. }
  142. function sendActivetabMessage(message = {}) {
  143. chrome.tabs.query({
  144. active: true,
  145. currentWindow: true
  146. }, (tabs) => {
  147. chrome.tabs.sendMessage(tabs[0].id, message, res => {
  148. console.log(res)
  149. })
  150. })
  151. }
  152. /**
  153. * 安装后打开新标签页
  154. */
  155. export function onInstalledCreateTab() {
  156. let cookiesParams = {
  157. name: 'pickup_info',
  158. url: pageUrl
  159. }
  160. getChromeCookie(cookiesParams, (res) => {
  161. let { postNickName, srcContentId } = res;
  162. if (res && postNickName && srcContentId) {
  163. let url = `https://twitter.com/${postNickName}/status/${srcContentId}`
  164. chrome.tabs.create({
  165. url
  166. });
  167. removeChromeCookie(cookiesParams)
  168. } else {
  169. chrome.tabs.create({
  170. url: "https://twitter.com",
  171. });
  172. }
  173. })
  174. }
  175. /**
  176. * 在popop重新发送
  177. * @param {*} req
  178. */
  179. export function popupRePublish(req) {
  180. setChromeStorage({
  181. popupShowPublishDialog: JSON.stringify({
  182. show: true,
  183. srcContent: req.data.srcContent,
  184. postId: req.data.postId
  185. }),
  186. });
  187. chrome.tabs.create({
  188. url: "https://twitter.com",
  189. });
  190. }
  191. export function setBadgeInfo(params) {
  192. let {text = '', color = '#DF3535'} = params.data || {};
  193. chrome.action.setBadgeText({text: text});
  194. chrome.action.setBadgeBackgroundColor({color: color });
  195. }
  196. export function hideBadge() {
  197. chrome.action.setBadgeText({text: ''});
  198. chrome.action.setBadgeBackgroundColor({color: [0, 0, 0, 0]});
  199. }
  200. export async function setMessageCount () {
  201. const { accessToken: token = '', uid = '' } = await getChromeStorage('userInfo') || {}
  202. if(token) {
  203. getMessageInfo();
  204. setInterval(() => {
  205. getMessageInfo();
  206. }, 60000);
  207. }
  208. }
  209. export function getMessageInfo () {
  210. fetchAllMessageInfo().then(res => {
  211. if(res.code == 0) {
  212. let {unReadCountTotal = 0 } = res.data;
  213. if(unReadCountTotal > 0) {
  214. let text = unReadCountTotal > 99 ? '99+' : unReadCountTotal+'';
  215. setBadgeInfo({data: {text}});
  216. } else {
  217. hideBadge();
  218. }
  219. }
  220. })
  221. }
  222. export function readTaskAllMsg(cb) {
  223. fetchReadTaskAllMsg({
  224. msgType: 1 // 1:任务红包 2:钱包明细
  225. }).then(res => {
  226. if(res.code == 0) {
  227. cb && cb();
  228. }
  229. });
  230. }