add cloudflare

This commit is contained in:
2025-03-18 12:55:32 +01:00
parent 940b8bbec4
commit a540f1eb0a
3 changed files with 63 additions and 43 deletions
+19 -4
View File
@@ -42,7 +42,14 @@ async function handleTabUpdate(url: string) {
const apiResponse = await fetch(`https://ip.albert.lol/${ip}`) const apiResponse = await fetch(`https://ip.albert.lol/${ip}`)
const apiData = await apiResponse.json() const apiData = await apiResponse.json()
await updateIcon(apiData.country || null) const country = apiData.country || null
const asn = apiData.org?.split(' ')[0]
let iconCode = country
if (!iconCode && asn === 'AS13335') {
iconCode = 'cloudflare'
}
await updateIcon(iconCode)
} catch (error) { } catch (error) {
console.error('Failed to handle tab update:', error) console.error('Failed to handle tab update:', error)
await updateIcon(null) await updateIcon(null)
@@ -60,7 +67,8 @@ browser.tabs.onUpdated.addListener(async (_tabId, changeInfo) => {
export default defineBackground({ export default defineBackground({
main() { main() {
browser.runtime.onMessage.addListener((request: any, _sender, sendResponse) => { browser.runtime.onMessage.addListener(
(request: any, _sender, sendResponse) => {
if (request.type === 'FETCH_SERVER_INFO') { if (request.type === 'FETCH_SERVER_INFO') {
;(async () => { ;(async () => {
try { try {
@@ -76,7 +84,13 @@ export default defineBackground({
const parsed = psl.parse(request.hostname) const parsed = psl.parse(request.hostname)
const origin = 'domain' in parsed ? parsed.domain : null const origin = 'domain' in parsed ? parsed.domain : null
await updateIcon(apiData.country) const country = apiData.country || null
const asn = apiData.org?.split(' ')[0]
let iconCode = country
if (!iconCode && asn === 'AS13335') {
iconCode = 'cloudflare'
}
await updateIcon(iconCode)
sendResponse({ sendResponse({
error: null, error: null,
@@ -102,6 +116,7 @@ export default defineBackground({
sendResponse({ error: 'Unknown request type', data: null }) sendResponse({ error: 'Unknown request type', data: null })
return true return true
}) }
)
}, },
}) })
Binary file not shown.

After

Width:  |  Height:  |  Size: 3.7 KiB

+7 -2
View File
@@ -1,9 +1,14 @@
export async function updateIcon(countryCode: string | null) { export async function updateIcon(countryCode: string | null) {
const validCode = let validCode
if (countryCode === 'cloudflare') {
validCode = 'cloudflare'
} else {
validCode =
countryCode?.match(/^[A-Z]{2}$/i)?.[0]?.toLowerCase() || 'unknown' countryCode?.match(/^[A-Z]{2}$/i)?.[0]?.toLowerCase() || 'unknown'
}
const loadImageBitmap = async (code: string): Promise<ImageBitmap> => { const loadImageBitmap = async (code: string): Promise<ImageBitmap> => {
const url = browser.runtime.getURL("/") const url = browser.runtime.getURL('/')
try { try {
const response = await fetch(url + `${code}.webp`) const response = await fetch(url + `${code}.webp`)
if (!response.ok) throw new Error('Flag not found') if (!response.ok) throw new Error('Flag not found')