










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}}} |
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。