diff --git a/src/background/messageListeners/anime.ts b/src/background/messageListeners/anime.ts index e79e809b..b4c95568 100644 --- a/src/background/messageListeners/anime.ts +++ b/src/background/messageListeners/anime.ts @@ -1,53 +1,57 @@ import browser from 'webextension-polyfill' -// import { getUserID } from '~/utils' +function handleMessage(message: any) { + // get popular anime list + if (message.contentScriptQuery === 'getPopularAnimeList') { + const url + = 'https://api.bilibili.com/pgc/web/rank/list?season_type=1&day=3' + return fetch(url) + .then(response => response.json()) + .then(data => data) + .catch(error => console.error(error)) + } + // https://github.com/SocialSisterYi/bilibili-API-collect/blob/36e250090800793b41b223b55eefdcbb9391b53e/user/space.md#%E6%9F%A5%E8%AF%A2%E7%94%A8%E6%88%B7%E8%BF%BD%E7%95%AA%E8%BF%BD%E5%89%A7%E6%98%8E%E7%BB%86 + else if (message.contentScriptQuery === 'getAnimeWatchList') { + const url = `https://api.bilibili.com/x/space/bangumi/follow/list?type=1&follow_status=0&pn=${message.pn}&ps=${message.ps}&vmid=${message.vmid}` + return fetch(url) + .then(response => response.json()) + .then(data => data) + .catch(error => console.error(error)) + } + else if (message.contentScriptQuery === 'getRecommendAnimeList') { + const url = `https://api.bilibili.com/pgc/page/web/v3/feed?name=anime&coursor=${ + message.coursor ?? '' + }` + return fetch(url) + .then(response => response.json()) + .then(data => data) + .catch(error => console.error(error)) + } + // https://github.com/SocialSisterYi/bilibili-API-collect/blob/master/bangumi/timeline.md#%E7%95%AA%E5%89%A7%E6%88%96%E5%BD%B1%E8%A7%86%E6%97%B6%E9%97%B4%E7%BA%BF + else if (message.contentScriptQuery === 'getAnimeTimeTable') { + const url + = 'https://api.bilibili.com/pgc/web/timeline?types=1&before=6&after=6' + return fetch(url) + .then(response => response.json()) + .then(data => data) + .catch(error => console.error(error)) + } + else if (message.contentScriptQuery === 'getAnimeDetail') { + const url = 'https://api.bilibili.com/pgc/view/web/season?ep_id=234406' + return fetch(url) + .then(response => response.json()) + .then(data => data) + .catch(error => console.error(error)) + } + // TODO: https://api.bilibili.com/pgc/season/index/condition?season_type=1&type=1 +} + +function handleConnect() { + browser.runtime.onMessage.removeListener(handleMessage) + browser.runtime.onMessage.addListener(handleMessage) +} export function setupAnimeMsgLstnr() { - browser.runtime.onConnect.addListener(() => { - browser.runtime.onMessage.addListener((message) => { - // get popular anime list - if (message.contentScriptQuery === 'getPopularAnimeList') { - const url - = 'https://api.bilibili.com/pgc/web/rank/list?season_type=1&day=3' - return fetch(url) - .then(response => response.json()) - .then(data => data) - .catch(error => console.error(error)) - } - // https://github.com/SocialSisterYi/bilibili-API-collect/blob/36e250090800793b41b223b55eefdcbb9391b53e/user/space.md#%E6%9F%A5%E8%AF%A2%E7%94%A8%E6%88%B7%E8%BF%BD%E7%95%AA%E8%BF%BD%E5%89%A7%E6%98%8E%E7%BB%86 - else if (message.contentScriptQuery === 'getAnimeWatchList') { - const url = `https://api.bilibili.com/x/space/bangumi/follow/list?type=1&follow_status=0&pn=${message.pn}&ps=${message.ps}&vmid=${message.vmid}` - return fetch(url) - .then(response => response.json()) - .then(data => data) - .catch(error => console.error(error)) - } - else if (message.contentScriptQuery === 'getRecommendAnimeList') { - const url = `https://api.bilibili.com/pgc/page/web/v3/feed?name=anime&coursor=${ - message.coursor ?? '' - }` - return fetch(url) - .then(response => response.json()) - .then(data => data) - .catch(error => console.error(error)) - } - // https://github.com/SocialSisterYi/bilibili-API-collect/blob/master/bangumi/timeline.md#%E7%95%AA%E5%89%A7%E6%88%96%E5%BD%B1%E8%A7%86%E6%97%B6%E9%97%B4%E7%BA%BF - else if (message.contentScriptQuery === 'getAnimeTimeTable') { - const url - = 'https://api.bilibili.com/pgc/web/timeline?types=1&before=6&after=6' - return fetch(url) - .then(response => response.json()) - .then(data => data) - .catch(error => console.error(error)) - } - else if (message.contentScriptQuery === 'getAnimeDetail') { - const url = 'https://api.bilibili.com/pgc/view/web/season?ep_id=234406' - return fetch(url) - .then(response => response.json()) - .then(data => data) - .catch(error => console.error(error)) - } - // TODO: https://api.bilibili.com/pgc/season/index/condition?season_type=1&type=1 - }) - }) + browser.runtime.onConnect.removeListener(handleConnect) + browser.runtime.onConnect.addListener(handleConnect) } diff --git a/src/background/messageListeners/auth.ts b/src/background/messageListeners/auth.ts index 5b655c91..173c0c5d 100644 --- a/src/background/messageListeners/auth.ts +++ b/src/background/messageListeners/auth.ts @@ -1,27 +1,33 @@ import browser from 'webextension-polyfill' -export function setupAuthMsgLstnr() { - browser.runtime.onConnect.addListener(() => { - browser.runtime.onMessage.addListener((message, sender, sendResponse) => { - if (message.contentScriptQuery === 'getAccessKey') { - const url = message.confirmUri - fetch(url) - .then(response => sendResponse({ accessKey: `${response.url}`.match(/access_key=([0-9a-z]{32})/)![1] })) - .catch(error => console.error(error)) - return true - } - else if (message.contentScriptQuery === 'logout') { - const url = `https://passport.bilibili.com/login/exit/v2?biliCSRF=${message.biliCSRF}` - fetch(url, { - method: 'POST', - body: JSON.stringify({ - biliCSRF: message.biliJct, - }), - }) - .then(response => response.json()) - .then(data => sendResponse(data)) - .catch(error => console.error(error)) - } +function handleMessage(message: any, sender: any, sendResponse: any) { + if (message.contentScriptQuery === 'getAccessKey') { + const url = message.confirmUri + fetch(url) + .then(response => sendResponse({ accessKey: `${response.url}`.match(/access_key=([0-9a-z]{32})/)![1] })) + .catch(error => console.error(error)) + return true + } + else if (message.contentScriptQuery === 'logout') { + const url = `https://passport.bilibili.com/login/exit/v2?biliCSRF=${message.biliCSRF}` + fetch(url, { + method: 'POST', + body: JSON.stringify({ + biliCSRF: message.biliJct, + }), }) - }) + .then(response => response.json()) + .then(data => sendResponse(data)) + .catch(error => console.error(error)) + } +} + +function handleConnect() { + browser.runtime.onMessage.removeListener(handleMessage) + browser.runtime.onMessage.addListener(handleMessage) +} + +export function setupAuthMsgLstnr() { + browser.runtime.onConnect.removeListener(handleConnect) + browser.runtime.onConnect.addListener(handleConnect) } diff --git a/src/background/messageListeners/favorite.ts b/src/background/messageListeners/favorite.ts index eb5d6aef..80194c81 100644 --- a/src/background/messageListeners/favorite.ts +++ b/src/background/messageListeners/favorite.ts @@ -1,42 +1,48 @@ import browser from 'webextension-polyfill' -export function 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 - if (message.contentScriptQuery === 'getFavoriteCategories') { - const url = `https://api.bilibili.com/x/v3/fav/folder/created/list-all?up_mid=${message.mid}&jsonp=jsonp` - return fetch(url) - .then(response => response.json()) - .then(data => data) - .catch(error => console.error(error)) - } - // https://github.com/SocialSisterYi/bilibili-API-collect/blob/master/fav/list.md#%E8%8E%B7%E5%8F%96%E6%94%B6%E8%97%8F%E5%A4%B9%E5%86%85%E5%AE%B9%E6%98%8E%E7%BB%86%E5%88%97%E8%A1%A8 - else if (message.contentScriptQuery === 'getFavoriteResources') { - const url = `https://api.bilibili.com/x/v3/fav/resource/list?media_id=${message.mediaId}&pn=${message.pageNum}&ps=20&keyword=${message.keyword}&order=mtime&type=0&tid=0&platform=web&jsonp=jsonp` - return fetch(url) - .then(response => response.json()) - .then(data => data) - .catch(error => console.error(error)) - } - // https://github.com/SocialSisterYi/bilibili-API-collect/blob/master/docs/fav/action.md#%E6%89%B9%E9%87%8F%E5%88%A0%E9%99%A4%E5%86%85%E5%AE%B9 - else if (message.contentScriptQuery === 'patchDelFavoriteResources') { - const url = 'https://api.bilibili.com/x/v3/fav/resource/batch-del' - return fetch(url, { - method: 'POST', - headers: { - 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8', - }, - body: new URLSearchParams({ - resources: message.resources, - media_id: message.mediaId, - csrf: message.csrf, - }), - }) - .then(response => response.json()) - .then(data => data) - .catch(error => console.error(error)) - } +function handleMessage(message: any) { + // 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 + if (message.contentScriptQuery === 'getFavoriteCategories') { + const url = `https://api.bilibili.com/x/v3/fav/folder/created/list-all?up_mid=${message.mid}&jsonp=jsonp` + return fetch(url) + .then(response => response.json()) + .then(data => data) + .catch(error => console.error(error)) + } + // https://github.com/SocialSisterYi/bilibili-API-collect/blob/master/fav/list.md#%E8%8E%B7%E5%8F%96%E6%94%B6%E8%97%8F%E5%A4%B9%E5%86%85%E5%AE%B9%E6%98%8E%E7%BB%86%E5%88%97%E8%A1%A8 + else if (message.contentScriptQuery === 'getFavoriteResources') { + const url = `https://api.bilibili.com/x/v3/fav/resource/list?media_id=${message.mediaId}&pn=${message.pageNum}&ps=20&keyword=${message.keyword}&order=mtime&type=0&tid=0&platform=web&jsonp=jsonp` + return fetch(url) + .then(response => response.json()) + .then(data => data) + .catch(error => console.error(error)) + } + // https://github.com/SocialSisterYi/bilibili-API-collect/blob/master/docs/fav/action.md#%E6%89%B9%E9%87%8F%E5%88%A0%E9%99%A4%E5%86%85%E5%AE%B9 + else if (message.contentScriptQuery === 'patchDelFavoriteResources') { + const url = 'https://api.bilibili.com/x/v3/fav/resource/batch-del' + return fetch(url, { + method: 'POST', + headers: { + 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8', + }, + body: new URLSearchParams({ + resources: message.resources, + media_id: message.mediaId, + csrf: message.csrf, + }), }) - }) + .then(response => response.json()) + .then(data => data) + .catch(error => console.error(error)) + } +} + +function handleConnect() { + browser.runtime.onMessage.removeListener(handleMessage) + browser.runtime.onMessage.addListener(handleMessage) +} + +export function setupFavoriteMsgLstnr() { + browser.runtime.onConnect.removeListener(handleConnect) + browser.runtime.onConnect.addListener(handleConnect) } diff --git a/src/background/messageListeners/history.ts b/src/background/messageListeners/history.ts index b85bbb0a..f0a1d8df 100644 --- a/src/background/messageListeners/history.ts +++ b/src/background/messageListeners/history.ts @@ -1,75 +1,81 @@ import browser from 'webextension-polyfill' -export function 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 - if (message.contentScriptQuery === 'getHistoryList') { - const url = `https://api.bilibili.com/x/web-interface/history/cursor?ps=20&type=${message.type}&view_at=${message.viewAt}` - return fetch(url) - .then(response => response.json()) - .then(data => data) - .catch(error => console.error(error)) - } - else if (message.contentScriptQuery === 'searchHistoryList') { - const url = `https://api.bilibili.com/x/web-goblin/history/search?pn=${message.pn}&keyword=${message.keyword}&business=all` - return fetch(url) - .then(response => response.json()) - .then(data => data) - .catch(error => console.error(error)) - } - // https://github.com/SocialSisterYi/bilibili-API-collect/blob/master/history&toview/history.md#%E5%88%A0%E9%99%A4%E5%8E%86%E5%8F%B2%E8%AE%B0%E5%BD%95 - else if (message.contentScriptQuery === 'deleteHistoryItem') { - const url = 'https://api.bilibili.com/x/v2/history/delete' - return fetch(url, { - method: 'POST', - headers: { - 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8', - }, - body: new URLSearchParams({ - kid: message.kid, - csrf: message.csrf, - }), - }) - .then(response => response.json()) - .then(data => data) - .catch(error => console.error(error)) - } - // https://github.com/SocialSisterYi/bilibili-API-collect/blob/master/history&toview/history.md#%E6%B8%85%E7%A9%BA%E5%8E%86%E5%8F%B2%E8%AE%B0%E5%BD%95 - else if (message.contentScriptQuery === 'clearAllHistory') { - const url = 'https://api.bilibili.com/x/v2/history/clear' - return fetch(url, { - method: 'POST', - body: new URLSearchParams({ - csrf: message.csrf, - }), - }) - .then(response => response.json()) - .then(data => data) - .catch(error => console.error(error)) - } - // https://github.com/SocialSisterYi/bilibili-API-collect/blob/master/history&toview/history.md#%E6%9F%A5%E8%AF%A2%E5%8E%86%E5%8F%B2%E8%AE%B0%E5%BD%95%E5%81%9C%E7%94%A8%E7%8A%B6%E6%80%81 - else if (message.contentScriptQuery === 'getHistoryPauseStatus') { - const url = 'https://api.bilibili.com/x/v2/history/shadow' - return fetch(url) - .then(response => response.json()) - .then(data => data) - .catch(error => console.error(error)) - } - // https://github.com/SocialSisterYi/bilibili-API-collect/blob/master/history&toview/history.md#%E5%81%9C%E7%94%A8%E5%8E%86%E5%8F%B2%E8%AE%B0%E5%BD%95 - else if (message.contentScriptQuery === 'setHistoryPauseStatus') { - const url = 'https://api.bilibili.com/x/v2/history/shadow/set' - return fetch(url, { - method: 'POST', - body: new URLSearchParams({ - switch: message.switch, - csrf: message.csrf, - }), - }) - .then(response => response.json()) - .then(data => data) - .catch(error => console.error(error)) - } +function handleMessage(message: any) { + // 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 + if (message.contentScriptQuery === 'getHistoryList') { + const url = `https://api.bilibili.com/x/web-interface/history/cursor?ps=20&type=${message.type}&view_at=${message.viewAt}` + return fetch(url) + .then(response => response.json()) + .then(data => data) + .catch(error => console.error(error)) + } + else if (message.contentScriptQuery === 'searchHistoryList') { + const url = `https://api.bilibili.com/x/web-goblin/history/search?pn=${message.pn}&keyword=${message.keyword}&business=all` + return fetch(url) + .then(response => response.json()) + .then(data => data) + .catch(error => console.error(error)) + } + // https://github.com/SocialSisterYi/bilibili-API-collect/blob/master/history&toview/history.md#%E5%88%A0%E9%99%A4%E5%8E%86%E5%8F%B2%E8%AE%B0%E5%BD%95 + else if (message.contentScriptQuery === 'deleteHistoryItem') { + const url = 'https://api.bilibili.com/x/v2/history/delete' + return fetch(url, { + method: 'POST', + headers: { + 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8', + }, + body: new URLSearchParams({ + kid: message.kid, + csrf: message.csrf, + }), }) - }) + .then(response => response.json()) + .then(data => data) + .catch(error => console.error(error)) + } + // https://github.com/SocialSisterYi/bilibili-API-collect/blob/master/history&toview/history.md#%E6%B8%85%E7%A9%BA%E5%8E%86%E5%8F%B2%E8%AE%B0%E5%BD%95 + else if (message.contentScriptQuery === 'clearAllHistory') { + const url = 'https://api.bilibili.com/x/v2/history/clear' + return fetch(url, { + method: 'POST', + body: new URLSearchParams({ + csrf: message.csrf, + }), + }) + .then(response => response.json()) + .then(data => data) + .catch(error => console.error(error)) + } + // https://github.com/SocialSisterYi/bilibili-API-collect/blob/master/history&toview/history.md#%E6%9F%A5%E8%AF%A2%E5%8E%86%E5%8F%B2%E8%AE%B0%E5%BD%95%E5%81%9C%E7%94%A8%E7%8A%B6%E6%80%81 + else if (message.contentScriptQuery === 'getHistoryPauseStatus') { + const url = 'https://api.bilibili.com/x/v2/history/shadow' + return fetch(url) + .then(response => response.json()) + .then(data => data) + .catch(error => console.error(error)) + } + // https://github.com/SocialSisterYi/bilibili-API-collect/blob/master/history&toview/history.md#%E5%81%9C%E7%94%A8%E5%8E%86%E5%8F%B2%E8%AE%B0%E5%BD%95 + else if (message.contentScriptQuery === 'setHistoryPauseStatus') { + const url = 'https://api.bilibili.com/x/v2/history/shadow/set' + return fetch(url, { + method: 'POST', + body: new URLSearchParams({ + switch: message.switch, + csrf: message.csrf, + }), + }) + .then(response => response.json()) + .then(data => data) + .catch(error => console.error(error)) + } +} + +function handleConnect() { + browser.runtime.onMessage.removeListener(handleMessage) + browser.runtime.onMessage.addListener(handleMessage) +} + +export function setupHistoryMsgLstnr() { + browser.runtime.onConnect.removeListener(handleConnect) + browser.runtime.onConnect.addListener(handleConnect) } diff --git a/src/background/messageListeners/moment.ts b/src/background/messageListeners/moment.ts index 5992621d..24e57d31 100644 --- a/src/background/messageListeners/moment.ts +++ b/src/background/messageListeners/moment.ts @@ -1,59 +1,65 @@ import browser from 'webextension-polyfill' -export function setupMomentMsgLstnr() { - browser.runtime.onConnect.addListener(() => { - browser.runtime.onMessage.addListener((message) => { - if (message.contentScriptQuery === 'getNewMomentsCount') { - const url = 'https://api.bilibili.com/x/web-interface/dynamic/entrance' - return fetch(url) - .then(response => response.json()) - .then(data => (data)) - .catch(error => console.error(error)) - } +function handleMessage(message: any) { + if (message.contentScriptQuery === 'getNewMomentsCount') { + const url = 'https://api.bilibili.com/x/web-interface/dynamic/entrance' + return fetch(url) + .then(response => response.json()) + .then(data => (data)) + .catch(error => console.error(error)) + } - // v2 get moment list - // else if (message.contentScriptQuery === 'getNewMoments') { - // // type: video | article - // const url = `https://api.bilibili.com/x/polymer/web-dynamic/v1/feed/nav?type=${message.type}&update_baseline=${message.updateBaseline}` - // return fetch(url) - // .then(response => response.json()) - // .then(data => (data)) - // .catch(error => console.error(error)) - // } - // else if (message.contentScriptQuery === 'getLiveMoments') { - // const url = `https://api.live.bilibili.com/xlive/web-ucenter/v1/xfetter/FeedList?page=${message.page}&pagesize=10` - // return fetch(url) - // .then(response => response.json()) - // .then(data => (data)) - // .catch(error => console.error(error)) - // } + // v2 get moment list + // else if (message.contentScriptQuery === 'getNewMoments') { + // // type: video | article + // const url = `https://api.bilibili.com/x/polymer/web-dynamic/v1/feed/nav?type=${message.type}&update_baseline=${message.updateBaseline}` + // return fetch(url) + // .then(response => response.json()) + // .then(data => (data)) + // .catch(error => console.error(error)) + // } + // else if (message.contentScriptQuery === 'getLiveMoments') { + // const url = `https://api.live.bilibili.com/xlive/web-ucenter/v1/xfetter/FeedList?page=${message.page}&pagesize=10` + // return fetch(url) + // .then(response => response.json()) + // .then(data => (data)) + // .catch(error => console.error(error)) + // } - else if (message.contentScriptQuery === 'getNewMoments') { - const url = `https://api.vc.bilibili.com/dynamic_svr/v1/dynamic_svr/dynamic_new?uid=${message.uid} - &type_list=${message.typeList}` - return fetch(url) - .then(response => response.json()) - .then(data => (data)) - .catch(error => console.error(error)) - } + else if (message.contentScriptQuery === 'getNewMoments') { + const url = `https://api.vc.bilibili.com/dynamic_svr/v1/dynamic_svr/dynamic_new?uid=${message.uid} + &type_list=${message.typeList}` + return fetch(url) + .then(response => response.json()) + .then(data => (data)) + .catch(error => console.error(error)) + } - else if (message.contentScriptQuery === 'getHistoryMoments') { - const url = `https://api.vc.bilibili.com/dynamic_svr/v1/dynamic_svr/dynamic_history?uid=${message.uid} - &type_list=${message.typeList} - &offset_dynamic_id=${message.offsetDynamicID}` - return fetch(url) - .then(response => response.json()) - .then(data => (data)) - .catch(error => console.error(error)) - } + else if (message.contentScriptQuery === 'getHistoryMoments') { + const url = `https://api.vc.bilibili.com/dynamic_svr/v1/dynamic_svr/dynamic_history?uid=${message.uid} + &type_list=${message.typeList} + &offset_dynamic_id=${message.offsetDynamicID}` + return fetch(url) + .then(response => response.json()) + .then(data => (data)) + .catch(error => console.error(error)) + } - else if (message.contentScriptQuery === 'getLiveMoments') { - const url = `https://api.live.bilibili.com/xlive/web-ucenter/v1/xfetter/FeedList?page=${message.page}&pagesize=${message.pageSize}` - return fetch(url) - .then(response => response.json()) - .then(data => (data)) - .catch(error => console.error(error)) - } - }) - }) + else if (message.contentScriptQuery === 'getLiveMoments') { + const url = `https://api.live.bilibili.com/xlive/web-ucenter/v1/xfetter/FeedList?page=${message.page}&pagesize=${message.pageSize}` + return fetch(url) + .then(response => response.json()) + .then(data => (data)) + .catch(error => console.error(error)) + } +} + +function handleConnect() { + browser.runtime.onMessage.removeListener(handleMessage) + browser.runtime.onMessage.addListener(handleMessage) +} + +export function setupMomentMsgLstnr() { + browser.runtime.onConnect.removeListener(handleConnect) + browser.runtime.onConnect.addListener(handleConnect) } diff --git a/src/background/messageListeners/notification.ts b/src/background/messageListeners/notification.ts index 0e8c30bd..0c35c94f 100644 --- a/src/background/messageListeners/notification.ts +++ b/src/background/messageListeners/notification.ts @@ -1,23 +1,29 @@ import browser from 'webextension-polyfill' -export function setupNotificationMsgLstnr() { - browser.runtime.onConnect.addListener(() => { - browser.runtime.onMessage.addListener((message) => { - if (message.contentScriptQuery === 'getUnreadMsg') { - const url = 'https://api.bilibili.com/x/msgfeed/unread?build=0&mobi_app=web' - return fetch(url) - .then(response => response.json()) - .then(data => (data)) - .catch(error => console.error(error)) - } +function handleMessage(message: any) { + if (message.contentScriptQuery === 'getUnreadMsg') { + const url = 'https://api.bilibili.com/x/msgfeed/unread?build=0&mobi_app=web' + return fetch(url) + .then(response => response.json()) + .then(data => (data)) + .catch(error => console.error(error)) + } - else if (message.contentScriptQuery === 'getUnreadDm') { - const url = 'https://api.vc.bilibili.com/session_svr/v1/session_svr/single_unread?build=0&mobi_app=web&unread_type=0' - return fetch(url) - .then(response => response.json()) - .then(data => (data)) - .catch(error => console.error(error)) - } - }) - }) + else if (message.contentScriptQuery === 'getUnreadDm') { + const url = 'https://api.vc.bilibili.com/session_svr/v1/session_svr/single_unread?build=0&mobi_app=web&unread_type=0' + return fetch(url) + .then(response => response.json()) + .then(data => (data)) + .catch(error => console.error(error)) + } +} + +function handleConnect() { + browser.runtime.onMessage.removeListener(handleMessage) + browser.runtime.onMessage.addListener(handleMessage) +} + +export function setupNotificationMsgLstnr() { + browser.runtime.onConnect.removeListener(handleConnect) + browser.runtime.onConnect.addListener(handleConnect) } diff --git a/src/background/messageListeners/search.ts b/src/background/messageListeners/search.ts index 6a8a417d..2f269a0e 100644 --- a/src/background/messageListeners/search.ts +++ b/src/background/messageListeners/search.ts @@ -1,15 +1,21 @@ import browser from 'webextension-polyfill' -export function setupSearchMsgLstnr() { - browser.runtime.onConnect.addListener(() => { - browser.runtime.onMessage.addListener((message) => { - if (message.contentScriptQuery === 'getSearchSuggestion') { - const url = `https://s.search.bilibili.com/main/suggest?term=${message.term}` - return fetch(url) - .then(response => response.json()) - .then(data => (data)) - .catch(error => console.error(error)) - } - }) - }) +function handleMessage(message: any) { + if (message.contentScriptQuery === 'getSearchSuggestion') { + const url = `https://s.search.bilibili.com/main/suggest?term=${message.term}` + return fetch(url) + .then(response => response.json()) + .then(data => (data)) + .catch(error => console.error(error)) + } +} + +function handleConnect() { + browser.runtime.onMessage.removeListener(handleMessage) + browser.runtime.onMessage.addListener(handleMessage) +} + +export function setupSearchMsgLstnr() { + browser.runtime.onConnect.removeListener(handleConnect) + browser.runtime.onConnect.addListener(handleConnect) } diff --git a/src/background/messageListeners/user.ts b/src/background/messageListeners/user.ts index b55f0041..84823077 100644 --- a/src/background/messageListeners/user.ts +++ b/src/background/messageListeners/user.ts @@ -1,24 +1,30 @@ import browser from 'webextension-polyfill' -export function setupUserMsgLstnr() { - browser.runtime.onConnect.addListener(() => { - browser.runtime.onMessage.addListener((message) => { - // https://github.com/SocialSisterYi/bilibili-API-collect/blob/e379d904c2753fa30e9083f59016f07e89d19467/docs/login/login_info.md#%E5%AF%BC%E8%88%AA%E6%A0%8F%E7%94%A8%E6%88%B7%E4%BF%A1%E6%81%AF - if (message.contentScriptQuery === 'getUserInfo') { - const url = 'https://api.bilibili.com/x/web-interface/nav' - return fetch(url) - .then(response => response.json()) - .then(data => (data)) - .catch(error => console.error(error)) - } +function handleMessage(message: any) { + // https://github.com/SocialSisterYi/bilibili-API-collect/blob/e379d904c2753fa30e9083f59016f07e89d19467/docs/login/login_info.md#%E5%AF%BC%E8%88%AA%E6%A0%8F%E7%94%A8%E6%88%B7%E4%BF%A1%E6%81%AF + if (message.contentScriptQuery === 'getUserInfo') { + const url = 'https://api.bilibili.com/x/web-interface/nav' + return fetch(url) + .then(response => response.json()) + .then(data => (data)) + .catch(error => console.error(error)) + } - else if (message.contentScriptQuery === 'getUserStat') { - const url = 'https://api.bilibili.com/x/web-interface/nav/stat' - return fetch(url) - .then(response => response.json()) - .then(data => (data)) - .catch(error => console.error(error)) - } - }) - }) + else if (message.contentScriptQuery === 'getUserStat') { + const url = 'https://api.bilibili.com/x/web-interface/nav/stat' + return fetch(url) + .then(response => response.json()) + .then(data => (data)) + .catch(error => console.error(error)) + } +} + +function handleConnect() { + browser.runtime.onMessage.removeListener(handleMessage) + browser.runtime.onMessage.addListener(handleMessage) +} + +export function setupUserMsgLstnr() { + browser.runtime.onConnect.removeListener(handleConnect) + browser.runtime.onConnect.addListener(handleConnect) } diff --git a/src/background/messageListeners/video.ts b/src/background/messageListeners/video.ts index d3b43f65..b8da4691 100644 --- a/src/background/messageListeners/video.ts +++ b/src/background/messageListeners/video.ts @@ -1,92 +1,98 @@ import browser from 'webextension-polyfill' -export function setupVideoMsgLstnr() { - browser.runtime.onConnect.addListener(() => { - browser.runtime.onMessage.addListener((message) => { - // #region APP端api,遺棄 - /** Recommend Videos */ - // if (message.contentScriptQuery === 'getRecommendVideos') { - // // https://github.com/indefined/UserScripts/blob/master/bilibiliHome/bilibiliHome.API.md#%E8%8E%B7%E5%8F%96%E9%A6%96%E9%A1%B5%E5%86%85%E5%AE%B9 - // const url = `${APP_URL}/x/feed/index?build=1&idx=${message.idx}&appkey=27eb53fc9058f8c3&access_key=${message.accessKey}` - // return fetch(url) - // .then(response => response.json()) - // .then(data => data) - // .catch(error => console.error(error)) - // } - // /** Submit a video that is not of interest */ - // else if (message.contentScriptQuery === 'submitDislike') { - // // https://github.com/indefined/UserScripts/blob/master/bilibiliHome/bilibiliHome.API.md#%E6%8F%90%E4%BA%A4%E4%B8%8D%E5%96%9C%E6%AC%A2 - // let url = `https://app.bilibili.com/x/feed/dislike?access_key=${message.accessKey} - // &goto=${message.goto} - // &id=${message.id} - // &mid=${message.mid} - // &reason_id=${message.reasonID} - // &rid=${message.rid} - // &tag_id=${message.tagID} - // &build=5000000` +function handleMessage(message: any) { +// #region APP端api,遺棄 + /** Recommend Videos */ + // if (message.contentScriptQuery === 'getRecommendVideos') { + // // https://github.com/indefined/UserScripts/blob/master/bilibiliHome/bilibiliHome.API.md#%E8%8E%B7%E5%8F%96%E9%A6%96%E9%A1%B5%E5%86%85%E5%AE%B9 + // const url = `${APP_URL}/x/feed/index?build=1&idx=${message.idx}&appkey=27eb53fc9058f8c3&access_key=${message.accessKey}` + // return fetch(url) + // .then(response => response.json()) + // .then(data => data) + // .catch(error => console.error(error)) + // } + // /** Submit a video that is not of interest */ + // else if (message.contentScriptQuery === 'submitDislike') { + // // https://github.com/indefined/UserScripts/blob/master/bilibiliHome/bilibiliHome.API.md#%E6%8F%90%E4%BA%A4%E4%B8%8D%E5%96%9C%E6%AC%A2 + // let url = `https://app.bilibili.com/x/feed/dislike?access_key=${message.accessKey} + // &goto=${message.goto} + // &id=${message.id} + // &mid=${message.mid} + // &reason_id=${message.reasonID} + // &rid=${message.rid} + // &tag_id=${message.tagID} + // &build=5000000` - // // remove url empty spaces - // url = url.replace(/\s+/g, '') + // // remove url empty spaces + // url = url.replace(/\s+/g, '') - // return fetch(url) - // .then(response => response.json()) - // .then(data => data) - // .catch(error => console.error(error)) - // } + // return fetch(url) + // .then(response => response.json()) + // .then(data => data) + // .catch(error => console.error(error)) + // } - // /** Undo a video that is not of interest */ - // else if (message.contentScriptQuery === 'undoDislike') { - // // https://github.com/indefined/UserScripts/blob/master/bilibiliHome/bilibiliHome.API.md#%E6%92%A4%E9%94%80%E4%B8%8D%E5%96%9C%E6%AC%A2 - // let url = `https://app.bilibili.com/x/feed/dislike/cancel?access_key=${message.accessKey} - // &goto=${message.goto} - // &id=${message.id} - // &mid=${message.mid} - // &reason_id=${message.reasonID} - // &rid=${message.rid} - // &tag_id=${message.tagID} - // &build=5000000` + // /** Undo a video that is not of interest */ + // else if (message.contentScriptQuery === 'undoDislike') { + // // https://github.com/indefined/UserScripts/blob/master/bilibiliHome/bilibiliHome.API.md#%E6%92%A4%E9%94%80%E4%B8%8D%E5%96%9C%E6%AC%A2 + // let url = `https://app.bilibili.com/x/feed/dislike/cancel?access_key=${message.accessKey} + // &goto=${message.goto} + // &id=${message.id} + // &mid=${message.mid} + // &reason_id=${message.reasonID} + // &rid=${message.rid} + // &tag_id=${message.tagID} + // &build=5000000` - // // remove url empty spaces - // url = url.replace(/\s+/g, '') + // // remove url empty spaces + // url = url.replace(/\s+/g, '') - // return fetch(url) - // .then(response => response.json()) - // .then(data => data) - // .catch(error => console.error(error)) - // } - // #endregion + // return fetch(url) + // .then(response => response.json()) + // .then(data => data) + // .catch(error => console.error(error)) + // } + // #endregion - if (message.contentScriptQuery === 'getRecommendVideos') { - const url = `https://api.bilibili.com/x/web-interface/index/top/feed/rcmd?fresh_idx=${message.refreshIdx}&feed_version=V2&fresh_type=4&ps=30&plat=1` - return fetch(url) - .then(response => response.json()) - .then(data => data) - .catch(error => console.error(error)) - } - else if (message.contentScriptQuery === 'getAppRecommendVideos') { - const url = `https://app.bilibili.com/x/feed/index?build=1&idx=${message.idx}&appkey=27eb53fc9058f8c3&access_key=${message.accessKey}` - // const url = `https://app.bilibili.com/x/v2/feed/index?build=72100100&idx=${message.idx}&appkey=27eb53fc9058f8c3&access_key=${message.accessKey}` - return fetch(url) - .then(response => response.json()) - .then(data => data) - .catch(error => console.error(error)) - } - // https://github.com/SocialSisterYi/bilibili-API-collect/blob/master/docs/video/info.md#%E8%8E%B7%E5%8F%96%E8%A7%86%E9%A2%91%E8%B6%85%E8%AF%A6%E7%BB%86%E4%BF%A1%E6%81%AFweb%E7%AB%AF - else if (message.contentScriptQuery === 'getVideoInfo') { - const url = `https://api.bilibili.com/x/web-interface/view/detail?${message.aid ? `aid=${message.aid}` : `bvid=${message.bvid}`}` - return fetch(url) - .then(response => response.json()) - .then(data => data) - .catch(error => console.error(error)) - } - // https://github.com/SocialSisterYi/bilibili-API-collect/blob/master/docs/comment/list.md#%E8%8E%B7%E5%8F%96%E8%AF%84%E8%AE%BA%E5%8C%BA%E6%98%8E%E7%BB%86_%E7%BF%BB%E9%A1%B5%E5%8A%A0%E8%BD%BD - else if (message.contentScriptQuery === 'getVideoComments') { - const url = `https://api.bilibili.com/x/v2/reply?csrf=${message.csrf}&type=1&oid=${message.oid}&sort=${message.sort ?? 0}&nohot=${message.nohot ?? 0}&pn=${message.pn ?? 1}&ps=${message.ps ?? 20}` - return fetch(url) - .then(response => response.json()) - .then(data => data) - .catch(error => console.error(error)) - } - }) - }) + if (message.contentScriptQuery === 'getRecommendVideos') { + const url = `https://api.bilibili.com/x/web-interface/index/top/feed/rcmd?fresh_idx=${message.refreshIdx}&feed_version=V2&fresh_type=4&ps=30&plat=1` + return fetch(url) + .then(response => response.json()) + .then(data => data) + .catch(error => console.error(error)) + } + else if (message.contentScriptQuery === 'getAppRecommendVideos') { + const url = `https://app.bilibili.com/x/feed/index?build=1&idx=${message.idx}&appkey=27eb53fc9058f8c3&access_key=${message.accessKey}` + // const url = `https://app.bilibili.com/x/v2/feed/index?build=72100100&idx=${message.idx}&appkey=27eb53fc9058f8c3&access_key=${message.accessKey}` + return fetch(url) + .then(response => response.json()) + .then(data => data) + .catch(error => console.error(error)) + } + // https://github.com/SocialSisterYi/bilibili-API-collect/blob/master/docs/video/info.md#%E8%8E%B7%E5%8F%96%E8%A7%86%E9%A2%91%E8%B6%85%E8%AF%A6%E7%BB%86%E4%BF%A1%E6%81%AFweb%E7%AB%AF + else if (message.contentScriptQuery === 'getVideoInfo') { + const url = `https://api.bilibili.com/x/web-interface/view/detail?${message.aid ? `aid=${message.aid}` : `bvid=${message.bvid}`}` + return fetch(url) + .then(response => response.json()) + .then(data => data) + .catch(error => console.error(error)) + } + // https://github.com/SocialSisterYi/bilibili-API-collect/blob/master/docs/comment/list.md#%E8%8E%B7%E5%8F%96%E8%AF%84%E8%AE%BA%E5%8C%BA%E6%98%8E%E7%BB%86_%E7%BF%BB%E9%A1%B5%E5%8A%A0%E8%BD%BD + else if (message.contentScriptQuery === 'getVideoComments') { + const url = `https://api.bilibili.com/x/v2/reply?csrf=${message.csrf}&type=1&oid=${message.oid}&sort=${message.sort ?? 0}&nohot=${message.nohot ?? 0}&pn=${message.pn ?? 1}&ps=${message.ps ?? 20}` + return fetch(url) + .then(response => response.json()) + .then(data => data) + .catch(error => console.error(error)) + } +} + +function handleConnect() { + browser.runtime.onMessage.removeListener(handleMessage) + browser.runtime.onMessage.addListener(handleMessage) +} + +export function setupVideoMsgLstnr() { + browser.runtime.onConnect.removeListener(handleConnect) + browser.runtime.onConnect.addListener(handleConnect) } diff --git a/src/background/messageListeners/watchLater.ts b/src/background/messageListeners/watchLater.ts index 1074c99e..ab9e6b1d 100644 --- a/src/background/messageListeners/watchLater.ts +++ b/src/background/messageListeners/watchLater.ts @@ -1,58 +1,64 @@ import browser from 'webextension-polyfill' -export function 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?${message.aid ? `aid=${message.aid}` : ''}` - return fetch(url, { - method: 'POST', - body: new URLSearchParams({ - csrf: message.csrf, - // aid: message.aid, - viewed: message.viewed, - }), - }) - .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#%E8%8E%B7%E5%8F%96%E7%A8%8D%E5%90%8E%E5%86%8D%E7%9C%8B%E8%A7%86%E9%A2%91%E5%88%97%E8%A1%A8 - else if (message.contentScriptQuery === 'getAllWatchLaterList') { - const url = 'https://api.bilibili.com/x/v2/history/toview' - return fetch(url) - .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#%E6%B8%85%E7%A9%BA%E7%A8%8D%E5%90%8E%E5%86%8D%E7%9C%8B%E8%A7%86%E9%A2%91%E5%88%97%E8%A1%A8 - else if (message.contentScriptQuery === 'clearAllWatchLater') { - const url = 'https://api.bilibili.com/x/v2/history/toview/clear' - return fetch(url, { - method: 'POST', - body: new URLSearchParams({ - csrf: message.csrf, - }), - }) - .then(response => response.json()) - .then(data => data) - .catch(error => console.error(error)) - } +function handleMessage(message: any) { + // 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?${message.aid ? `aid=${message.aid}` : ''}` + return fetch(url, { + method: 'POST', + body: new URLSearchParams({ + csrf: message.csrf, + // aid: message.aid, + viewed: message.viewed, + }), + }) + .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#%E8%8E%B7%E5%8F%96%E7%A8%8D%E5%90%8E%E5%86%8D%E7%9C%8B%E8%A7%86%E9%A2%91%E5%88%97%E8%A1%A8 + else if (message.contentScriptQuery === 'getAllWatchLaterList') { + const url = 'https://api.bilibili.com/x/v2/history/toview' + return fetch(url) + .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#%E6%B8%85%E7%A9%BA%E7%A8%8D%E5%90%8E%E5%86%8D%E7%9C%8B%E8%A7%86%E9%A2%91%E5%88%97%E8%A1%A8 + else if (message.contentScriptQuery === 'clearAllWatchLater') { + const url = 'https://api.bilibili.com/x/v2/history/toview/clear' + return fetch(url, { + method: 'POST', + body: new URLSearchParams({ + csrf: message.csrf, + }), + }) + .then(response => response.json()) + .then(data => data) + .catch(error => console.error(error)) + } +} + +function handleConnect() { + browser.runtime.onMessage.removeListener(handleMessage) + browser.runtime.onMessage.addListener(handleMessage) +} + +export function setupWatchLaterMsgLstnr() { + browser.runtime.onConnect.removeListener(handleConnect) + browser.runtime.onConnect.addListener(handleConnect) }