fix: onMessage.addListener not working

This commit is contained in:
Hakadao
2022-10-19 00:55:19 +08:00
parent 837900c553
commit 1fe708b81b
9 changed files with 33 additions and 22 deletions

View File

@@ -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))
}
})

View File

@@ -1,3 +1,4 @@
import browser from 'webextension-polyfill'
import { API_URL } from '.'
export const setupHistoryAPIs = () => {

View File

@@ -1,3 +1,4 @@
import browser from 'webextension-polyfill'
import { API_URL } from '.'
export const setupMomentsAPIs = () => {

View File

@@ -1,3 +1,4 @@
import browser from 'webextension-polyfill'
import { API_URL } from '.'
export const setupNotificationsAPIs = () => {

View File

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

View File

@@ -1,3 +1,4 @@
import browser from 'webextension-polyfill'
import { API_URL } from '.'
export const setupUserAPIs = () => {

View File

@@ -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))
}
})

View File

@@ -121,4 +121,4 @@ browser.tabs.onUpdated.addListener((tabId: number, changInfo: Tabs.OnUpdatedChan
})
// Setup APIs
// setupAllAPIs()
setupAllAPIs()

View File

@@ -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