prime8dev

prime8dev
git clone https://git.prime8.dev/prime8dev.git
Log | Files | Refs | README | LICENSE

blogDetail.js (1658B)


      1 import { setAppTitleDetail, updateAppTitle } from "./header.js";
      2 
      3 export function initBlogDetail() {
      4   const heading = document.querySelector(".blog-article-title")?.textContent?.trim() || "";
      5   if (heading) {
      6     setAppTitleDetail("blog", heading);
      7     updateAppTitle("blog");
      8   }
      9 
     10   const shareButtons = document.querySelectorAll(".blog-share-btn");
     11   if (shareButtons.length === 0) return;
     12 
     13   shareButtons.forEach((btn) => {
     14     if (btn.dataset.boundClick === "1") return;
     15     btn.dataset.boundClick = "1";
     16 
     17     btn.addEventListener("click", async (e) => {
     18       e.stopPropagation();
     19       const button = e.currentTarget;
     20       const blogId = button.dataset.blogId;
     21       const title = button.dataset.blogTitle;
     22       const url = `${window.location.origin}/blog/${blogId}`;
     23 
     24       if (navigator.share) {
     25         try {
     26           await navigator.share({ title, url });
     27         } catch {
     28           const copied = await copyToClipboard(url);
     29           if (copied) showCopiedState(button);
     30         }
     31       } else {
     32         const copied = await copyToClipboard(url);
     33         if (copied) showCopiedState(button);
     34       }
     35     });
     36   });
     37 }
     38 
     39 function showCopiedState(button) {
     40   const originalText = button.textContent;
     41   button.textContent = "[Copied!]";
     42   button.classList.add("copied");
     43   window.setTimeout(() => {
     44     button.textContent = originalText;
     45     button.classList.remove("copied");
     46   }, 2000);
     47 }
     48 
     49 async function copyToClipboard(text) {
     50   if (!navigator.clipboard || typeof navigator.clipboard.writeText !== "function") {
     51     return false;
     52   }
     53 
     54   try {
     55     await navigator.clipboard.writeText(text);
     56     return true;
     57   } catch {
     58     return false;
     59   }
     60 }