-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathnativeShare.js
209 lines (197 loc) · 8.63 KB
/
nativeShare.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
var nativeShare = function (elementNode, config) {
if (!document.getElementById(elementNode)) {
return false;
}
var qApiSrc = {
lower: "//3gimg.qq.com/html5/js/qb.js",
higher: "//jsapi.qq.com/get?api=app.share"
};
var bLevel = {
qq: {forbid: 0, lower: 1, higher: 2},
uc: {forbid: 0, allow: 1}
};
var UA = navigator.appVersion;
var isqqBrowser = (UA.split("MQQBrowser/").length > 1) ? bLevel.qq.higher : bLevel.qq.forbid;
var isucBrowser = (UA.split("UCBrowser/").length > 1) ? bLevel.uc.allow : bLevel.uc.forbid;
var version = {
uc: "",
qq: ""
};
var isWeixin = false;
var isInnerQQ = false;
config = config || {};
this.elementNode = elementNode;
this.url = config.url || document.location.href || '';
this.title = config.title || document.title || '';
this.desc = config.desc || document.title || '';
this.img = config.img || document.getElementsByTagName('img').length > 0 && document.getElementsByTagName('img')[0].src || '';
this.img_title = config.img_title || document.title || '';
this.from = config.from || window.location.host || '';
this.ucAppList = {
sinaWeibo: ['kSinaWeibo', 'SinaWeibo', 11, '新浪微博'],
weixin: ['kWeixin', 'WechatFriends', 1, '微信好友'],
weixinFriend: ['kWeixinFriend', 'WechatTimeline', '8', '微信朋友圈'],
QQ: ['kQQ', 'QQ', '4', 'QQ好友'],
QZone: ['kQZone', 'QZone', '3', 'QQ空间']
};
this.share = function (to_app) {
var title = this.title, url = this.url, desc = this.desc, img = this.img, img_title = this.img_title, from = this.from;
if (isucBrowser) {
to_app = to_app == '' ? '' : (platform_os == 'iPhone' ? this.ucAppList[to_app][0] : this.ucAppList[to_app][1]);
if (to_app == 'QZone') {
B = "mqqapi://share/to_qzone?src_type=web&version=1&file_type=news&req_type=1&image_url="+img+"&title="+title+"&description="+desc+"&url="+url+"&app_name="+from;
k = document.createElement("div"), k.style.visibility = "hidden", k.innerHTML = '<iframe src="' + B + '" scrolling="no" width="1" height="1"></iframe>', document.body.appendChild(k), setTimeout(function () {
k && k.parentNode && k.parentNode.removeChild(k)
}, 5E3);
}
if (typeof(ucweb) != "undefined") {
ucweb.startRequest("shell.page_share", [title, title, url, to_app, "", "@" + from, ""])
} else {
if (typeof(ucbrowser) != "undefined") {
ucbrowser.web_share(title, title, url, to_app, "", "@" + from, '')
} else {
}
}
} else {
if (isqqBrowser && !isWeixin) {
to_app = to_app == '' ? '' : this.ucAppList[to_app][2];
var ah = {
url: url,
title: title,
description: desc,
img_url: img,
img_title: img_title,
to_app: to_app,//微信好友1,腾讯微博2,QQ空间3,QQ好友4,生成二维码7,微信朋友圈8,啾啾分享9,复制网址10,分享到微博11,创意分享13
cus_txt: "请输入此时此刻想要分享的内容"
};
ah = to_app == '' ? '' : ah;
if (typeof(browser) != "undefined") {
if (typeof(browser.app) != "undefined" && isqqBrowser == bLevel.qq.higher) {
browser.app.share(ah)
}
} else {
if (typeof(window.qb) != "undefined" && isqqBrowser == bLevel.qq.lower) {
window.qb.share(ah)
} else {
}
}
} else {
}
}
};
this.html = function(type) {
/* type: 1:调用原生分享功能, type: 2:调用qrcode填充, type: 3:对微信浏览器填充背景*/
var position = document.getElementsByTagName('body')[0];
if (type === 1) {
var html = '<div id="nativeShare"><div class="label"><span class="title">分享至:</span><span class="close"></span></div>'+
'<div class="list clearfix">'+
'<span data-app="sinaWeibo" class="nativeShare weibo"><i></i>新浪微博</span>'+
'<span data-app="weixin" class="nativeShare weixin"><i></i>微信好友</span>'+
'<span data-app="weixinFriend" class="nativeShare weixin_timeline"><i></i>微信朋友圈</span>'+
'<span data-app="QQ" class="nativeShare qq"><i></i>QQ好友</span>'+
'<span data-app="QZone" class="nativeShare qzone"><i></i>QQ空间</span>'+
'<span data-app="" class="nativeShare more"><i></i>更多</span>'+
'</div></div>';
} else if (type === 2) {
var html = '<div id="nativeShare"><div class="label"><span class="title">分享至:</span><span class="close"></span></div>'+
'<div class="list clearfix">'+
'<div class="intro">长按复制下方链接,去粘贴给好友吧:</div>' +
'<a href="'+ document.location.href +'" target="_blank">' +
'<div class="url"><span>' +
document.location.href +
'</span></div>'+
'</a>' +
'</div></div>';
} else {
var html = '<div class="closewx">'+
'<div class="wxshare"></div>'+
'</div>';
}
var wrapper = document.createElement('div');
wrapper.id = 'nativeShareBox';
wrapper.innerHTML = html;
position.appendChild(wrapper);
this.bindEvents();
};
this.bindEvents = function() {
var close = document.querySelector('#nativeShare .close, #nativeShareBox .closewx');
close.addEventListener('click', function () {
document.getElementById('nativeShareBox').style.display = 'none';
});
document.getElementById(elementNode).addEventListener('click', function () {
document.getElementById('nativeShareBox').style.display = 'block';
});
}
this.isloadqqApi = function () {
if (isqqBrowser) {
var b = (version.qq < 5.4) ? qApiSrc.lower : qApiSrc.higher;
var d = document.createElement("script");
var a = document.getElementsByTagName("body")[0];
d.setAttribute("src", b);
a.appendChild(d)
}
};
this.getPlantform = function () {
ua = navigator.userAgent;
if ((ua.indexOf("iPhone") > -1 || ua.indexOf("iPod") > -1)) {
return "iPhone"
}
return "Android"
};
this.is_weixin = function () {
var a = UA.toLowerCase();
if (a.match(/MicroMessenger/i) == "micromessenger") {
return true
} else {
return false
}
};
this.getVersion = function (c) {
var a = c.split("."), b = parseFloat(a[0] + "." + a[1]);
return b
};
this.is_innerQQ = function () {
if (UA.match(/QQ\//i) == "QQ/") {
return true;
} else {
return false;
}
}
this.init = function () {
platform_os = this.getPlantform();
version.qq = isqqBrowser ? this.getVersion(UA.split("MQQBrowser/")[1]) : 0;
version.uc = isucBrowser ? this.getVersion(UA.split("UCBrowser/")[1]) : 0;
isWeixin = this.is_weixin();
isInnerQQ = this.is_innerQQ();
if ((isqqBrowser && version.qq < 5.4 && platform_os == "iPhone") || (isqqBrowser && version.qq < 5.3 && platform_os == "Android")) {
isqqBrowser = bLevel.qq.forbid
} else {
if (isqqBrowser && version.qq < 5.4 && platform_os == "Android") {
isqqBrowser = bLevel.qq.lower
} else {
if (isucBrowser && ((version.uc < 10.2 && platform_os == "iPhone") || (version.uc < 9.7 && platform_os == "Android"))) {
isucBrowser = bLevel.uc.forbid
}
}
}
this.isloadqqApi();
if ((isqqBrowser || isucBrowser) && !isWeixin) {
this.html(1);
} else if (isWeixin) {
this.html(3);
} else if (isInnerQQ){
this.html(2);
} else {
this.html(2);
}
};
this.init();
var share = this;
var items = document.getElementsByClassName('nativeShare');
for (var i=0;i<items.length;i++) {
items[i].onclick = function(){
share.share(this.getAttribute('data-app'));
}
}
return this;
};