diff --git a/src/utils/dataFormatter.ts b/src/utils/dataFormatter.ts index 11e32102..1f87b9d3 100644 --- a/src/utils/dataFormatter.ts +++ b/src/utils/dataFormatter.ts @@ -1,5 +1,5 @@ import { language } from '~/logic' -import { i18n } from '~/utils' +import { i18n } from '~/utils/i18n' export const { t } = i18n.global export const numFormatter = (num: number) => { diff --git a/src/utils/index.ts b/src/utils/index.ts index 90ebc411..45189e68 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,54 +1,5 @@ -import { grantAccessKey, revokeAccessKey } from './authProvider' -import { SVG_ICONS } from './svgIcons' -import { i18n } from './i18n' - -export { grantAccessKey, revokeAccessKey, SVG_ICONS, i18n } +export * from './authProvider' +export * from './svgIcons' +export * from './i18n' export * from './dataFormatter' - -/** - * get cookie by name - * @param name cookie name - * @returns cookie value - */ -export const getCookie = (name: string) => { - const value = `; ${document.cookie}` - const parts: Array = value.split(`; ${name}=`) - if (parts.length === 2) - return parts?.pop()?.split(';').shift() -} - -/** - * set cookie - * @param name cookie name - * @param value cookie value - */ -export const setCookie = (name: string, value: any, expDays: number) => { - const date = new Date() - date.setTime(date.getTime() + expDays * 24 * 60 * 60 * 1000) - const expires = `expires=${date.toUTCString()}` - document.cookie = `${name}=${value}; ${expires}; domain=.bilibili.com; path=/` -} - -/** - * get current login user id - * @returns userId - */ -export const getUserID = () => getCookie('DedeUserID') - -/** - * get csrf token - */ -export const getCSRF = () => getCookie('bili_jct') - -/** - * remove 'http://' or 'https://' from a URL - * @param url - * @returns - */ -export const removeHttpFromUrl = (url: string) => { - return url.replace(/^https?:/, '') -} - -export const openLinkToNewTab = (url: string) => { - window.open(url, '_blank') -} +export * from './main' diff --git a/src/utils/main.ts b/src/utils/main.ts new file mode 100644 index 00000000..6bae473c --- /dev/null +++ b/src/utils/main.ts @@ -0,0 +1,47 @@ +/** + * get cookie by name + * @param name cookie name + * @returns cookie value + */ +export const getCookie = (name: string) => { + const value = `; ${document.cookie}` + const parts: Array = value.split(`; ${name}=`) + if (parts.length === 2) + return parts?.pop()?.split(';').shift() +} + +/** + * set cookie + * @param name cookie name + * @param value cookie value + */ +export const setCookie = (name: string, value: any, expDays: number) => { + const date = new Date() + date.setTime(date.getTime() + expDays * 24 * 60 * 60 * 1000) + const expires = `expires=${date.toUTCString()}` + document.cookie = `${name}=${value}; ${expires}; domain=.bilibili.com; path=/` +} + +/** + * get current login user id + * @returns userId + */ +export const getUserID = () => getCookie('DedeUserID') + +/** + * get csrf token + */ +export const getCSRF = () => getCookie('bili_jct') + +/** + * remove 'http://' or 'https://' from a URL + * @param url + * @returns + */ +export const removeHttpFromUrl = (url: string) => { + return url.replace(/^https?:/, '') +} + +export const openLinkToNewTab = (url: string) => { + window.open(url, '_blank') +}