前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >web网页动态分享facebook和twitter

web网页动态分享facebook和twitter

作者头像
陈灬大灬海
发布2020-07-09 15:54:10
5.7K1
发布2020-07-09 15:54:10
举报

介绍

facebook分享 http://www.facebook.com/sharer.php?t=${text}u=encodeURIComponent('静态html') twitter分享 https://twitter.com/share?text=${text}&url=静态html 原理,通过调用第三方的分享地址,第三方回调你传的url,解析里面的meta信息,来显示标题图片啥的 参数text可以忽略,所以就是要解决静态html的问题

示例静态html

主要的就是图片,标题,描述。 site,url啥的随缘填写。 card和type等都是固定的

代码语言:javascript
复制
<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  
  <!-- twitter分享 -->
  <meta property="twitter:url" content="http://gg.chendahai.cn/static/share/index.html"/>
  <meta name="twitter:title" content="This is title"/>
  <meta name="twitter:description" content="This is desc"/>
  <meta name="twitter:site" content="http://gg.chendahai.cn/static/share/index.html">
  <meta name="twitter:card" content="summary_large_image"/>
  <meta name="twitter:image" content="http://gg.chendahai.cn/static/image/apple.jpg"/>
  
  <!-- facebook分享 -->
  <meta property="og:url" content="http://gg.chendahai.cn/static/share/index.html"/>
  <meta property="og:title" content="This is my plan,let's play together"/>
  <meta property="og:description" content="This is my plan,let's play together"/>
  <meta property="og:image" content="http://gg.chendahai.cn/static/image/apple.jpg"/>
  <meta property="og:type" content="website"/>
  
  <title>share test</title>
</head>
<body>
</body>
</html>

前提

有域名,端口号须为80,整个二级域名,nginx转发即可 比如java.chendahai.cn(80端口转发到5005端口)

代码语言:javascript
复制
  1 server {
  2     listen       80;
  3     server_name  java.chendahai.cn;
  4 
  5     client_max_body_size 20m;
  6 
  7     location / {
  8         proxy_set_header  X-Real-IP $remote_addr;
  9         proxy_set_header  Host   $http_host;
 10         proxy_pass     http://0.0.0.0:5005;
 11     }
 12 
 13 }

调用后端接口,根据参数动态返回html页面

注意事项

  1. url编码与解码得梳理清楚
  2. twitter分享地址有内容限制,所以参数不能太长。所以直接传meta的标签过去是行不通的,当然也会生成xss漏洞
  3. 先通过静态的页面测试通过之后再一步步往下走

为了保证接口参数的长度问题,接收参数选择用逗号分隔的字符串。 后端代码示例基于SpringMVC

代码语言:javascript
复制
    /**
     * facebook和twitter通用的动态分享接口
     *
     * @param meta k,v,k,v 类型的字符串
     * @return html页面
     */
    @RequestMapping(value = "/share/new", produces = "text/html;charset=utf-8")
    public String shareWin(String meta) throws UnsupportedEncodingException {
        // twitter的url需要进行url解码处理
        meta = URLDecoder.decode(meta, "UTF-8");
        String[] split = meta.split(",");
        String metaHtml = "";
        for (int i = 0; i < split.length; i++) {
            metaHtml += "<meta property=\"" + split[i] + "\" name=\"" + split[i] + "\" content=\"" + split[i + 1] + "\"/>\n";
            i++;
        }
        String retHtml = "<!DOCTYPE html>\n"
            + "<html lang=\"en\">\n"
            + "<head>\n"
            + metaHtml
            + "</head>\n"
            + "<body>\n"
            + "<script type=\"text/javascript\">\n"
            + "\twindow.location.href=\"http://java.chendahai.cn/\";\n"
            + "</script>"
            + "</body>\n"
            + "</html>";
        System.out.println(retHtml);
        return retHtml;
    }

postman请求返回html例图

前端示例

facebook
代码语言:javascript
复制
let metaArr = [
    'og:url', 'http://java.chendahai.cn',
    'og:title', 'this is title',
    'og:description', 'this is desc',
    'og:image', 'http://gg.chendahai.cn/static/image/apple.jpg',
    'og:type', 'website'
]
let metaParams = metaArr.toString()
window.open('http://www.facebook.com/sharer.php?u=' + encodeURIComponent(`http://java.chendahai.cn/share/new?meta=${metaParams}`))
twitter
代码语言:javascript
复制
let metaArr = [
    'twitter:url', 'http://java.chendahai.cn',
    'twitter:site', 'http://java.chendahai.cn',
    'twitter:title', 'this is title',
    'twitter:description', 'this is desc',
    'twitter:card', 'summary_large_image',
    'twitter:image', 'http://gg.chendahai.cn/static/image/pkq.jpg'
]
let metaParams = metaArr.toString()
// 需要encode两次 因为浏览器会自动decode一次,另一次是服务端会decode
metaParams = encodeURIComponent(encodeURIComponent(metaParams))
window.open(`https://twitter.com/share?text=${title}&url=http://java.chendahai.cn/share/new?meta=${metaParams}`)
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-07-08 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 介绍
  • 示例静态html
  • 前提
  • 调用后端接口,根据参数动态返回html页面
  • postman请求返回html例图
  • 前端示例
    • facebook
      • twitter
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档