123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- 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)
- })
- })
- }
|