微信公众号 替换 前端显示方案


const axios = require('axios');
const options = async (ctx, next) => {
  console.log( ctx.request.body);
  if(ctx.request.body.url){
    ctx.body = await getDagta(ctx.request.body.url)
  }else{
    ctx.body = '刁毛快传url'
}
};

const getDagta = (url) => {
  return new Promise((resolve, reject) => {
    axios.get(url)
    .then(response => {
      let src = response.data.replace(/data-src/g, 'src')
      .replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/g, '')
      .replace(/https/g, 'http')
      .replace(/visibility/g, " ")
      .replace(/opacity/g, " ")
      let htmlArr = src.split('</head>');
      // 解决公众号 不显示问题
      let html_src_add = htmlArr[0] + '<meta name="referrer" content="never"></head>' + htmlArr[1];
      resolve(html_src_add)
    })
    .catch(error => {
      reject(error)
    });
  });
}
module.exports = options;

 

posted @ 2024-07-25 14:20  前端搬运工bug  阅读(19)  评论(0)    收藏  举报