宜信同城网交流论坛

 找回密码
 立即注册
开启左侧

企业微信设置网页分享卡片

[复制链接]
发表于 2023-3-1 19:08:00 | 显示全部楼层 |阅读模式 来自 中国福建福州

企业微信设置网页分享卡片

企业微信会自动读取网页 title, html body 内的第一段文字和第一个图片(也有一些情况会使用网页的 favicon.ico)作为分享卡片的标题,描述,和图片。

这三个元素进行修改就好

注意:图片需要分辨率在 300x300 以上,否则可能不显示

setFirstImageAndFavicon(this.shareConfig.imgUrl)
setDescription(this.shareConfig.title)
setDocumentTitle(this.shareConfig.title)

const setFirstImageAndFavicon = (faviconUrl) => {
  let link = window.document.querySelector("link[rel*='icon']") || window.document.createElement('link');
  link.rel = 'shortcut icon';
  link.href = faviconUrl;
  window.document.getElementsByTagName('head')[0].appendChild(link);
  window.document.getElementById('first-image').src = faviconUrl // first-image 为网页里的第一个图片的id,可以设置为 opacity: 0; height: 0; 来不显示。
}

const setDescription = (title) => {
  window.document.getElementById('first-p').html = title // first-p 为网页里的第一行文字所在标签的id,可以设置为 opacity: 0; height: 0; 来不显示。
}

// 参考链接 http://www.cnblogs.com/lihanying/p/6227192.html
const setDocumentTitle = (title) => {
  document.title = title
  const iframe = document.createElement('iframe')
  const loadHandler = () => {
    window.setTimeout(() => {
      iframe.removeEventListener('load', loadHandler)
      document.body.removeChild(iframe)
    }, 0)
  }
  iframe.style.display = 'none'
  iframe.setAttribute('src', '/favicon.png')
  iframe.addEventListener('load', loadHandler)
  document.body.appendChild(iframe)
}
宜信网交流论坛 - 版权声明 1、在发表言论时,请遵守当地法律法规。主题所有言论纯属个人意见,与本站立场无关。
2、本站所有主题由作者发表,作者享有帖子相关版权,其他单位或个人使用、转载或引用本文时必须征得作者同意并注明来源于宜信网
3、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意。
4、帖子不遵守当地法律法规、广告、人身攻击等情况时,宜信网管理人员有权不事先通知发贴者而删除本文。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|宜信同城网交流论坛 ( 闽ICP备19021048号 )|站点地图

GMT+8, 2026-3-27 10:01 , Processed in 0.140938 second(s), 12 queries , Redis On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表