mirror of
https://github.com/BewlyBewly/BewlyBewly.git
synced 2025-04-14 13:15:29 +00:00
fix: onMessage.addListener not working
This commit is contained in:
@@ -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))
|
||||
}
|
||||
})
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import browser from 'webextension-polyfill'
|
||||
import { API_URL } from '.'
|
||||
|
||||
export const setupHistoryAPIs = () => {
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import browser from 'webextension-polyfill'
|
||||
import { API_URL } from '.'
|
||||
|
||||
export const setupMomentsAPIs = () => {
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import browser from 'webextension-polyfill'
|
||||
import { API_URL } from '.'
|
||||
|
||||
export const setupNotificationsAPIs = () => {
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import browser from 'webextension-polyfill'
|
||||
|
||||
export const setupSearchAPIs = () => {
|
||||
browser.runtime.onMessage.addListener((message) => {
|
||||
if (message.contentScriptQuery === 'getSearchSuggestion') {
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import browser from 'webextension-polyfill'
|
||||
import { API_URL } from '.'
|
||||
|
||||
export const setupUserAPIs = () => {
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
})
|
||||
|
||||
@@ -121,4 +121,4 @@ browser.tabs.onUpdated.addListener((tabId: number, changInfo: Tabs.OnUpdatedChan
|
||||
})
|
||||
|
||||
// Setup APIs
|
||||
// setupAllAPIs()
|
||||
setupAllAPIs()
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user