homoer enhancement

made by SC

  1. // ==UserScript==
  2. // @name homoer enhancement
  3. // @version 1.0
  4. // @match http*://*.homoer.com/*
  5. // @description made by SC
  6. // @license MIT
  7. // @namespace https://greasyfork.org/users/1010601
  8. // ==/UserScript==
  9. async function parse(url){
  10. const html=await (await fetch(url)).text(),key1='class="guest_box"';
  11. return html.substring(html.indexOf(key1)+key1.length,html.lastIndexOf('adsbygoogle.js'))
  12. .split(key1).filter(p=>!p.match(/這邊有一段限制閱讀文字|已經過期,無法閱讀/)).map((p,r)=>(
  13. r=[...p.matchAll(/(?:reply_by">|guest_time">|ip_flag' )(.+?)<\/span>/g)].map(m=>m[1]),
  14. r[3]=p.substring(...[...p.matchAll(/<div class="HTML_info"/g)].map(m=>m.index))
  15. .replaceAll(/<[^>]+>|[\t\r ]+/g,'').slice(1,-1).replaceAll(/\s*\n+\s*/g,'<br/>'),r));
  16. }
  17.  
  18. function addRows(ol,page,rows){
  19. if(rows.length)rows.forEach(([name,time,ip,msg],i)=>ol.append(i>0||1==page?ip?
  20. `<li>${msg}<a class="guest_option_top">${name} (<img class='ip_flag' ${ip} ${time}</a></li>`:
  21. `<li>${msg}<a class="guest_option_top">${name}${time}</a></li>`:`<hr/>page #${page}`));
  22. }
  23.  
  24. function createRoot(title){
  25. const ol=$(`<ol class="footer" style="padding:revert;background:black;color:white;font:12pt monospaced">${title}</ol>`);
  26. $('.footer').parent().empty().append(ol);
  27. return ol;
  28. }
  29.  
  30. async function viewAll(tr){
  31. const {href,textContent:title}=tr.querySelector('a:first-child'),{textContent:pages}=tr.querySelector('a:last-child');
  32. const ol=createRoot(title.trim()),url=href.substring(0,href.lastIndexOf('=')+1);
  33. for(let i=1,n=~~pages||1;i<=n;i++)addRows(ol,i,await parse(url+i));
  34. }
  35.  
  36. async function viewMore(href,param){
  37. const ol=createRoot(''),url=href.substring(0,href.lastIndexOf('=')+1),pages=~~param.get('p'),end=Math.max(pages-30,1);
  38. for(let page=pages;page>end;page--)addRows(ol,page,await parse(url+page));
  39. ol.append(`<hr/><a href="${url}${end}" style="color:pink">page #${end} &gt;</a>`);
  40. }
  41.  
  42. let dom;
  43. if((dom=document.querySelectorAll('#article_list tr:not(:first-child)')).length)
  44. for(const tr of dom)tr.addEventListener('click',e=>viewAll(tr));
  45. else if((dom=document.querySelectorAll('#page_block .page_nr')).length)
  46. viewMore(location.href,new URLSearchParams(location.search));//{href,textContent}=dom[dom.length-1]