shop-champion-select
Version:
Buy champions inside Champion Select! 🐧
14 lines (13 loc) • 4.98 kB
JavaScript
(function(){"use strict";try{if(typeof document<"u"){var i=document.createElement("style");i.appendChild(document.createTextNode(".mini-icon-champion{display:none;position:relative;height:100%;flex-shrink:0;margin-left:2px;box-sizing:border-box;background-size:contain}.mini-icon-img{position:inherit;height:100%;border:1px solid #46371487;width:inherit;overflow:hidden;object-fit:cover;box-sizing:border-box;background-color:#010a13}.mini-icon-img:hover{border-image:linear-gradient(to top,#46371487 0%,#695625 23%,#b88d35 93%,#c8aa6e 100%) 1}.champion-grid .champion-grid-header .champion-input{height:25px}")),document.head.appendChild(i)}}catch(e){console.error("vite-plugin-css-injected-by-js",e)}})();
/**
* @author balaclava
* @name shop-champion-select
* @link https://github.com/controlado/shop-champion-select#readme
* @description Buy champions inside Champion Select! 🐧
* @version 6.7.5
* @license GPL-3.0-or-later
*/
import{addRoutines as C,sleep as v,request as m,linkEndpoint as f,StoreBase as E,Champion as L,layerManager as d}from"https://cdn.skypack.dev/balaclava-utils@latest";const k="6.7.5",A="en",B={pt:`Botão esquerdo: compra o campeão
Botão direito: compra e seleciona o campeão`,es:`Botón izquierdo: compra el campeón
Botón derecho: compra y selecciona el campeón`,en:`Left click: buy the champion
Right click: buy and select the champion`},h={missing:A,tooltip:B};class I extends E{async getNotOwnedChampions(){return(await this.request("GET","/storefront/v3/view/champions")).data.catalog.filter(e=>!e.owned).map(e=>({...e,name:e.name.toLowerCase()}))}}class R{constructor(t){this.element=document.createElement("div"),this.element.classList.add("mini-icon-champion"),this.img=document.createElement("img"),this.img.classList.add("mini-icon-img"),this.element.appendChild(this.img),this.store=t,this.champion=null}setChampion(t){this.champion=new L(t.itemId,t.ip),this.img.src=`/lol-game-data/assets/v1/champion-icons/${this.champion.id}.png`}show(){this.element.style.display="block"}hide(){this.element.style.display="none"}}class S{constructor(t){this.element=document.createElement("div"),this.element.setAttribute("id","lol-uikit-tooltip-root"),this.element.classList.add("tooltip","tooltip-random-champion");const e=document.createElement("lol-uikit-tooltip");e.setAttribute("data-tooltip-position",t),e.setAttribute("type","system");const n=document.createElement("lol-uikit-content-block");n.setAttribute("type","tooltip-system");const s=document.createElement("p");s.innerText=this.getLabel(),n.appendChild(s),e.appendChild(n),this.element.appendChild(e)}getLabel(){const t=document.body.dataset.lang;return h.tooltip[t]||h.tooltip[h.missing]}show(t){const e=t.getBoundingClientRect();this.element.style.top=`${e.top+e.height-46}px`,this.element.style.left=`${e.left+e.width}px`,d.appendChild(this.element)}hide(){d.removeChild(this.element)}}class T{constructor(t,e){this.element=document.querySelector(t),this.store=e,this.champions=[]}async refreshPlaceholder(){const t=await m("GET","/lol-inventory/v1/wallet/lol_blue_essence"),{lol_blue_essence:e}=await t.json();this.element.setAttribute("placeholder",`BE: ${e}`)}async updateChampions(){const t=new Event("input");this.champions=await this.store.getNotOwnedChampions(),this.element.dispatchEvent(t)}getSearchedChampions(){const t=this.element.value.toLowerCase();return this.champions.filter(e=>e.name.startsWith(t))}}async function q(a,t){const e=document.querySelector(a);if(!e||e.hasAttribute(t))return;e.setAttribute(t,"true");const n=new I;await n.wait(400);const s=new T(".champion-input",n);await Promise.all([s.refreshPlaceholder(),s.updateChampions()]);const o=new R(n);e.appendChild(o.element);const r=new S("right");o.element.addEventListener("mouseleave",()=>r.hide()),o.element.addEventListener("mouseenter",()=>r.show(o.element)),o.element.addEventListener("click",()=>n.buyChampions(o.champion)),o.element.addEventListener("contextmenu",async()=>{const i=await n.buyChampions(o.champion);if(i.status!==200){const l=await i.json();console.debug("shop-champion-select(buy): error",i,l);return}await v(2e3);const u=await m("GET","/lol-champ-select/v1/session"),{localPlayerCellId:w,actions:g}=await u.json();for(const l of g)for(const c of l)if(c.completed===!1&&c.actorCellId===w){const y={championId:o.champion.id,completed:!1},p=await m("PATCH",`/lol-champ-select/v1/session/actions/${c.id}`,{body:y});if(p.ok)return;{const b=await p.json();console.debug("shop-champion-select(select): error",p,b)}}}),s.element.addEventListener("input",()=>{const i=s.getSearchedChampions();i.length===1?(o.setChampion(...i),o.show()):o.hide()}),f("/lol-inventory/v1/wallet",i=>{i.eventType==="Update"&&(s.refreshPlaceholder(),s.updateChampions(),console.debug("shop-champion-select: Refreshed champions"))})}addEventListener("load",()=>{C(()=>q(".champion-grid-header","shop-champion-select")),console.debug(`shop-champion-select(${k}): Report bugs to Balaclava#1912`)});