lmislm / lmislm.github.io

hexo的配置
0 stars 0 forks source link

图片重写header #24

Open lmislm opened 9 months ago

lmislm commented 9 months ago
const fetchImage = async (imageSrc, imageToken) => {
  try {
    const response = await fetch(imageSrc.replace('http:', 'https:'), {
      headers: {
        Authorization: `Bearer ${imageToken}`,
        // 'Content-Type': 'application/json',
        // 'Access-Control-Allow-Origin': '*'
      }
      // credentials: 'include'
    });

    if (response.ok) {
      const blob = await response.blob();
      return new Promise((resolve, reject) => {
        const reader = new FileReader();
        reader.onloadend = () => {
          resolve(reader.result);
        };
        reader.onerror = (error) => {
          reject(new Error('Failed to read image data'));
        };
        reader.readAsDataURL(blob);
      });
    } else {
      throw new Error('Failed to fetch image');
    }
  } catch (error) {
    console.error('Error fetching image:', error);
    throw new Error('Failed to fetch image');
  }
}