123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- // 此文件不要写具体逻辑,只调用函数
- import { pageUrl } from '@/http/configAPI'
- import {
- twitterShortUrl,
- twitterPinLoginToken,
- twitterPinLoginCode,
- onInstalledMid,
- onInstalledUserSet
- } from "@/logic/background/twitter";
- import { setChromeStorage, getChromeCookie , removeChromeCookie } from "@/uilts/chromeExtension";
- 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",
- });
- }
- })
- onInstalledMid()
- onInstalledUserSet()
- }
- 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() {
- twitterPinLoginToken();
- }
- // 消息通讯
- chrome.runtime.onConnect.addListener(function (port) {
- port.onMessage.addListener(function (res) {
- switch (res.state) {
- case "CONTENT_SEND_CODE":
- twitterPinLoginCode(port,res.code);
- break;
- case "CONTENT_TWITTER_LOGIN":
- twitterPinLoginToken();
- break;
- case "CONTENT_TWITTER_SHORT_LINK":
- twitterShortUrl(res.url)
- 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)
- })
- })
- }
|