123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- import { fetchTtwitterRequestToken, fetchTwitterLogin, fetchTwitterShortUrl, fetchAllMessageInfo, fetchReadTaskAllMsg } from '@/logic/background/fetch/twitter.js'
- import { LANDING_PAGE, LANDING_PAGE_MID, setChromeStorage, setChromeCookie, getChromeCookie ,getChromeStorage, removeChromeCookie} from '@/uilts/chromeExtension.js'
- import { guid } from '@/uilts/help.js'
- import { pageUrl } from '@/http/configAPI'
- let authToken = ''
- export function twitterPinLoginToken() {
- // 1.判断是否登陆了
- getChromeStorage('userInfo', (res) => {
- // 没有登陆
- if (!res) {
- fetchTtwitterRequestToken().then((res) => {
- authToken = res.data.authToken
- chrome.tabs.create({
- url: `https://api.twitter.com/oauth/authorize?oauth_token=${res.data.authToken}`
- })
- })
- }
- })
- }
- export function twitterPinLoginCode(port,code) {
- port.postMessage({
- state: "BACK_TWITTER_LOGIN_SUCCESS",
- });
- // 关闭code页面
- chrome.tabs.query({}, (tab) => {
- for (let i in tab) {
- console.log(tab[i])
- if (tab[i].url == 'https://api.twitter.com/oauth/authorize') {
- chrome.tabs.remove(tab[i].id)
- }
- }
- })
- chrome.cookies.getAll(LANDING_PAGE, (e = []) => {
- let _str = '[]'
- if (e.length > 0) {
- _str = e[0].value
- }
- let _arr = JSON.parse(decodeURIComponent(_str))
- let receivedIds = []
- if (_arr.length > 0) {
- for (let i in _arr) {
- receivedIds.push(_arr[i].receivedId)
- }
- }
- // 发送请求
- // token,code
- fetchTwitterLogin(authToken, code, receivedIds).then(res => {
- if (res.code == 0) {
- setChromeStorage({ userInfo: JSON.stringify(res.data) })
- // 获取全局消息数据
- setMessageCount()
- chrome.cookies.remove(LANDING_PAGE)
- }
- })
- }
- )
- }
- export function twitterShortUrl(url) {
- return new Promise(function (resolve, reject) {
- fetchTwitterShortUrl(url).then(res => {
- let _str_arr = res.match(/denetme.net\/([\s\S]*?)"/) || []
- let _post_id = _str_arr[1] || ''
- console.log('_str_arr_post_id', _post_id)
- if (!_post_id) {
- return
- }
- // 解析
- let _obj = {
- url,
- post_id: _post_id
- // tweet_id
- }
- getChromeStorage('sortLink', item => {
- if (item) {
- for (let i in item) {
- if (item[i].url == _obj.url) {
- item[i] = _obj
- }
- // else{
- // delete item[i].tweet_id
- // }
- }
- setChromeStorage({ sortLink: JSON.stringify(item) })
- } else {
- setChromeStorage({ sortLink: JSON.stringify([_obj]) })
- }
- resolve({
- post_id: _post_id
- })
- })
- })
- })
- }
- // 安装插件后获取mid
- export function onInstalledMid() {
- getChromeCookie(LANDING_PAGE_MID, (res_arr) => {
- // 没有cookie
- if (res_arr && res_arr.length) {
- setChromeStorage({ mid: JSON.stringify(res_arr[0]) })
- } else {
- let _params = {
- mid: guid()
- }
- setChromeCookie(LANDING_PAGE, { 'mid': _params.mid })
- setChromeStorage({ mid: JSON.stringify(_params) })
- }
- })
- }
- export function onInstalledUserSet() {
- chrome.action.getUserSettings().then(res => {
- setChromeStorage({ userSettings: JSON.stringify({ res }) })
- // 无刷新插入js
- chrome.tabs.query({}, (tab) => {
- for (let i in tab) {
- if (tab[i].url.indexOf('twitter.com') >= 0) {
- chrome.scripting.executeScript({
- target: { tabId: tab[i].id },
- files: ['js/content.js'],
- }, () => {
- setTimeout(() => {
- setChromeStorage({ executeScript: JSON.stringify({ executeScript: 1 }) })
- }, 2000);
- })
- }
- }
- })
- })
- }
- /**
- * 检查是否pined 显示tips
- */
- export function checkPined() {
- chrome.action.getUserSettings(res => {
- let { isOnToolbar } = res;
- if (!isOnToolbar) {
- sendActivetabMessage({
- actionType: 'BG_SHOW_PIN_TIPS'
- });
- }
- })
- }
- function sendActivetabMessage(message = {}) {
- chrome.tabs.query({
- active: true,
- currentWindow: true
- }, (tabs) => {
- chrome.tabs.sendMessage(tabs[0].id, message, res => {
- console.log(res)
- })
- })
- }
- /**
- * 安装后打开新标签页
- */
- export function onInstalledCreateTab() {
- let cookiesParams = {
- name: 'pickup_info',
- url: pageUrl
- }
- getChromeCookie(cookiesParams, (res) => {
- let { postNickName, srcContentId } = res;
- if (res && postNickName && srcContentId) {
- let url = `https://twitter.com/${postNickName}/status/${srcContentId}`
- chrome.tabs.create({
- url
- });
- removeChromeCookie(cookiesParams)
- } else {
- chrome.tabs.create({
- url: "https://twitter.com",
- });
- }
- })
- }
- /**
- * 在popop重新发送
- * @param {*} req
- */
- export function popupRePublish(req) {
- setChromeStorage({
- popupShowPublishDialog: JSON.stringify({
- show: true,
- srcContent: req.data.srcContent,
- postId: req.data.postId
- }),
- });
- chrome.tabs.create({
- url: "https://twitter.com",
- });
- }
- export function setBadgeInfo(params) {
- let {text = '', color = '#DF3535'} = params.data || {};
- chrome.action.setBadgeText({text: text});
- chrome.action.setBadgeBackgroundColor({color: color });
- }
- export function hideBadge() {
- chrome.action.setBadgeText({text: ''});
- chrome.action.setBadgeBackgroundColor({color: [0, 0, 0, 0]});
- }
- export async function setMessageCount () {
- const { accessToken: token = '', uid = '' } = await getChromeStorage('userInfo') || {}
- if(token) {
- getMessageInfo();
- setInterval(() => {
- getMessageInfo();
- }, 60000);
- }
- }
- export function getMessageInfo () {
- fetchAllMessageInfo().then(res => {
- if(res.code == 0) {
- let {unReadCountTotal = 0 } = res.data;
- if(unReadCountTotal > 0) {
- let text = unReadCountTotal > 99 ? '99+' : unReadCountTotal+'';
- setBadgeInfo({data: {text}});
- } else {
- hideBadge();
- }
- }
- })
- }
- export function readTaskAllMsg(cb) {
- fetchReadTaskAllMsg({
- msgType: 1 // 1:任务红包 2:钱包明细
- }).then(res => {
- if(res.code == 0) {
- cb && cb();
- }
- });
- }
|