refactor: reorganize files and adjust variable names

This commit is contained in:
Hakadao
2023-05-06 19:01:32 +08:00
parent 2433c315a5
commit e686c828fa
13 changed files with 71 additions and 61 deletions

View File

@@ -1,21 +0,0 @@
import { setupAnimeAPIs } from './anime'
import { setupAuthAPIs } from './auth'
import { setupVideosAPIs } from './videos'
import { setupUserAPIs } from './user'
import { setupSearchAPIs } from './search'
import { setupNotificationsAPIs } from './notifications'
import { setupMomentsAPIs } from './moments'
import { setupHistoryAPIs } from './history'
import { setupFavoritesAPIs } from './favorites'
export const setupAllAPIs = () => {
setupAuthAPIs()
setupVideosAPIs()
setupUserAPIs()
setupSearchAPIs()
setupNotificationsAPIs()
setupMomentsAPIs()
setupHistoryAPIs()
setupFavoritesAPIs()
setupAnimeAPIs()
}

View File

@@ -2,7 +2,7 @@ import type { Tabs } from 'webextension-polyfill'
import browser from 'webextension-polyfill'
// import { onMessage, sendMessage } from 'webext-bridge'
import { resetCss } from './resetWebsiteStyle'
import { setupAllAPIs } from './apis'
import { setupAllMsgLstnrs } from './messageListeners'
browser.runtime.onInstalled.addListener((): void => {
// eslint-disable-next-line no-console
@@ -128,5 +128,5 @@ browser.tabs.onUpdated.addListener((tabId: number, changInfo: Tabs.OnUpdatedChan
}
})
// Setup APIs
setupAllAPIs()
// Setup all message listeners
setupAllMsgLstnrs()

View File

@@ -1,7 +1,7 @@
import browser from 'webextension-polyfill'
// import { getUserID } from '~/utils'
export const setupAnimeAPIs = () => {
export const setupAnimeMsgLstnr = () => {
browser.runtime.onConnect.addListener(() => {
browser.runtime.onMessage.addListener((message) => {
// get popular anime list

View File

@@ -1,6 +1,6 @@
import browser from 'webextension-polyfill'
export const setupAuthAPIs = () => {
export const setupAuthMsgLstnr = () => {
browser.runtime.onConnect.addListener(() => {
browser.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.contentScriptQuery === 'getAccessKey') {

View File

@@ -1,6 +1,6 @@
import browser from 'webextension-polyfill'
export const setupFavoritesAPIs = () => {
export const setupFavoriteMsgLstnr = () => {
browser.runtime.onConnect.addListener(() => {
browser.runtime.onMessage.addListener((message) => {
// https://github.com/SocialSisterYi/bilibili-API-collect/blob/master/fav/info.md#%E8%8E%B7%E5%8F%96%E6%8C%87%E5%AE%9A%E7%94%A8%E6%88%B7%E5%88%9B%E5%BB%BA%E7%9A%84%E6%89%80%E6%9C%89%E6%94%B6%E8%97%8F%E5%A4%B9%E4%BF%A1%E6%81%AF

View File

@@ -1,6 +1,6 @@
import browser from 'webextension-polyfill'
export const setupHistoryAPIs = () => {
export const setupHistoryMsgLstnr = () => {
browser.runtime.onConnect.addListener(() => {
browser.runtime.onMessage.addListener((message) => {
// https://github.com/SocialSisterYi/bilibili-API-collect/blob/master/history&toview/history.md#%E8%8E%B7%E5%8F%96%E5%8E%86%E5%8F%B2%E8%AE%B0%E5%BD%95%E5%88%97%E8%A1%A8_web%E7%AB%AF

View File

@@ -0,0 +1,23 @@
import { setupAnimeMsgLstnr } from './anime'
import { setupAuthMsgLstnr } from './auth'
import { setupVideoMsgLstnr } from './video'
import { setupUserMsgLstnr } from './user'
import { setupSearchMsgLstnr } from './search'
import { setupNotificationMsgLstnr } from './notification'
import { setupMomentMsgLstnr } from './moment'
import { setupHistoryMsgLstnr } from './history'
import { setupFavoriteMsgLstnr } from './favorite'
import { setupWatchLaterMsgLstnr } from './watchLater'
export const setupAllMsgLstnrs = () => {
setupAuthMsgLstnr()
setupVideoMsgLstnr()
setupUserMsgLstnr()
setupSearchMsgLstnr()
setupNotificationMsgLstnr()
setupMomentMsgLstnr()
setupHistoryMsgLstnr()
setupFavoriteMsgLstnr()
setupAnimeMsgLstnr()
setupWatchLaterMsgLstnr()
}

View File

@@ -1,6 +1,6 @@
import browser from 'webextension-polyfill'
export const setupMomentsAPIs = () => {
export const setupMomentMsgLstnr = () => {
browser.runtime.onConnect.addListener(() => {
browser.runtime.onMessage.addListener((message) => {
if (message.contentScriptQuery === 'getNewMomentsCount') {

View File

@@ -1,6 +1,6 @@
import browser from 'webextension-polyfill'
export const setupNotificationsAPIs = () => {
export const setupNotificationMsgLstnr = () => {
browser.runtime.onConnect.addListener(() => {
browser.runtime.onMessage.addListener((message) => {
if (message.contentScriptQuery === 'getUnreadMsg') {

View File

@@ -1,6 +1,6 @@
import browser from 'webextension-polyfill'
export const setupSearchAPIs = () => {
export const setupSearchMsgLstnr = () => {
browser.runtime.onConnect.addListener(() => {
browser.runtime.onMessage.addListener((message) => {
if (message.contentScriptQuery === 'getSearchSuggestion') {

View File

@@ -1,6 +1,6 @@
import browser from 'webextension-polyfill'
export const setupUserAPIs = () => {
export const setupUserMsgLstnr = () => {
browser.runtime.onConnect.addListener(() => {
browser.runtime.onMessage.addListener((message) => {
if (message.contentScriptQuery === 'getUserInfo') {

View File

@@ -1,6 +1,6 @@
import browser from 'webextension-polyfill'
export const setupVideosAPIs = () => {
export const setupVideoMsgLstnr = () => {
browser.runtime.onConnect.addListener(() => {
browser.runtime.onMessage.addListener((message) => {
// #region APP端api遺棄
@@ -88,34 +88,6 @@ export const setupVideosAPIs = () => {
.then(data => data)
.catch(error => console.error(error))
}
// https://github.com/SocialSisterYi/bilibili-API-collect/blob/master/docs/history&toview/toview.md#%E8%A7%86%E9%A2%91%E6%B7%BB%E5%8A%A0%E7%A8%8D%E5%90%8E%E5%86%8D%E7%9C%8B
else if (message.contentScriptQuery === 'saveToWatchLater') {
const url = 'https://api.bilibili.com/x/v2/history/toview/add'
return fetch(url, {
method: 'POST',
body: new URLSearchParams({
csrf: message.csrf,
aid: message.aid,
}),
})
.then(response => response.json())
.then(data => data)
.catch(error => console.error(error))
}
// https://github.com/SocialSisterYi/bilibili-API-collect/blob/master/docs/history&toview/toview.md#%E5%88%A0%E9%99%A4%E7%A8%8D%E5%90%8E%E5%86%8D%E7%9C%8B%E8%A7%86%E9%A2%91
else if (message.contentScriptQuery === 'removeFromWatchLater') {
const url = 'https://api.bilibili.com/x/v2/history/toview/del'
return fetch(url, {
method: 'POST',
body: new URLSearchParams({
csrf: message.csrf,
aid: message.aid,
}),
})
.then(response => response.json())
.then(data => data)
.catch(error => console.error(error))
}
})
})
}

View File

@@ -0,0 +1,36 @@
import browser from 'webextension-polyfill'
export const setupWatchLaterMsgLstnr = () => {
browser.runtime.onConnect.addListener(() => {
browser.runtime.onMessage.addListener((message) => {
// https://github.com/SocialSisterYi/bilibili-API-collect/blob/master/docs/history&toview/toview.md#%E8%A7%86%E9%A2%91%E6%B7%BB%E5%8A%A0%E7%A8%8D%E5%90%8E%E5%86%8D%E7%9C%8B
if (message.contentScriptQuery === 'saveToWatchLater') {
const url = 'https://api.bilibili.com/x/v2/history/toview/add'
return fetch(url, {
method: 'POST',
body: new URLSearchParams({
csrf: message.csrf,
aid: message.aid,
}),
})
.then(response => response.json())
.then(data => data)
.catch(error => console.error(error))
}
// https://github.com/SocialSisterYi/bilibili-API-collect/blob/master/docs/history&toview/toview.md#%E5%88%A0%E9%99%A4%E7%A8%8D%E5%90%8E%E5%86%8D%E7%9C%8B%E8%A7%86%E9%A2%91
else if (message.contentScriptQuery === 'removeFromWatchLater') {
const url = 'https://api.bilibili.com/x/v2/history/toview/del'
return fetch(url, {
method: 'POST',
body: new URLSearchParams({
csrf: message.csrf,
aid: message.aid,
}),
})
.then(response => response.json())
.then(data => data)
.catch(error => console.error(error))
}
})
})
}