- vừa được xem lúc

Một số đoạn code hữu ích cho JS developer

0 0 15

Người đăng: Đặng Xuân Thắng

Theo Viblo Asia

1. Sao chép ảnh

const copyImageToClipboard = async (imageElement) => { let canvas = document.createElement("canvas"); canvas.width = imageElement.width; canvas.height = imageElement.height; let ctx = canvas.getContext("2d"); ctx.drawImage(imageElement, 0, 0); const dataURL = canvas.toDataURL(); const blob = await (await fetch(dataURL)).blob(); navigator.clipboard.write([ new ClipboardItem({ "image/png": blob, }), ]);
};
<img crossorigin="anonymous" src="https://picsum.photos/200" alt="">
<button>Copy</button>
document.querySelector("button").onclick = () => { copyImageToClipboard(document.querySelector("img"))
}

2. Sao chép văn bản

const copyToClipboard = (text) => { try { if (navigator.clipboard && window.isSecureContext) { return navigator.clipboard.writeText(text); } else { let textArea = document.createElement("textarea"); textArea.value = text; textArea.style.position = "fixed"; textArea.style.left = "-999999px"; textArea.style.top = "-999999px"; document.body.appendChild(textArea); textArea.focus(); textArea.select(); return new Promise((res, rej) => { document.execCommand("copy") ? res() : rej(); textArea.remove(); }); } } catch (error) { console.error(error); }
};

3. Force download file

Tải file với thẻ a. Không hoạt động nếu file được trả về không có header "Content-Disposition: attachment"

const forceDownloadFile = (url) => { const anchor = document.createElement("a"); anchor.href = url; anchor.download = url; anchor.style.display = "none"; document.body.appendChild(anchor); anchor.click(); document.body.removeChild(anchor);
};

4. Tính khoảng thời gian đã qua

const calculateElapsedTime = (timeCreated) => { const created = new Date(timeCreated).getTime(); let periods = { year: 365 * 30 * 24 * 60 * 60 * 1000, month: 30 * 24 * 60 * 60 * 1000, week: 7 * 24 * 60 * 60 * 1000, day: 24 * 60 * 60 * 1000, hour: 60 * 60 * 1000, minute: 60 * 1000, }; let diff = Date.now() - created; for (const key in periods) { if (diff >= periods[key]) { let result = Math.floor(diff / periods[key]); return `${result} ${(result === 1 ? key : key + "s")} ago`; } } return "Just now";
}; calculateElapsedTime("2022-05-20T09:03:20.229Z")
// output: 2 minutes ago

5. Định dạng số

const formatNumber = (num) => { return Intl.NumberFormat('en-US', { notation: "compact", maximumFractionDigits: 1 }).format(num);
} formatNumber(389210)
// output: '389.2K'

6. Thay thế tất cả các ký tự trong chuỗi

let str = "ababaa";
// Replace "b" with ""
// Output "aaaa" // Regex
str.replace(/b/g, ""); // Better regex
str.replace(new RegExp("b", "g"), ""); // Split and join
str.split("b").join(""); // Replace All (modern browser)
str.replaceAll("b", ""); // While loop
while (str.includes("b")) str = str.replace("b", "");

7. Format file size

const formatFileSize = (size) => { let i = Math.floor(Math.log(size) / Math.log(1024)); return `${(size / Math.pow(1024, i)).toFixed(1)} ${ ["B", "KB", "MB", "GB", "TB"][i] }`;
}; formatFileSize(32143332) // output: "30.7 MB"
formatFileSize(8904869085) // output: "8.3 GB"

8. Format video time

const formatVideoTime = (seconds) => { try { const date = new Date(0); date.setSeconds(seconds); const time = date.toISOString().slice(11, 19); const result = time.startsWith("00:0") ? time.slice(4) : time.startsWith("00") ? time.slice(3) : time.length === 8 && time.startsWith("0") ? time.slice(1) : time; return result; } catch (error) { return "0:00"; }
}; formatVideoTime(20) // output: "0:20"
formatVideoTime(135) // output: "2:15"
formatVideoTime(3214) // output: "53:34"
formatVideoTime(32143) // output: "8:55:43"

Bình luận

Bài viết tương tự

- vừa được xem lúc

Refresh token là gì? Cách hoạt động có khác gì so với token không?

Ở những bài trước chúng ta đã nói nhiều về JWT là gì? Vì sao phải sử dụng JWT, trong suốt quá trình sử dụng JWT chúng tôi có nhận được nhiều phản hồi về chủ đề JWT. Trong đó có một vấn đề mà có nhiều

0 0 89

- vừa được xem lúc

Design patterns là gì? Từ cuộc sống đến lập trình và cách học trong 1 tuần (mẹo) - Kèm tài liệu và source - Series Design patterns

Design patterns là gì? Design patterns chính là một gã khổng lồ, nếu bạn đứng được trên vai thì bạn sẽ nhìn xa hơn. Làm sao bạn đứng được trên vai hãy theo cách của bài viết này, từ CUỘC SỐNG đến PHẦN

0 0 26

- vừa được xem lúc

Detect When Users Switch Tabs using JavaScript | Tutorials Javascript | SHORT VIDEOS

Video được đăng tại channel Tips Javascript

0 0 16

- vừa được xem lúc

Push or Pull ngăn xếp công nghệ nào được FACEBOOK và INSTAGRAM lựa chọn phát triển NEWS FEED?

Video được đăng tại channel Tips Javascript

0 0 17

- vừa được xem lúc

Show doanh thu KHỦNG trên youtube khi đạt 10.000 Subscribers và LỜI CẢM ƠN ANH CHỊ DEVELOPERS.

Video được đăng tại channel Tips Javascript

0 0 14