Web Share API可以启用本机共享功能(如果该您所有使用的平台支持该功能)。
if (navigator.share) {
const article = document.querySelector("article");
article.insertAdjacentHTML(
"beforeend",
`
<button onclick="window.doSharing(this)">Share</button>
`
);
}
window.doSharing = (el) => {
navigator.share({
title: el.parentNode.querySelector("h1").innerText,
text: el.parentNode.querySelector("p").innerText,
url: "https://codepen.io/chriscoyier/pen/KKzjQWO"
});
};
或者更直接点:
if (navigator.share) {
navigator.share(
{
title: document.querySelector('title').textContent,
text: document.querySelector('meta[name="description"]').getAttribute('content'),
url: document.querySelector('link[rel="canonical"]').getAttribute('href')
}
);
}