惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
G
Google Developers Blog
M
MIT News - Artificial intelligence
Vercel News
Vercel News
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
GbyAI
GbyAI
B
Blog RSS Feed
Blog — PlanetScale
Blog — PlanetScale
Microsoft Security Blog
Microsoft Security Blog
V
Visual Studio Blog
Stack Overflow Blog
Stack Overflow Blog
雷峰网
雷峰网
The Cloudflare Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
F
Fortinet All Blogs
L
LangChain Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
A
About on SuperTechFans
T
The Blog of Author Tim Ferriss
B
Blog
J
Java Code Geeks
Hugging Face - Blog
Hugging Face - Blog
I
InfoQ
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com

叶泯希·杂七杂八的🐕窝

每天都是绝版呢———记博客二周年 | 叶泯希·杂七杂八的🐕窝 海的那边,真的是自由吗? | 叶泯希·杂七杂八的🐕窝 放学打球 | 叶泯希·杂七杂八的🐕窝 2025:得与失 | 叶泯希·杂七杂八的🐕窝 世事无常,珍惜眼前 | 叶泯希·杂七杂八的🐕窝 “花怎么会落呢” | 叶泯希·杂七杂八的🐕窝 “世界充满分歧,所以要学会尊重别人” | 叶泯希·杂七杂八的🐕窝 “死亡不是永别,遗忘才是” | 叶泯希·杂七杂八的🐕窝 2024-07-28 “爱就像蓝天白云晴空万里突然暴风雨” | 叶泯希·杂七杂八的🐕窝 “小雨天气” | 叶泯希·杂七杂八的🐕窝 快下雨,别打雷了,赶紧的,热! | 叶泯希·杂七杂八的🐕窝 寄博客一周年 | 叶泯希·杂七杂八的🐕窝 出门不要摔跤容易烫伤 | 叶泯希·杂七杂八的🐕窝 Windows 安装 Docker 部署 Immich 主题魔改——Solitude主题 | 叶泯希·杂七杂八的🐕窝 自定义表盘导航软件——对小米手环9的折腾 | 叶泯希·杂七杂八的🐕窝 武汉之旅 | 叶泯希·杂七杂八的🐕窝 关于我在2024年做了这件事... | 叶泯希·杂七杂八的🐕窝 2024.11.24 | 叶泯希·杂七杂八的🐕窝 他乡纵有当头月,不及家乡一盏灯 | 叶泯希·杂七杂八的🐕窝
因 LeanCloud 停止对外服务,对数据迁移至 Vercel 。
作者: 叶泯希 · 2026-03-25 · via 叶泯希·杂七杂八的🐕窝
1
const likeMe=(()=>{let instance;let initialCallOptions=null;let retryCount=0;const MAX_RETRIES=20;const RETRY_INTERVAL=50;function tryInitializeInstance(options){const targetElement=document.querySelector(options.el);if(targetElement){instance=new LikeMeSingleton(options);instance.reinitialize(options.el);initialCallOptions=null;retryCount=0}else if(retryCount<MAX_RETRIES){console.warn(`LikeMe: Target element ${options.el} not found yet. Retrying in ${RETRY_INTERVAL}ms (attempt ${retryCount+1}/${MAX_RETRIES}).`);retryCount++;setTimeout(()=>tryInitializeInstance(options),RETRY_INTERVAL)}else{console.error(`LikeMe: Target element ${options.el} not found after ${MAX_RETRIES} retries. Initialization failed.`);initialCallOptions=null;retryCount=0}}return options=>{if(!instance){if(!initialCallOptions){initialCallOptions=options};tryInitializeInstance(initialCallOptions)}else{instance.url=options.serverURL||instance.url;instance.color=options.color||instance.color;instance.reinitialize(options.el)}return instance}})();class LikeMeSingleton{constructor({el,serverURL,color='#ff9797'}){Object.assign(this,{el,url:serverURL,color});this.isLiking=false}async reinitialize(newEl=this.el){this.el=newEl;const currentElement=document.querySelector(this.el);if(currentElement){currentElement.innerHTML=''}else{console.warn(`LikeMe target element ${this.el} not found during reinitialization. Skipping render.`);return}try{await this.renderUI();this.attachEvents();this.updateCount()}catch(error){console.error('LikeMe reinitialize error:',error)}}async init(){return this.reinitialize(this.el)}async renderUI(){try{const response=await fetch(this.url);const html=await response.text();const element=document.querySelector(this.el);if(element){const parser=new DOMParser();const doc=parser.parseFromString(html,'text/html');element.innerHTML=doc.body.innerHTML;const card=element.querySelector('.likeCard');if(card){card.style.setProperty('background-color',this.color)}}}catch(error){console.error('Render UI error:',error)}}attachEvents(){const card=document.querySelector(`${this.el} .likeCard`);if(card){card.addEventListener('click',()=>this.handleLike())}}async updateCount(){try{const response=await fetch(`${this.url}/info`);const data=await response.json().catch(()=>null);if(response.ok&&data){const count=data.data?.count||0;const textElement=document.querySelector(`${this.el} .likeCard-text`);if(textElement){textElement.textContent=`❤ ${count}`}}}catch(error){console.error('Update count error:',error)}}setUIState(isLoading,text,isSuccess=false){const card=document.querySelector(`${this.el} .likeCard`);const textElement=document.querySelector(`${this.el} .likeCard-text`);if(card&&textElement){card.classList.remove('loading','success');if(isLoading){card.classList.add('loading')}else if(isSuccess){card.classList.add('success')}textElement.textContent=text}}async handleLike(){if(this.isLiking)return;this.isLiking=true;this.setUIState(true,'❤ 爱意传递中...');try{const response=await fetch(`${this.url}/like`);const payload=await response.json().catch(()=>null);if(!payload){this.setUIState(false,'❤ 网络错误');setTimeout(()=>{this.updateCount()},1500);return}const{code,data={},msg}=payload;const count=data.count||0;if(response.ok&&code==='200'){this.setUIState(false,'❤ 传递成功~',true);setTimeout(()=>{this.setUIState(false,`❤ ${count}`)},500);return}this.setUIState(false,msg||'❤ 传递失败~');setTimeout(()=>{this.updateCount()},1500)}catch(error){console.error('Handle like error:',error);this.setUIState(false,'❤ 网络错误');setTimeout(()=>{this.updateCount()},1500)}finally{this.isLiking=false}}}