bep40 commited on
Commit
b68704a
·
verified ·
1 Parent(s): 101d073

Fix: doShare uses navigator.clipboard.writeText as primary (works on Chrome mobile); overflow:clip for inline comments

Browse files
Files changed (1) hide show
  1. static/app_v2.js +29 -21
static/app_v2.js CHANGED
@@ -395,7 +395,7 @@ function showView(id){document.querySelectorAll('.view').forEach(v=>v.classList.
395
  function switchCat(id){document.querySelectorAll('.cat').forEach(c=>c.classList.remove('active'));document.querySelector(`[data-cat="${id}"]`)?.classList.add('active');document.querySelectorAll('.view').forEach(v=>v.classList.remove('active'));document.querySelectorAll('video').forEach(v=>{v.pause();if(v._hls){v._hls.destroy();v._hls=null}});document.querySelectorAll('iframe[data-yt-src]').forEach(f=>{f.src=''});if(id==='home')document.getElementById('view-home').classList.add('active');else if(id==='news-all'){document.getElementById('view-cat').classList.add('active');loadNewsTab()}else{document.getElementById('view-cat').classList.add('active');loadCat(id)}}
396
  function toast(msg){let t=document.getElementById('progress-toast');if(t){t.textContent=msg;t.style.display='block';setTimeout(()=>{t.style.display='none'},3500)}}
397
 
398
- // ===== FIXED doShare: copy link SYNCHRONOUSLY first (preserves user gesture), then try navigator.share as bonus =====
399
  function doShare(title,url,img,postId){
400
  var shareUrl;
401
  if(postId){
@@ -403,26 +403,34 @@ function doShare(title,url,img,postId){
403
  } else {
404
  shareUrl = SPACE+'/s?url='+encodeURIComponent(url)+'&title='+encodeURIComponent(title)+'&img='+encodeURIComponent(img||'');
405
  }
406
- // Step 1: Copy to clipboard SYNCHRONOUSLY during user gesture (MUST work)
407
- var copied = false;
408
- try{
409
- var ta=document.createElement('textarea');
410
- ta.value=shareUrl;ta.style.position='fixed';ta.style.left='-9999px';ta.style.top='-9999px';ta.style.opacity='0';
411
- document.body.appendChild(ta);ta.select();ta.setSelectionRange(0,99999);
412
- copied = document.execCommand('copy');
413
- document.body.removeChild(ta);
414
- }catch(e){}
415
- // Step 2: Try navigator.share for native share UI (async, bonus)
416
- try{
417
- if(navigator.share){
418
- navigator.share({title:title||'',url:shareUrl}).catch(function(){});
419
- }
420
- }catch(e){}
421
- // Step 3: Show result
422
- if(copied){toast('📋 Đã sao chép link!');}
423
- else{
424
- // Last resort: prompt
425
- try{prompt('Sao chép link:', shareUrl);}catch(e){toast('❌ Không thể chia sẻ');}
 
 
 
 
 
 
 
 
426
  }
427
  }
428
 
 
395
  function switchCat(id){document.querySelectorAll('.cat').forEach(c=>c.classList.remove('active'));document.querySelector(`[data-cat="${id}"]`)?.classList.add('active');document.querySelectorAll('.view').forEach(v=>v.classList.remove('active'));document.querySelectorAll('video').forEach(v=>{v.pause();if(v._hls){v._hls.destroy();v._hls=null}});document.querySelectorAll('iframe[data-yt-src]').forEach(f=>{f.src=''});if(id==='home')document.getElementById('view-home').classList.add('active');else if(id==='news-all'){document.getElementById('view-cat').classList.add('active');loadNewsTab()}else{document.getElementById('view-cat').classList.add('active');loadCat(id)}}
396
  function toast(msg){let t=document.getElementById('progress-toast');if(t){t.textContent=msg;t.style.display='block';setTimeout(()=>{t.style.display='none'},3500)}}
397
 
398
+ // ===== doShare: COPY link to clipboard, then try native share as bonus =====
399
  function doShare(title,url,img,postId){
400
  var shareUrl;
401
  if(postId){
 
403
  } else {
404
  shareUrl = SPACE+'/s?url='+encodeURIComponent(url)+'&title='+encodeURIComponent(title)+'&img='+encodeURIComponent(img||'');
405
  }
406
+ // Try clipboard API first (modern, works on HTTPS)
407
+ if(navigator.clipboard && navigator.clipboard.writeText){
408
+ navigator.clipboard.writeText(shareUrl).then(function(){
409
+ toast('📋 Đã sao chép link!');
410
+ try{if(navigator.share)navigator.share({title:title||'',url:shareUrl}).catch(function(){});}catch(e){}
411
+ }).catch(function(){
412
+ // Fallback: execCommand (deprecated but works on some browsers)
413
+ try{
414
+ var ta=document.createElement('textarea');
415
+ ta.value=shareUrl;ta.style.position='fixed';ta.style.left='-9999px';ta.style.top='-9999px';ta.style.opacity='0';
416
+ document.body.appendChild(ta);ta.select();ta.setSelectionRange(0,99999);
417
+ if(document.execCommand('copy')){toast('📋 Đã sao chép link!');}
418
+ else{prompt('📋 Sao chép link:', shareUrl);}
419
+ document.body.removeChild(ta);
420
+ }catch(e){prompt('📋 Sao chép link:', shareUrl);}
421
+ try{if(navigator.share)navigator.share({title:title||'',url:shareUrl}).catch(function(){});}catch(e){}
422
+ });
423
+ } else {
424
+ // execCommand approach
425
+ try{
426
+ var ta=document.createElement('textarea');
427
+ ta.value=shareUrl;ta.style.position='fixed';ta.style.left='-9999px';ta.style.top='-9999px';ta.style.opacity='0';
428
+ document.body.appendChild(ta);ta.select();ta.setSelectionRange(0,99999);
429
+ if(document.execCommand('copy')){toast('📋 Đã sao chép link!');}
430
+ else{prompt('📋 Sao chép link:', shareUrl);}
431
+ document.body.removeChild(ta);
432
+ }catch(e){prompt('📋 Sao chép link:', shareUrl);}
433
+ try{if(navigator.share)navigator.share({title:title||'',url:shareUrl}).catch(function(){});}catch(e){}
434
  }
435
  }
436