hexo-theme-ayer/source-src/js/share.js
2023-01-20 19:07:42 +03:00

51 lines
1.4 KiB
JavaScript

function generate(url, opts) {
var url = url.replace(/<%-sUrl%>/g, encodeURIComponent(opts.sUrl))
.replace(/<%-sTitle%>/g, encodeURIComponent(opts.sTitle))
.replace(/<%-sDesc%>/g, encodeURIComponent(opts.sDesc))
.replace(/<%-sPic%>/g, encodeURIComponent(opts.sPic));
window.open(url);
}
function showWX() {
$('.wx-share-modal').addClass('in ready')
$('#share-mask').show()
}
function hideWX() {
$('.wx-share-modal').removeClass('in ready')
$('#share-mask').hide()
}
function handleClick(type, opts) {
} if (type === 'google') {
generate('https://plus.google.com/share?url=<%-sUrl%>', opts)
}
const share_init = () => {
let $sns = document.querySelectorAll('.share-sns');
if (!$sns || $sns.length === 0) return;
let sUrl = window.location.href;
let sTitle = document.querySelector('title').innerHTML;
let $img = document.querySelectorAll('.article-entry img');
let sPic = $img.length ? document.querySelector('.article-entry img').getAttribute('src') : '';
if ((sPic !== '') && !/^(http:|https:)?\/\//.test(sPic)) {
sPic = window.location.origin + sPic
}
$sns.forEach(($em) => {
$em.onclick = (e) => {
let type = $em.getAttribute('data-type')
handleClick(type, {
sUrl: sUrl,
sPic: sPic,
sTitle: sTitle,
sDesc: sTitle
})
}
})
document.querySelector('#mask').onclick = hideWX
document.querySelector('.modal-close').onclick = hideWX
}
share_init()