123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743 |
- import { fetchTtwitterRequestToken, fetchTwitterLogin, fetchTwitterShortUrl, fetchAllMessageInfo, fetchReadTaskAllMsg, getDiscordUserInfo, fetchGetTwitterNftPostPre, fetchPublish, fetchGetAllUnReadNotices } from '@/logic/background/fetch/twitter.js'
- import { LANDING_PAGE, WEBSITE_USER_INFO, LANDING_PAGE_MID, setChromeStorage, setChromeCookie, getChromeCookie, getChromeStorage, removeChromeCookie, LANDING_PAGE_JUMP_INFO } from '@/uilts/chromeExtension.js'
- import { guid } from '@/uilts/help.js'
- import { discordAuthRedirectUri } from '@/http/configAPI'
- import { setContentMessage } from '@/logic/background/help.js'
- import Report from "@/log-center/log"
- let authToken = ''
- let consumerKey = ''
- let tab_flag = true
- const isHasTabByUrl = (url, callback) => {
- try {
- let item
- chrome.tabs.query({}, (tab) => {
- for (let i in tab) {
- if (tab[i].url.indexOf(url) >= 0) {
- item = tab[i]
- break
- }
- }
- callback(item)
- })
- } catch (error) {
- Report.reportLog({
- objectType: Report.objectType.background_function_catch,
- funcName: 'isHasTabByUrl',
- errMsg: error.message
- })
- }
- }
- export function twitterPinLoginToken() {
- try {
- // 1.判断是否登陆了
- getChromeStorage('userInfo', (res) => {
- // 没有登陆
- if (!res) {
- if (tab_flag == false) {
- return
- }
- tab_flag = false
- fetchTtwitterRequestToken().then((res) => {
- tab_flag = true
- if (res.code == 0) {
- authToken = res.data.authToken
- consumerKey = res.data.consumerKey
- isHasTabByUrl('https://api.twitter.com/oauth/authorize?oauth_token', (tab) => {
- if (!tab) {
- chrome.tabs.create({
- url: `https://api.twitter.com/oauth/authorize?oauth_token=${res.data.authToken}`
- })
- } else {
- chrome.tabs.highlight({ windowId: tab.windowId, tabs: tab.index })
- }
- })
- }
- }).catch(() => {
- tab_flag = true
- })
- }
- })
- } catch (error) {
- Report.reportLog({
- objectType: Report.objectType.background_function_catch,
- funcName: 'twitterPinLoginToken',
- errMsg: error.message
- })
- }
- }
- export function twitterPinLoginCode(sender, 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)
- // }
- // }
- // })
- try {
- chrome.tabs.sendMessage(sender.tab.id, { actionType: 'BACK_TWITTER_LOGIN_SUCCESS' }, (res) => { console.log(res) });
- chrome.tabs.remove(sender.tab.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, consumerKey, code, receivedIds).then(res => {
- if (res.code == 0) {
- setChromeStorage({ userInfo: JSON.stringify(res.data) })
- setChromeCookie(WEBSITE_USER_INFO, res.data)
- sendActivetabMessage({
- actionType: 'BG_LOGIN_SET_USERINFO_CB'
- });
- // 获取全局消息数据
- setMessageCount()
- chrome.cookies.remove(LANDING_PAGE)
- }
- }).catch((error) => {
- console.log('catch', error)
- })
- })
- } catch (error) {
- Report.reportLog({
- objectType: Report.objectType.background_function_catch,
- funcName: 'twitterPinLoginCode',
- errMsg: error.message
- })
- }
- }
- let discordAuthWindowId = '';
- export function saveDiscordAuthWindowId(params) {
- let { windowId = '' } = params.data || {};
- discordAuthWindowId = windowId;
- }
- export function discordLoginCode({ code }, sender) {
- try {
- console.log('sender', sender)
- let { windowId, id } = sender.tab || {};
- chrome.tabs.remove(id);
- // 发送请求
- getDiscordUserInfo({
- authCode: code,
- redirectUrl: discordAuthRedirectUri
- }).then(res => {
- if (res.code == 0) {
- setTimeout(() => {
- sendActivetabMessage({
- actionType: 'BACK_DISCORD_LOGIN_SUCCESS'
- });
- })
- }
- }).catch((error) => {
- console.log('catch', error)
- })
- } catch (error) {
- Report.reportLog({
- objectType: Report.objectType.background_function_catch,
- funcName: 'discordLoginCode',
- errMsg: error.message
- })
- }
- // if(windowId) {
- // chrome.windows.remove(
- // windowId,
- // function () {
- // }
- // )
- // }
- }
- export function twitterShortUrl(sender, url) {
- try {
- fetchTwitterShortUrl(url).then(res => {
- let str_arr = res.match(/denetme.net\/([\s\S]*?)"/) || []
- let post_Id = str_arr[1] || ''
- if (!post_Id) {
- return
- }
- // 解析
- let _obj = {
- short_url: url,
- post_Id
- }
- getChromeStorage('cardData', item => {
- if (item) {
- let has = false
- for (let i in item) {
- if (item[i].short_url == _obj.short_url) {
- item[i].short_url = _obj.short_url
- item[i].post_Id = _obj.post_Id
- setChromeStorage({ cardData: JSON.stringify(item) })
- has = true
- break
- }
- }
- if (!has) {
- item.push(_obj)
- setChromeStorage({ cardData: JSON.stringify(item) })
- }
- } else {
- setChromeStorage({ cardData: JSON.stringify([_obj]) })
- }
- chrome.tabs.sendMessage(sender.tab.id, { actionType: 'BACK_TWITTER_SHORT_URL' }, (response) => { });
- // port.postMessage({
- // state: "BACK_TWITTER_SHORT_URL"
- // });
- })
- }).catch((error) => {
- console.log('catch', error)
- })
- } catch (error) {
- Report.reportLog({
- objectType: Report.objectType.background_function_catch,
- funcName: 'twitterShortUrl',
- errMsg: error.message
- })
- }
- }
- // 安装插件后获取mid
- export function onInstalledMid(cb) {
- try {
- getChromeCookie(LANDING_PAGE_MID, (res_arr) => {
- // 没有cookie
- if (res_arr && res_arr.length) {
- setChromeStorage({ mid: JSON.stringify(res_arr[0]) }, () => {
- cb && cb()
- })
- } else {
- let _params = {
- mid: guid()
- }
- setChromeCookie(LANDING_PAGE, { 'mid': _params.mid })
- setChromeStorage({ mid: JSON.stringify(_params) }, () => {
- cb && cb()
- })
- }
- })
- } catch (error) {
- cb && cb();
- Report.reportLog({
- objectType: Report.objectType.background_function_catch,
- funcName: 'onInstalledMid',
- errMsg: error.message
- })
- }
- }
- export function onInstalledUserSet() {
- try {
- chrome.action.getUserSettings().then(res => {
- setChromeStorage({ userSettings: JSON.stringify({ res }) })
- // 无刷新插入js
- chrome.tabs.query({}, (tabs) => {
- for (let i in tabs) {
- if (tabs[i].url && tabs[i].url.substr(0, 4).includes('http')) {
- chrome.scripting.executeScript({
- target: { tabId: tabs[i].id },
- files: ['js/content_help.js']
- }, () => { })
- if (tabs[i].url.indexOf('twitter.com') >= 0 || tabs[i].url.indexOf('facebook.com') >= 0) {
- chrome.scripting.executeScript({
- target: { tabId: tabs[i].id },
- files: ['js/content.js'],
- }, () => {
- setTimeout(() => {
- setChromeStorage({ executeScript: JSON.stringify({ executeScript: 1 }) })
- }, 2000);
- })
- }
- }
- }
- })
- }).catch((error) => {
- console.log('catch', error)
- })
- } catch (error) {
- Report.reportLog({
- objectType: Report.objectType.background_function_catch,
- funcName: 'onInstalledUserSet',
- errMsg: error.message
- })
- }
- }
- /**
- * 检查是否pined 显示tips
- */
- export function checkPined() {
- try {
- chrome.action.getUserSettings(res => {
- let { isOnToolbar } = res;
- if (!isOnToolbar) {
- sendActivetabMessage({
- actionType: 'BG_SHOW_PIN_TIPS'
- });
- }
- })
- } catch (error) {
- Report.reportLog({
- objectType: Report.objectType.background_function_catch,
- funcName: 'checkPined',
- errMsg: error.message
- })
- }
- }
- function sendActivetabMessage(message = {}) {
- try {
- chrome.tabs.query({
- active: true,
- currentWindow: true
- }, (tabs) => {
- if (tabs && tabs.length) {
- chrome.tabs.sendMessage(tabs[0].id, message)
- }
- })
- } catch (error) {
- Report.reportLog({
- objectType: Report.objectType.background_function_catch,
- funcName: 'sendActivetabMessage',
- errMsg: error.message
- });
- }
- }
- /**
- * 安装后打开新标签页
- */
- export function onInstalledCreateTab() {
- try {
- getChromeCookie(LANDING_PAGE_JUMP_INFO, (res = {}) => {
- onInstalledMid(() => {
- setTimeout(() => {
- if (!res) {
- res = {}
- }
- // 安装成功埋点
- Report.reportLog({
- objectType: Report.objectType.chrome_extension_installed,
- funcName: 'onInstalledCreateTab',
- postId: res.postId || '',
- shareLinkId: res.shareLinkId || '',
- 'channel-name': res.channelName
- })
- }, 5000)
- });
- let url = 'https://twitter.com/search?q=%23denet'
- // jump_info
- if (!res || !res.jump_type) {
- chrome.tabs.create({
- url
- });
- return
- }
- let created_detail = false
- switch (String(res.jump_type)) {
- // 普通红包
- case 'red_packet':
- if (res && res.postNickName && res.srcContentId) {
- created_detail = true
- url = `https://twitter.com/${res.postNickName}/status/${res.srcContentId}`
- chrome.tabs.create({
- url
- });
- }
- break
- // 抽奖红包
- case 'luck_draw':
- if (res && res.postNickName && res.srcContentId) {
- created_detail = true
- url = `https://twitter.com/${res.postNickName}/status/${res.srcContentId}`
- chrome.tabs.create({
- url
- });
- }
- break
- // NFT
- case 'nft_info':
- if (res && res.twitterAccount && res.nftProjectId) {
- created_detail = true
- url = `https://twitter.com/${res.twitterAccount}`
- chrome.tabs.create({
- url
- });
- }
- break
- // NFT 组
- case 'nft_group_info':
- if (res && res.twitterAccount) {
- created_detail = true
- // setChromeStorage({ groupTabData: JSON.stringify({
- // deTabVal: 'deGroupTab'
- // })})
- chrome.storage.local.set({
- groupTabData: JSON.stringify({
- deTabVal: 'deGroupTab'
- })
- }, (response) => {
- url = `https://twitter.com/${res.twitterAccount}`
- chrome.tabs.create({
- url
- });
- })
- }
- break
- // toolbox
- case 'tool_box':
- if (res && res.postNickName && res.srcContentId) {
- created_detail = true
- url = `https://twitter.com/${res.postNickName}/status/${res.srcContentId}`
- chrome.tabs.create({
- url
- });
- }
- break
- case 'treasure_info':
- if (res && res.postNickName && res.srcContentId) {
- created_detail = true
- url = `https://twitter.com/${res.postNickName}/status/${res.srcContentId}`
- chrome.tabs.create({
- url
- });
- }
- break;
- }
- if (created_detail == false) {
- chrome.tabs.create({
- url
- });
- }
- removeChromeCookie(LANDING_PAGE_JUMP_INFO)
- })
- } catch (error) {
- Report.reportLog({
- objectType: Report.objectType.background_function_catch,
- funcName: 'onInstalledCreateTab',
- errMsg: error.message
- })
- }
- }
- /**
- * 在popop重新发送
- * @param {*} req
- */
- export function popupRePublish(req) {
- try {
- setChromeStorage({
- popupShowPublishDialog: JSON.stringify({
- ...req.data,
- show: true
- }),
- });
- chrome.tabs.create({
- url: "https://twitter.com",
- });
- } catch (error) {
- Report.reportLog({
- objectType: Report.objectType.background_function_catch,
- funcName: 'popupRePublish',
- errMsg: error.message
- })
- }
- }
- export function setBadgeInfo(params) {
- try {
- let { text = '', color = '#DF3535' } = params.data || {};
- chrome.action.setBadgeText({ text: text });
- chrome.action.setBadgeBackgroundColor({ color: color });
- } catch (error) {
- Report.reportLog({
- objectType: Report.objectType.background_function_catch,
- funcName: 'setBadgeInfo',
- errMsg: error.message
- })
- }
- }
- export function hideBadge() {
- try {
- chrome.action.setBadgeText({ text: '' });
- chrome.action.setBadgeBackgroundColor({ color: [0, 0, 0, 0] });
- } catch (error) {
- Report.reportLog({
- objectType: Report.objectType.background_function_catch,
- funcName: 'hideBadge',
- errMsg: error.message
- })
- }
- }
- export async function setMessageCount() {
- try {
- const { accessToken: token = '', uid = '' } = await getChromeStorage('userInfo') || {}
- if (token) {
- getMessageInfo();
- createAlarm();
- }
- } catch (error) {
- }
- }
- function createAlarm() {
- try {
- let alarmInfo = {
- //1分鐘之後開始(該值不能小於1)
- delayInMinutes: 1,
- //與上方等同的寫法是
- // when : Date.now() + n,
- //開始後每一分鐘執行一次(該值不能小于1)
- periodInMinutes: 1
- };
- //每次加載就清空定時器
- chrome.alarms.clear('denetChromeAlarm');
- //創造定時器
- chrome.alarms.create('denetChromeAlarm', alarmInfo);
- } catch (error) {
- Report.reportLog({
- objectType: Report.objectType.background_function_catch,
- funcName: 'createAlarm',
- errMsg: error.message
- });
- }
- }
- export function getMessageInfo() {
- try {
- 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();
- }
- }
- }).catch((error) => {
- console.log('catch', error)
- })
- } catch (error) {
- Report.reportLog({
- objectType: Report.objectType.background_function_catch,
- funcName: 'getMessageInfo',
- errMsg: error.message
- })
- }
- }
- export function readTaskAllMsg({ msgType }, cb) {
- try {
- fetchReadTaskAllMsg({
- msgType // 1:任务红包 2:钱包明细
- }).then(res => {
- if (res.code == 0) {
- cb && cb();
- }
- });
- } catch (error) {
- Report.reportLog({
- objectType: Report.objectType.background_function_catch,
- funcName: 'readTaskAllMsg',
- errMsg: error.message
- })
- }
- }
- export const onDisconnectHandler = (port) => {
- try {
- if (port.name === "popup" || port.name === "popup_transactions") {
- let msgType = port.name === "popup" ? 1 : 2;
- readTaskAllMsg({ msgType }, () => {
- getMessageInfo();
- })
- }
- } catch (error) {
- Report.reportLog({
- objectType: Report.objectType.background_function_catch,
- funcName: 'onDisconnectHandler',
- errMsg: error.message
- })
- }
- }
- export const injectExtensionPopup = (tab) => {
- try {
- sendActivetabMessage({
- actionType: 'BG_INJECT_EXTENSION_POPUP'
- });
- } catch (error) {
- Report.reportLog({
- objectType: Report.objectType.background_function_catch,
- funcName: 'injectExtensionPopup',
- errMsg: error.message
- })
- }
- }
- export const setPopupConfig = (activeInfo) => {
- try {
- chrome.tabs.query({
- active: true,
- currentWindow: true
- }, (tabs) => {
- if (tabs.length) {
- let { pendingUrl = '', url = '' } = tabs[0];
- if (pendingUrl.startsWith('https://twitter.com') || url.startsWith('https://twitter.com')) {
- sendActivetabMessage({
- actionType: 'BG_SET_POPUP_CONFIG'
- });
- } else {
- chrome.action.setPopup({
- popup: 'popup.html',
- }, function () {
- });
- }
- }
- })
- } catch (error) {
- Report.reportLog({
- objectType: Report.objectType.background_function_catch,
- funcName: 'setPopupConfig',
- errMsg: error.message
- })
- }
- }
- export const getSysMessage = () => {
- try {
- // 请求通知接口
- fetchGetAllUnReadNotices({})
- .then((res) => {
- // 向选中的content发送消息
- setContentMessage({ actionType: 'BACK_UNREAD_MESSAGE', data: res })
- }).catch((error) => {
- console.log('catch', error)
- })
- } catch (error) {
- Report.reportLog({
- objectType: Report.objectType.background_function_catch,
- funcName: 'getSysMessage',
- errMsg: error.message
- })
- }
- }
- export const windwoLoadSetPopupPage = (data, sender) => {
- let { url = '' } = sender.tab;
- if (url.startsWith('chrome://')) {
- chrome.action.setPopup({
- popup: 'popup.html',
- }, function () {
- });
- } else {
- chrome.action.setPopup({
- popup: '',
- }, function () {
- });
- }
- }
- export const setActionPopup = (data) => {
- try {
- let { popup } = data.data || {};
- if (popup) {
- chrome.action.getPopup(
- {},
- function (result) {
- if (!result) {
- chrome.action.setPopup({
- popup,
- }, function () {
- });
- }
- });
- } else {
- chrome.action.setPopup({
- popup: '',
- }, function () {
- });
- }
- } catch (error) {
- Report.reportLog({
- objectType: Report.objectType.background_function_catch,
- funcName: 'setActionPopup',
- errMsg: error.message
- });
- }
- }
- export const getTwitterNftPostPre = (params, sender) => {
- fetchGetTwitterNftPostPre(params).then((res) => {
- if (res.code == 0) {
- chrome.tabs.sendMessage(sender.tab.id, { actionType: 'BACK_TWITTER_NFT_POST_PRE', data: res.data }, (res) => { console.log(res) });
- }
- }).catch((error) => {
- console.log('catch', error)
- })
- }
- export const nftTxtPublish = (params, sender) => {
- fetchPublish(params).then((res) => {
- if (res.code == 0) {
- chrome.tabs.sendMessage(sender.tab.id, { actionType: 'BACK_NFT_PUBLISH_DONE', data: res.data }, (res) => { console.log(res) });
- }
- }).catch((error) => {
- console.log('catch', error)
- })
- }
- export const checkShowPublishDialog = (params) => {
- const twitterUrl = 'https://twitter.com';
- createTabShowGiveaway({
- url: twitterUrl
- })
- }
- const createTabShowGiveaway = (params) => {
- setChromeStorage({
- showGiveawayData: JSON.stringify({
- show: true
- })
- });
- chrome.tabs.create({
- url: params.url,
- });
- }
|