Skip to content

Commit 24f9a63

Browse files
committed
Update clog.js
1 parent 9542ccd commit 24f9a63

1 file changed

Lines changed: 39 additions & 21 deletions

File tree

js/clog.js

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -562,33 +562,51 @@ function handleScrollProgress() {
562562
progress.style.width = scrolled + '%';
563563
}
564564

565-
function shareCurrentPost() {
565+
async function shareCurrentPost() {
566566
if (!currentArticleId) return;
567567

568568
const dbEntry = indexData.find(item => item.Article === currentArticleId);
569-
const shareTitle = dbEntry ? dbEntry.Name : "MainRoute Core Clog";
570-
const shareUrl = window.location.href;
571569

572-
const shareText = `Read ${shareTitle}\n==>\n${shareUrl}`;
570+
const title = dbEntry ? dbEntry.Name : "MainRoute Core Clog";
571+
const author = dbEntry?.Author ? `@${dbEntry.Author}` : "@Unknown";
572+
const url = window.location.href;
573+
const imageUrl = dbEntry?.Img || null;
573574

574-
if (navigator.share) {
575-
navigator.share({
576-
title: shareTitle,
577-
text: shareText,
578-
url: shareUrl
579-
}).catch(() => { });
580-
} else {
581-
navigator.clipboard.writeText(shareText).then(() => {
582-
const btn = document.querySelector('#full-meta-block button[onclick="shareCurrentPost()"]');
583-
if (btn) {
584-
const originalText = btn.innerHTML;
585-
btn.innerHTML = `<svg class="icon" style="width: 14px; height: 14px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polyline points="20 6 9 17 4 12"></polyline></svg> <span>Copied!</span>`;
586-
setTimeout(() => {
587-
btn.innerHTML = originalText;
588-
}, 2000);
575+
const shareText =`Read ${title}\nBy: ${author}\n==>\n${url}${imageUrl ? `\nImage: ${imageUrl}` : ""}`;
576+
try {
577+
if (navigator.share) {
578+
if (imageUrl && navigator.canShare) {
579+
try {
580+
const res = await fetch(imageUrl);
581+
const blob = await res.blob();
582+
const file = new File([blob], "image.jpg", { type: blob.type || "image/jpeg" });
583+
if (navigator.canShare({ files: [file] })) {
584+
await navigator.share({
585+
title, text: `${title} by ${author}`, url, files: [file]
586+
});
587+
return;
588+
}
589+
} catch (e) {
590+
}
589591
}
590-
});
591-
}
592+
await navigator.share({ title, text: `${shareText}`, url });
593+
return;
594+
}
595+
} catch (e) {}
596+
const finalText = shareText;
597+
navigator.clipboard.writeText(finalText).then(() => {
598+
const btn = document.querySelector('#full-meta-block button[onclick="shareCurrentPost()"]');
599+
if (btn) {
600+
const originalText = btn.innerHTML;
601+
btn.innerHTML = `
602+
<svg class="icon" style="width: 14px; height: 14px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
603+
<polyline points="20 6 9 17 4 12"></polyline>
604+
</svg>
605+
<span>Copied!</span>
606+
`;
607+
setTimeout(() => { btn.innerHTML = originalText; }, 2000);
608+
}
609+
});
592610
}
593611

594612
async function loadAndRenderFullPost(logId) {

0 commit comments

Comments
 (0)