forked from MarshaIIs/ChromeWebStoreSorter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
minifiedsorter.js
1 lines (1 loc) · 4.48 KB
/
minifiedsorter.js
1
async function main(){console.clear();try{await validateUrl();var e=confirm("Start sorting?");console.clear()}catch(e){return}e&&(blurPage(!0),sortWebstore())}async function stop(e){stop=!0,blurPage(!1),window.scrollTo(0,0),"auto"===!e&&console.info("%cThe script was force stopped\n%cDid something go wrong? :(","color: orange; font-weight: bold; font-size: 130%;","")}async function blurPage(e){let o="html { filter: blur(5px); }";if(e){let e=document.createElement("style");e.textContent=o,document.head.appendChild(e)}else document.head.querySelectorAll("style").forEach((e=>{e.textContent.includes(o)&&e.remove()}))}async function validateUrl(){const e="chrome.google.com",o="webstore/search",t="extensions",n=window.location.hostname.toLowerCase(),r=window.location.pathname.toLowerCase(),[l,i,s]=r.split("/").slice(1),a=`${l}/${i}`,c=window.location.search.toLowerCase(),g=new URLSearchParams(c),d=g.get("_category")??"",h=g.get("hl")??"";if(n!==e)throw console.error(`%cError not in Chrome Web Store!\n%cSite should be "${e}"`,"color:orange; font-size: 130%; font-weight: bold","");if(a!==o)throw console.error(`%cError not searching Chrome Web Store!\n%cPath should be "/${o}/..."`,"color:orange; font-size: 130%; font-weight: bold","");if(c!==`?_category=${t}`&&c!==`?hl=${h}&_category=${t}`){if(console.warn(`%cError search not filtered to "${t}" !\n%cRecommend searching for "${t}".`,"color:orange; font-size: 130%; font-weight: bold",""),confirm(`ERROR!\n${t===d?"Search seems broken.":`Recommend searching for ${t}.`}\nWant to search in ${t}?`)){console.clear(),console.info("%cAttempting to fix search...%c\nRemember to %crestart Sorting%c after the page loads","color:orange; font-size: 130%; font-weight: bold","","font-weight: bold; color: skyblue; border-bottom: solid skyblue 2px",""),await new Promise((e=>setTimeout(e,2500)));const e=`https://${n}${r}?_category=${t}`;try{console.info("%cSuccess!\n%cTaking you to search for extensions now.","color:limegreen; font-size: 130%; font-weight: bold",""),await new Promise((e=>setTimeout(e,1500))),window.location.assign(e)}catch(e){throw console.clear(),console.error("%cFatal error!\n%cSorry search fix failed.\n","color:orange; font-size: 130%; font-weight: bold",""),e}}else console.info("%cContinuing sort!\n%cSearching without filtering for extensions.","color:orange; font-size: 130%; font-weight: bold","")}else console.info("%cSuccess!\n%cURL is valid.","color:limegreen; font-size: 130%; font-weight: bold","");return new Promise((e=>{setTimeout((()=>{e("success")}),800)}))}function sortWebstore(e=2){console.info("%cSorting search results!\n%cThis might take a moment. Be right back.","font-size: 130%; font-weight: bold; color: skyblue;","");let o,t=0,n=0,r=0,l=!1;!function e(r){t++,document.querySelectorAll("div.h-a-Hd-mb.a-Hd-mb")[0].scrollIntoView();let i=document.querySelectorAll(".a-d-na.a-d.webstore-test-wall-tile.a-d-zc.Xd.dd");o=i.length,n!=o&&(n=o);if(t>r||l)return t=0,n=0,l=!1,i[o-1].scrollIntoView(!1),void function(){let e=document.querySelectorAll(".a-d-na.a-d.webstore-test-wall-tile.a-d-zc.Xd.dd"),o=Array.from(e),t=[{}];try{o.forEach((e=>{itemTitle=e.querySelector("div.a-na-d-w").textContent.toString(),orderingNum=e.attributes.hasOwnProperty("index")?Number(e.attributes.index.value.toString().replace(/,/g,"")):0,rating_str=e.getElementsByClassName("nAtiRe").length>0?Number(e.getElementsByClassName("nAtiRe")[0].textContent.toString().replace(/,/g,"")).toString():"Not voted",t.push({Order:orderingNum,Title:itemTitle,Rating:rating_str})}))}catch(e){throw console.error(e)}o.sort(((e,o)=>{let t=e.getElementsByClassName("nAtiRe").length>0?Number(e.getElementsByClassName("nAtiRe")[0].textContent.toString().replace(/,/g,"")):0;return(o.getElementsByClassName("nAtiRe").length>0?Number(o.getElementsByClassName("nAtiRe")[0].textContent.toString().replace(/,/g,"")):0)-t}));let n=document.querySelectorAll('.h-a-x > [role="grid"] > [role="row"]'),r=Array.from(n);for(let e=0;e<r.length;e++){row_start=3*e;for(let t=row_start;t<row_start+3&&t<o.length;t++)r[e].appendChild(o[t])}console.info("%cSorting finished!!\n%cResults are now ranked by the number of ratings they have!","color: limegreen; font-weight: bold; font-size: 130%;",""),console.groupCollapsed("Sorted data info"),console.table(t.slice(1),["Title","Rating","Order"]),console.groupEnd(),stop("auto")}();setTimeout((()=>{e(r)}),500)}(100),function o(){"display: none;"===document.querySelectorAll("div.h-a-Kd.a-Hd-mb")[0].attributes.style.value?r++:r=0;if(r>e)return r=0,void(l=!0);setTimeout((()=>{o()}),500)}()}main();