From 1fe708b81bc2995be5067c9733fdbb9de0b3d24f Mon Sep 17 00:00:00 2001 From: Hakadao Date: Wed, 19 Oct 2022 00:55:19 +0800 Subject: [PATCH] fix: onMessage.addListener not working --- src/background/apis/auth.ts | 16 ++++++++-------- src/background/apis/history.ts | 1 + src/background/apis/moments.ts | 1 + src/background/apis/notifications.ts | 1 + src/background/apis/search.ts | 2 ++ src/background/apis/user.ts | 1 + src/background/apis/videos.ts | 9 +++++---- src/background/index.ts | 2 +- src/utils/auth-provider.ts | 22 +++++++++++++--------- 9 files changed, 33 insertions(+), 22 deletions(-) diff --git a/src/background/apis/auth.ts b/src/background/apis/auth.ts index 1838857f..de9ebca5 100644 --- a/src/background/apis/auth.ts +++ b/src/background/apis/auth.ts @@ -1,22 +1,22 @@ +import browser from 'webextension-polyfill' + export const setupAuthAPIs = () => { - browser.runtime.onMessage.addListener((message) => { + browser.runtime.onMessage.addListener((message, sender, sendResponse) => { if (message.contentScriptQuery === 'getAccessKey') { const url = message.confirmUri - return fetch(url) - .then(response => ({ accessKey: `${response.url}`.match(/access_key=([0-9a-z]{32})/)![1] })) + fetch(url) + .then(response => sendResponse({ accessKey: `${response.url}`.match(/access_key=([0-9a-z]{32})/)![1] })) .catch(error => console.error(error)) - } - - if (message.contentScriptQuery === 'logout') { + } else if (message.contentScriptQuery === 'logout') { const url = `https://passport.bilibili.com/login/exit/v2?biliCSRF=${message.biliCSRF}` - return fetch(url, { + fetch(url, { method: 'POST', body: JSON.stringify({ biliCSRF: message.biliJct, }), }) .then(response => response.json()) - .then(data => (data)) + .then(data => sendResponse(data)) .catch(error => console.error(error)) } }) diff --git a/src/background/apis/history.ts b/src/background/apis/history.ts index d46fc7c7..a4795039 100644 --- a/src/background/apis/history.ts +++ b/src/background/apis/history.ts @@ -1,3 +1,4 @@ +import browser from 'webextension-polyfill' import { API_URL } from '.' export const setupHistoryAPIs = () => { diff --git a/src/background/apis/moments.ts b/src/background/apis/moments.ts index 1d0758ba..8da54149 100644 --- a/src/background/apis/moments.ts +++ b/src/background/apis/moments.ts @@ -1,3 +1,4 @@ +import browser from 'webextension-polyfill' import { API_URL } from '.' export const setupMomentsAPIs = () => { diff --git a/src/background/apis/notifications.ts b/src/background/apis/notifications.ts index f5decd07..a1328a6d 100644 --- a/src/background/apis/notifications.ts +++ b/src/background/apis/notifications.ts @@ -1,3 +1,4 @@ +import browser from 'webextension-polyfill' import { API_URL } from '.' export const setupNotificationsAPIs = () => { diff --git a/src/background/apis/search.ts b/src/background/apis/search.ts index fc31cba8..1ac4832f 100644 --- a/src/background/apis/search.ts +++ b/src/background/apis/search.ts @@ -1,3 +1,5 @@ +import browser from 'webextension-polyfill' + export const setupSearchAPIs = () => { browser.runtime.onMessage.addListener((message) => { if (message.contentScriptQuery === 'getSearchSuggestion') { diff --git a/src/background/apis/user.ts b/src/background/apis/user.ts index e7d8e98e..0857d905 100644 --- a/src/background/apis/user.ts +++ b/src/background/apis/user.ts @@ -1,3 +1,4 @@ +import browser from 'webextension-polyfill' import { API_URL } from '.' export const setupUserAPIs = () => { diff --git a/src/background/apis/videos.ts b/src/background/apis/videos.ts index 0178ee4c..09e1184e 100644 --- a/src/background/apis/videos.ts +++ b/src/background/apis/videos.ts @@ -1,13 +1,14 @@ +import browser from 'webextension-polyfill' import { APP_URL } from '.' export const setupVideosAPIs = () => { - browser.runtime.onMessage.addListener((message) => { + browser.runtime.onMessage.addListener(message => { /** Recommend Videos */ if (message.contentScriptQuery === 'getRecommendVideos') { 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)) + .then(data => data) .catch(error => console.error(error)) } @@ -28,7 +29,7 @@ export const setupVideosAPIs = () => { return fetch(url) .then(response => response.json()) - .then(data => (data)) + .then(data => data) .catch(error => console.error(error)) } @@ -49,7 +50,7 @@ export const setupVideosAPIs = () => { return fetch(url) .then(response => response.json()) - .then(data => (data)) + .then(data => data) .catch(error => console.error(error)) } }) diff --git a/src/background/index.ts b/src/background/index.ts index 35f76a03..e4203ba7 100644 --- a/src/background/index.ts +++ b/src/background/index.ts @@ -121,4 +121,4 @@ browser.tabs.onUpdated.addListener((tabId: number, changInfo: Tabs.OnUpdatedChan }) // Setup APIs -// setupAllAPIs() +setupAllAPIs() diff --git a/src/utils/auth-provider.ts b/src/utils/auth-provider.ts index ccf317cd..d74c2f0e 100644 --- a/src/utils/auth-provider.ts +++ b/src/utils/auth-provider.ts @@ -1,4 +1,5 @@ /* eslint-disable no-throw-literal */ +import browser from 'webextension-polyfill' import { accessKey } from '~/logic/storage' /** @@ -21,18 +22,19 @@ export const grantAccessKey = (element: HTMLButtonElement): void => { const tip = 'Failed to grant Access Key' fetch( - 'https://passport.bilibili.com/login/app/third?appkey=27eb53fc9058f8c3' - + '&api=https%3A%2F%2Fwww.mcbbs.net%2Ftemplate%2Fmcbbs%2Fimage%2Fspecial_photo_bg.png&sign=04224646d1fea004e79606d3b038c84a', + 'https://passport.bilibili.com/login/app/third?appkey=27eb53fc9058f8c3' + + '&api=https%3A%2F%2Fwww.mcbbs.net%2Ftemplate%2Fmcbbs%2Fimage%2Fspecial_photo_bg.png&sign=04224646d1fea004e79606d3b038c84a', { method: 'GET', credentials: 'include', - }, + } ) .then(res => res.json()) - .then((data) => { + .then(data => { if (data.code || !data.data) throw { tip, msg: data.msg || data.message || data.code, data } else if (!data.data.has_login) throw { tip, msg: 'Please login to bilibili first', data } - else if (!data.data.confirm_uri) throw { tip, msg: 'Unable to receive verified URL. Please go back and try againe.', data } + else if (!data.data.confirm_uri) + throw { tip, msg: 'Unable to receive verified URL. Please go back and try againe.', data } else return data.data.confirm_uri }) .then( @@ -42,16 +44,18 @@ export const grantAccessKey = (element: HTMLButtonElement): void => { .sendMessage({ contentScriptQuery: 'getAccessKey', confirmUri: url, - }).then((res: {accessKey: string}) => { + }) + .then((res: { accessKey: string }) => { accessKey.value = res.accessKey resolve() - }).catch((err: any) => { + }) + .catch((err: any) => { // eslint-disable-next-line prefer-promise-reject-errors reject({ tip, msg: 'Failed to get Access Key', data: err }) }) - }), + }) ) - .catch((error) => { + .catch(error => { element.innerHTML = orginalInnerHTML element.style.pointerEvents = 'auto' element.disabled = false