zoukankan      html  css  js  c++  java
  • UC浏览器 分享到朋友圈和微信好友 分类: JavaScript 2015-04-28 14:45 615人阅读 评论(1) 收藏

    用手机UC浏览器访问新浪微博,会注意到有这样的两个分享按钮:


    在手机端浏览器里,点击分享按钮,就可以启动微信客户端并分享到微信。研究了下其源代码,存在这样的一个js:http://mjs.sinaimg.cn/wap/module/share/201504071745/js/addShare.min.js

    从里面抽离出了分享调用的方法,方便调用。(注意:这个分享功能只在UC手机浏览器有效)

    if (/AppleWebKit.*Mobile/i.test(navigator.userAgent) || (/MIDP|SymbianOS|NOKIA|SAMSUNG|LG|NEC|TCL|Alcatel|BIRD|DBTEL|Dopod|PHILIPS|HAIER|LENOVO|MOT-|Nokia|SonyEricsson|SIE-|Amoi|ZTE/.test(navigator.userAgent))) {
            if (window.location.href.indexOf("?mobile") < 0) {
                try {
                    //判断是手机端访问
                    if (/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) {
    
    	            //判断是UC浏览器
                        if (typeof (ucweb) != "undefined") {
                            
                            //微信好友
                            $("#btnShareFirend").unbind();
                            $("#btnShareFirend").bind("click", function () {
                                var Browser = new Object();
                                Browser.ios = /iphone/.test(Browser.userAgent); //判断ios系统 
                                var title = document.title;
                                var img = "";
                                var url = location.href;
                                if (Browser.ios) {
                                    ucbrowser.web_share(title, img, url, 'kWeixin', '', '', '');
                                } else {
                                    ucweb.startRequest("shell.page_share", [title, img, url, 'WechatFriends', '', '', '']);
                                };
                            });
    
                            //微信朋友圈
                            $("#btnWeixinShare").unbind();
                            $("#btnWeixinShare").bind("click", function () {
                                var Browser = new Object();
                                Browser.ios = /iphone/.test(Browser.userAgent); //判断ios系统 
                                var title = document.title;
                                var img = "";
                                var url = location.href;
                                if (Browser.ios) {
                                    ucbrowser.web_share(title, img, url, 'kWeixinFriend', '', '', '');
                                } else {
                                    ucweb.startRequest("shell.page_share", [title, img, url, 'WechatTimeline', '', '', '']);
                                };
                            });
                             
                        }
                    }
                } catch (e) { }
            }
        }
    

    完整代码:

    <html>
    <body>
        <script src="jquery.min.js"></script>  
        <h4>测试微信分享功能(仅在手机版UC浏览器下有效)</h4> 
        <input type="button" class="btnShareFriends" style='margin: 20px auto;  100%;height:50px;' value='分享给微信好友'> </input>
        <br/> 
        <input type="button" class="btnWeixinShare" style='margin: 20px auto;  100%;height:50px;' value='分享到微信朋友圈'> </input>
        <script type="TEXT/javascript">
          $(function () {
        if (/AppleWebKit.*Mobile/i.test(navigator.userAgent) || (/MIDP|SymbianOS|NOKIA|SAMSUNG|LG|NEC|TCL|Alcatel|BIRD|DBTEL|Dopod|PHILIPS|HAIER|LENOVO|MOT-|Nokia|SonyEricsson|SIE-|Amoi|ZTE/.test(navigator.userAgent))) {
            if (window.location.href.indexOf("?mobile") < 0) {
    
                //判断是手机端访问
                if (/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) {
    
                    //判断是UC浏览器
                    if (typeof (ucweb) != "undefined") {
    
                        $(".btnShareFriends").click(function () {
                            var Browser = new Object();
                            Browser.ios = /iphone/.test(Browser.userAgent); //判断ios系统 
                            var title = "测试分享到朋友圈";
                            var img = "";
                            var url = location.href;
                            if (Browser.ios) {
                                ucbrowser.web_share(title, img, url, 'kWeixin', '', '@39yst', '');
                            } else {
                                ucweb.startRequest("shell.page_share", [title, img, url, 'WechatFriends', '', '', '']);
                            } 
                        });
    
                        $(".btnWeixinShare").click(function () {
                            var Browser = new Object();
                            Browser.ios = /iphone/.test(Browser.userAgent); //判断ios系统 
                            var title = "测试分享到朋友圈";
                            var img = "";
                            var url = location.href;
                            if (Browser.ios) {
                                ucbrowser.web_share(title, img, url, 'kWeixinFriend', '', '@39yst', '');
                            } else {
                                ucweb.startRequest("shell.page_share", [title, img, url, 'WechatTimeline', '', '', '']);
                            }
                        });
                    }
                } else {
                    alert("请使用手机UC浏览器测试");
                }
            } else {
                alert("请使用手机访问测试");
            }
        }
    });
        </script>
    </body>
    </html>
    


    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    逆变和协变
    委托的泛型版本
    委托的协变和逆变
    IIS8应用池重启脚本
    JS获取url参数及url编码、解码
    Jmeter压测Thrift服务接口
    浏览器插件及好用的小工具
    Jmeter入门实例
    BugBash活动分享
    如何作缺陷分析
  • 原文地址:https://www.cnblogs.com/zhangqs008/p/4687611.html
Copyright © 2011-2022 走看看