// ==UserScript==
// @name c.AI Search Sort
// @author EnergoStalin
// @description Sort search so cards with public definition stays on top and marked with a star
// @license AGPL-3.0-only
// @version 1.1.0
// @namespace https://c.ai
// @match https://character.ai/*
// @run-at document-body
// @icon https://www.google.com/s2/favicons?sz=64&domain=character.ai
// @grant GM.addStyle
// ==/UserScript==
(async () => {
var y=Object.defineProperty;var o=(t,e)=>y(t,"name",{value:e,configurable:!0});async function a(t,e=1e4,n=1e3){return new Promise((i,r)=>{let l=e,s=setInterval(async()=>{let c=await t();l-=n,l<=0&&(clearInterval(s),r()),c&&(clearInterval(s),i(c))},n)})}o(a,"waitNotNull");function p(t){let e=unsafeWindow.location.href;new MutationObserver(()=>{e!==unsafeWindow.location.href&&(e=unsafeWindow.location.href,t(e))}).observe(unsafeWindow.document.body,{subtree:!0,childList:!0}),t(e)}o(p,"injectNavigationHook");var q=await a(()=>document.querySelector("#__NEXT_DATA__")?.textContent).then(t=>JSON.parse(t).props.pageProps),T=q.token;async function d(t){return await fetch("https://plus.character.ai/chat/character/info/",{headers:{Authorization:`Token ${T}`,Origin:"https://character.ai/",Referer:"https://character.ai/","Content-Type":"application/json",Accept:"application/json"},method:"POST",body:JSON.stringify({external_id:t})}).then(e=>e.json()).then(e=>e.character)}o(d,"getCharacterInfo");GM.addStyle(`
.tooltip{position:relative;cursor:pointer}.tooltip .tooltip-text{visibility:hidden;text-align:left;z-index:1;opacity:0;transition:opacity .3s;font-size:.7em;color:#a6a6a6;text-wrap:nowrap}.tooltip .tooltip-head{text-align:center;font-size:1.3em;color:#a6a6a6}.tooltip .tooltip-even{flex-basis:50%}.tooltip .tooltip-number{color:#b0a676;text-align:right}.tooltip:hover .tooltip-text{visibility:visible;opacity:1}
`);GM.addStyle(`
.align-items-start{align-items:flex-start}
`);var f='<svg xmlns="http://www.w3.org/2000/svg" height="20px" viewBox="0 -960 960 960" width="20px" fill="#75FB4C"><path d="M371.01-324 480-390.22 589-324l-29-124 97-84-127-11-50-117-50 117-127 11 96.89 83.95L371.01-324ZM480-72 360-192H192v-168L72-480l120-120v-168h168l120-120 120 120h168v168l120 120-120 120v168H600L480-72Zm0-102 90-90h126v-126l90-90-90-90v-126H570l-90-90-90 90H264v126l-90 90 90 90v126h126l90 90Zm0-306Z"/></svg>',u='<svg xmlns="http://www.w3.org/2000/svg" height="20px" viewBox="0 -960 960 960" width="20px" fill="#5985E1"><path d="M288-420q25 0 42.5-17.5T348-480q0-25-17.5-42.5T288-540q-25 0-42.5 17.5T228-480q0 25 17.5 42.5T288-420Zm192 0q25 0 42.5-17.5T540-480q0-25-17.5-42.5T480-540q-25 0-42.5 17.5T420-480q0 25 17.5 42.5T480-420Zm192 0q25 0 42.5-17.5T732-480q0-25-17.5-42.5T672-540q-25 0-42.5 17.5T612-480q0 25 17.5 42.5T672-420ZM480.28-96Q401-96 331-126t-122.5-82.5Q156-261 126-330.96t-30-149.5Q96-560 126-629.5q30-69.5 82.5-122T330.96-834q69.96-30 149.5-30t149.04 30q69.5 30 122 82.5T834-629.28q30 69.73 30 149Q864-401 834-331t-82.5 122.5Q699-156 629.28-126q-69.73 30-149 30Zm-.28-72q130 0 221-91t91-221q0-130-91-221t-221-91q-130 0-221 91t-91 221q0 130 91 221t221 91Zm0-312Z"/></svg>';function h(t){t.querySelector("div[data-status]")?.remove()}o(h,"clearStatus");function v(t,e){let n=document.createElement("div");return n.dataset.status=e,n.classList.add("flex","flex-col","tooltip"),t.classList.add("align-items-start"),t.append(n),n}o(v,"statusWrapper");function m(t){return!!t.querySelector('div[data-status="starred"]')}o(m,"isStarred");function x(t,e){v(t,"starred").innerHTML=`
<div class="flex grow-0 shrink-0 justify-center">
${f}
</div>
<div class="flex flex-row gap-1 tooltip-text">
<span class="tooltip-even">Description</span>
<span class="tooltip-even tooltip-number">${e}</span>
</div>
`}o(x,"setStarredStatus");function g(t){v(t,"pending").innerHTML=`
<div class="flex flex-row grow-0 shrink-0 w-full items-center justify-center">
${u}
</div>
`}o(g,"setPendingStatus");async function S(t){let n=Array.from(t.childNodes).map(async i=>{if(m(i))return[];g(i);let r=await d(i.href.split("/").pop());return h(i),r.description?.length>0?x(i,r.description.length):t.append(i),[i,r.description?.length]});return Promise.all(n)}o(S,"_sort");function b(t,e){let n=t.filter(([i,r])=>r).sort(([i,r],[l,s])=>r>s?1:-1);for(let[i]of n)e.insertBefore(i,e.firstChild)}o(b,"sortByDefinitionLength");async function w(t,e){t.disconnect();let n=await S(e);b(n,e),t.observe(e,{attributes:!1,childList:!0,subtree:!1})}o(w,"sort");p(async()=>{if(console.log(unsafeWindow.location),unsafeWindow.location.pathname!=="/search")return;let t=await a(()=>document.evaluate("/html/body/div[1]/div/main/div/div/div/main/div/div[2]",document).iterateNext()),e=o((n,i)=>w(i,t),"sortSearches");e([],new MutationObserver(e))});
})()