import { pageUrl, websiteUrl } from "@/http/configAPI.js" import { guid } from "@/uilts/help"; export const LANDING_PAGE = { name: 'received_log', url: pageUrl } export const LANDING_PAGE_MID = { name: 'mid', url: pageUrl } export const LANDING_PAGE_JUMP_INFO = { name: 'jump_info', url: pageUrl } export const WEBSITE_USER_INFO = { name: 'userInfo', url: websiteUrl } export function setChromeStorage(params, callback) { try { if (callback) { chrome.storage.local.set(params, callback) } else { chrome.storage.local.set(params) } } catch (error) { console.error('catch', error) } } export function getChromeStorage(key = '', callback) { try { let params = {} params[key] = '' if (!callback) { return new Promise((resolve) => { chrome.storage.local.get(params, (item) => { if (item[key]) { resolve(JSON.parse(item[key])) } else { resolve(null) } }) }) } chrome.storage.local.get(params, (item) => { if (item[key]) { callback(JSON.parse(item[key])) } else { callback(null) } }); } catch (error) { console.error('catch', error) } } // export function chromeSendMessage() { // try { // // chrome.runtime.sendMessage // } catch { // } // } export function setChromeCookie({ name, url }, value_obj) { try { chrome.cookies.set({ expirationDate: new Date().getTime() / 10, name: name, url: url, value: encodeURIComponent(JSON.stringify(value_obj)) || '' }) } catch (error) { console.error('catch', error) } } export function getChromeCookie({ name = '', url = '', return_type = '' }, callback) { try { chrome.cookies.getAll({ name, url }, (res = []) => { let _str = '' if (Array.isArray(res) && res.length) { _str = res[0].value } if (_str) { if (return_type == 'str') { callback(_str) } else { callback(JSON.parse(decodeURIComponent(_str))) } } else { callback(null) } }) } catch (error) { console.error('catch', error) } } export function removeChromeCookie(params, cb) { try { let { name, url } = params; chrome.cookies.remove({ name, url }, () => { cb && cb }) } catch (error) { console.log('catch', error) } } export function sendChromeTabMessage(params, callback) { try { chrome.tabs.getCurrent((tab) => { chrome.tabs.sendMessage(tab.id, params, callback); }) } catch (error) { console.log('catch', error) } } export function checkIsLogin(callback) { try { return new Promise((resolve) => { getChromeStorage('userInfo', (_userInfo) => { if (!_userInfo) { chrome.runtime.sendMessage( { actionType: "POPUP_LOGIN", data: "" }, (response) => { console.log("res", response); } ) callback && callback() resolve(_userInfo) } else { resolve(_userInfo) } }) }) } catch (error) { console.error('catch', error) } } let callback_arr = [] export function httpContentToBack(data, callback) { try { let callback_id = guid() chrome.runtime.sendMessage({ actionType: "HTTP_CONTENT_TO_BACK", data, callback_id }, (res) => { if (res && callback) { callback_arr.push({ callback_id, callback, }) } }) } catch (error) { console.error('catch', error) } } // 再找到它执行 export function httpBackToContentCallBack(req) { try { for (let i in callback_arr) { if (callback_arr[i].callback_id == req.callback_id) { // 执行 callback_arr[i].callback(req.data) // 删除 callback_arr.splice(i, 1) break } } } catch (error) { console.error('catch', error) } }