1. 程式人生 > >UC瀏覽器 分享到朋友圈和微信好友

UC瀏覽器 分享到朋友圈和微信好友

用手機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; width: 100%;height:50px;' value='分享給微信好友'> </input>
    <br/> 
    <input type="button" class="btnWeixinShare" style='margin: 20px auto; width: 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>