Skip to content

Instantly share code, notes, and snippets.

@zew13
Created December 21, 2021 10:04
Show Gist options
  • Select an option

  • Save zew13/58b43871db61f0baa2c331dabfbe48f1 to your computer and use it in GitHub Desktop.

Select an option

Save zew13/58b43871db61f0baa2c331dabfbe48f1 to your computer and use it in GitHub Desktop.
// Generated by CoffeeScript 2.6.1
(function() {
var hex, sha1, sign;
sha1 = function(str) {
var buffer;
// We transform the string into an arraybuffer.
buffer = new TextEncoder('utf-8').encode(str);
return crypto.subtle.digest('SHA-1', buffer).then(function(hash) {
return hex(hash);
});
};
hex = function(buffer) {
var hexCodes, i, paddedValue, padding, stringValue, value, view;
hexCodes = [];
view = new DataView(buffer);
i = 0;
while (i < view.byteLength) {
// Using getUint32 reduces the number of iterations needed (we process 4 bytes each time)
value = view.getUint32(i);
// toString(16) will give the hex representation of the number without padding
stringValue = value.toString(16);
// We use concatenation and slice for padding
padding = '00000000';
paddedValue = (padding + stringValue).slice(-padding.length);
hexCodes.push(paddedValue);
i += 4;
}
// Join all the hex strings into one
return hexCodes.join('');
};
sign = async(ticket, url) => {
var nonce, signature, str, timestamp;
nonce = Math.random().toString(36).substr(2, 15);
timestamp = parseInt(new Date().getTime());
str = "jsapi_ticket=" + ticket + "&noncestr=" + nonce + "&timestamp=" + timestamp + "&url=" + location.href.split("#", 1)[0];
signature = (await sha1(str));
return {
signature,
timestamp,
nonceStr: nonce
};
};
if (navigator.userAgent.indexOf('MicroMessenger') + 1) {
$.when($.get("//wxticket.xvc.com/ticket.txt"), $.getScript("//res.wx.qq.com/open/js/jweixin-1.6.0.js")).done(function([ticket]) {
return sign(ticket).then(function(config) {
var jsApiList;
jsApiList = "updateAppMessageShareData updateTimelineShareData onMenuShareTimeline onMenuShareAppMessage onMenuShareQQ onMenuShareWeibo onMenuShareQZone".split(' ');
//"updateAppMessageShareData"
//"updateTimelineShareData"
wx.config(Object.assign(config, {
appId: 'wxab95844927471714',
jsApiList: jsApiList.slice()
}));
return wx.ready(function() {
var i, img_url, j, len, o, page, ref, results;
page = JSON.parse($("script[type='application/ld+json']").html());
o = {
title: page.headline,
link: location.href,
desc: page.description,
imgUrl: "https://font.xvc.com/xvc.webp"
};
img_url = (ref = page.image) != null ? ref.url : void 0;
if (img_url) {
o.imgUrl = "https:" + img_url + "?imageView2/1/w/300/h/300";
}
results = [];
for (j = 0, len = jsApiList.length; j < len; j++) {
i = jsApiList[j];
results.push(wx[i](o));
}
return results;
});
});
});
}
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment