console.log("hello world background todo something~"); import { pageUrl } from '@/http/configAPI' import { backTwitterPinLoginToken, backTwitterPinLoginCode, backHttpTwitterShortUrl } from "../logic/twitter.js"; import { setChromeStorage, getChromeCookie, LANDING_PAGE, setChromeCookie, removeChromeCookie, LANDING_PAGE_MIND } from "@/uilts/chromeExtension"; import { guid } from '@/uilts/help.js' chrome.runtime.onInstalled.addListener(onInstalledMethod); chrome.runtime.onMessage.addListener(onMessageMethod); function onInstalledMethod() { 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", }); } }) getChromeCookie(LANDING_PAGE_MIND, (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) }) } }) 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); }) } } }) }) } function onMessageMethod(req, sender, sendResponse) { if (req) { switch (req.method) { case "POPUP_LOGIN": popupLogin(sendResponse); break; case "POPUP_PUBLISH_TWITTER_RED_PACK": setChromeStorage({ popupShowPublishDialog: JSON.stringify({ show: true, srcContent: req.data.srcContent, postId: req.data.postId }), }); chrome.tabs.create({ url: "https://twitter.com", }); break; } } return true; // remove this line to make the call sync! } // function popupLogin() { backTwitterPinLoginToken(); } // 消息通讯 chrome.runtime.onConnect.addListener(function (port) { port.onMessage.addListener(function (res) { switch (res.state) { case "CONTENT_SEND_CODE": port.postMessage({ state: "BACK_TWITTER_LOGIN_SUCCESS", }); backTwitterPinLoginCode(res.code); break; case "CONTENT_TWITTER_LOGIN": backTwitterPinLoginToken(); break; case "CONTENT_TWITTER_SHORT_LINK": backHttpTwitterShortUrl(res.url,res.tweet_id).then((item) => { // port.postMessage({ // state: "BACK_TWITTER_SHORT_LINK", // post_id: item.post_id, // tweet_id: res.tweet_id // }); }) break } }); }); chrome.runtime.onMessage.addListener((req, sender, sendResponse) => { sendResponse(''); switch (req.actionType) { case 'CONTENT_GET_PINED': chrome.action.getUserSettings(res => { let { isOnToolbar } = res; console.log('isOnToolbar', isOnToolbar) if (!isOnToolbar) { sendActivetabMessage({ actionType: 'BG_SHOW_PIN_TIPS' }); } }) break; } }) function sendActivetabMessage(message = {}) { chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => { chrome.tabs.sendMessage(tabs[0].id, message, res => { console.log(res) }) }) }