Skip to content

Commit bb87c13

Browse files
authored
Merge pull request #70 from log-z/dev 修复 GitHub 阻止中文语言浏览器请求问题 fk-gh
修复 GitHub 阻止中文语言浏览器请求问题 fk-gh
2 parents ae69a57 + aa4974c commit bb87c13

File tree

3 files changed

+94
-13
lines changed

3 files changed

+94
-13
lines changed

src/components/nav-favorites/nav-favorite-item.vue

+61-12
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ export default {
6969
<script setup>
7070
import { computed, onMounted, reactive, watch } from 'vue'
7171
import $_ from 'lodash'
72-
import axios from 'axios'
7372
import { useConfigStore } from '@/stores/config'
7473
import { httpAbsPath } from '@/utils/common'
74+
import { imgApi, ghCli, isGithubUrl } from '@/utils/request'
7575
7676
const configStore = useConfigStore()
7777
@@ -81,36 +81,85 @@ const state = reactive({
8181
isActive: false,
8282
iconB64: null,
8383
iconB64OnDark: null,
84+
iconBlobUrl: null,
85+
iconBlobUrlOnDark: null,
8486
})
8587
8688
// 图标URL
89+
// 图标URL默认值
90+
const DEFAULT_ICON_URL = '#'
8791
// 图标URL(常规)
8892
const iconUrl = computed(() => {
89-
const path = props.website.icon;
90-
if ($_.isEmpty(path)) {
91-
return '#'
93+
const path = state.iconBlobUrl || props.website.icon;
94+
if ($_.isEmpty(path) || path === DEFAULT_ICON_URL) {
95+
return DEFAULT_ICON_URL
96+
}
97+
if (path.startsWith('blob:')) {
98+
return path
9299
}
93100
94101
const baseUrl = configStore.baseUrl
95102
const prefix = configStore.config.favorites.iconPrefix
96-
return httpAbsPath(path, baseUrl + prefix)
103+
const url = httpAbsPath(path, baseUrl + prefix)
104+
return fetchIconUrl(url, false)
97105
})
98106
// 图标URL(暗色模式)
99107
const iconUrlOnDark = computed(() => {
100-
let path = props.website.iconOnDark;
101-
if ($_.isEmpty(path)) {
108+
let path = state.iconBlobUrlOnDark || props.website.iconOnDark;
109+
if ($_.isEmpty(path) || path === DEFAULT_ICON_URL) {
102110
// 降级到正常图标
103-
path = props.website.icon;
111+
path = state.iconBlobUrl || props.website.icon;
104112
if ($_.isEmpty(path)) {
105-
return '#'
113+
return DEFAULT_ICON_URL
106114
}
107115
}
116+
if (path.startsWith('blob:')) {
117+
return path
118+
}
108119
109120
const baseUrl = configStore.baseUrl
110121
const prefix = configStore.config.favorites.iconPrefix
111-
return httpAbsPath(path, baseUrl + prefix)
122+
const url = httpAbsPath(path, baseUrl + prefix)
123+
return fetchIconUrl(url, true)
112124
})
113125
126+
// 对某些站点特殊处理图标URL
127+
const ghIconCache = {}
128+
function fetchIconUrl(url, onDark) {
129+
130+
// 特殊处理 GitHub URL
131+
// 避免 GitHub 阻止使用中文语言的浏览器请求
132+
// 先通过 XHR 请求图标,然后转换为 BlobUrl 进行展示
133+
if (isGithubUrl(url) && checkNotBase64(url)) {
134+
const cacheUrl = ghIconCache[url]
135+
if (cacheUrl) {
136+
return cacheUrl
137+
}
138+
139+
ghCli.get(url)
140+
.then(resp => {
141+
const blob = new Blob([resp.data], { type: resp.headers['content-type'] })
142+
const blobUrl = URL.createObjectURL(blob)
143+
ghIconCache[url] = blobUrl
144+
if (onDark) {
145+
state.iconBlobUrlOnDark = blobUrl
146+
} else {
147+
state.iconBlobUrl = blobUrl
148+
}
149+
})
150+
.catch(() => {
151+
if (onDark) {
152+
state.iconBlobUrlOnDark = DEFAULT_ICON_URL
153+
} else {
154+
state.iconBlobUrl = DEFAULT_ICON_URL
155+
}
156+
})
157+
return DEFAULT_ICON_URL
158+
}
159+
160+
return url
161+
}
162+
114163
// 图标Base64探测
115164
// 检查图标是否为Base64扩展名的文件
116165
const BASE64_FILE_EXTENSION = '.base64'
@@ -124,7 +173,7 @@ const refreshIconB64 = (url) => {
124173
state.iconB64 = null
125174
return
126175
}
127-
axios.get(url)
176+
imgApi(url)
128177
.then(resp => {
129178
state.iconB64 = resp.data
130179
})
@@ -135,7 +184,7 @@ const refreshIconB64OnDark = (url) => {
135184
state.iconB64OnDark = null
136185
return
137186
}
138-
axios.get(url)
187+
imgApi(url)
139188
.then(resp => {
140189
state.iconB64OnDark = resp.data
141190
})

src/utils/config/source.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import YAML from 'yaml'
55
const request = axios.create({
66
headers: {
77
get: {
8-
'Accept': 'text/plain'
8+
'Accept': 'text/plain',
9+
'Accept-Language': '', // 避免 GitHub 阻止使用中文语言的浏览器请求
910
}
1011
},
1112
transformResponse: httpTransformResponse,

src/utils/request/index.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import axios from 'axios'
2+
3+
// 通用客户端
4+
export const commCli = axios.create()
5+
6+
// GitHub 客户端
7+
// 避免 GitHub 阻止使用中文语言的浏览器请求
8+
export const ghCli = axios.create({
9+
headers: {
10+
common: {
11+
'Accept-Language': '',
12+
}
13+
}
14+
})
15+
16+
// 检查是否是 GitHub URL
17+
const ghUrlPrefix = [
18+
'http://raw.githubusercontent.com/',
19+
'https://raw.githubusercontent.com/',
20+
'http://gist.githubusercontent.com/',
21+
'https://gist.githubusercontent.com/',
22+
]
23+
export function isGithubUrl(url) {
24+
return typeof url === "string" &&
25+
ghUrlPrefix.find(prefix => url.startsWith(prefix)) !== undefined
26+
}
27+
28+
// 图片请求API
29+
export function imgApi(url) {
30+
return isGithubUrl(url) ? ghCli.get(url) : commCli.get(url)
31+
}

0 commit comments

Comments
 (0)