refund-purchases
Version:
Play with a champion or skin for free! 🐧
11 lines (10 loc) • 3.9 kB
JavaScript
(function(){"use strict";try{if(typeof document<"u"){var e=document.createElement("style");e.appendChild(document.createTextNode(".dropdown-refund-items{height:fit-content;position:absolute;right:20px;bottom:57px;width:249px}")),document.head.appendChild(e)}}catch(t){console.error("vite-plugin-css-injected-by-js",t)}})();
/**
* @author balaclava
* @name refund-purchases
* @link https://github.com/controlado/refund-purchases#readme
* @description Play with a champion or skin for free! 🐧
* @version 1.2.1
* @license GPL-3.0-or-later
*/
var u=Object.defineProperty;var h=(s,e,t)=>e in s?u(s,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):s[e]=t;var r=(s,e,t)=>(h(s,typeof e!="symbol"?e+"":e,t),t);import{StoreBase as p,waitShadowRoot as m,linkEndpoint as f,addRoutines as w,sleep as y,sendChatNotification as v}from"https://cdn.jsdelivr.net/npm/balaclava-utils@latest";class g extends p{constructor(){super(...arguments);r(this,"validInventoryTypes",["CHAMPION","CHAMPION_SKIN"])}refund(t){const n={inventoryType:t.inventoryType,transactionId:t.transactionId,accountId:this.summoner.accountId};return this.request("POST","/storefront/v3/refund",n)}async getTransactions(){const{data:t}=await this.request("GET","/storefront/v3/history/purchase");return t.transactions.filter(n=>this.validInventoryTypes.includes(n.inventoryType))}}class T{constructor(){this.items=[]}async setItems(){const t=await(await fetch("/lol-game-data/assets/v1/skins.json")).json();this.items=Object.values(t);for(const n of this.items)n.id=n.isBase?Math.floor(n.id/1e3):n.id}getItemName(e){const t=this.items.find(n=>n.id==e);return t?t.name:null}}class b{constructor(e,t,n){r(this,"green","#33CA72");r(this,"yellow","#F9BD0A");r(this,"red","#CA3336");this.element=document.createElement("lol-uikit-framed-dropdown"),this.element.classList.add("dropdown-refund-items"),this.text=e,this.store=t,this.assets=n,this.transactions=null}addOption(e,t,{color:n}={}){var d,c;const o=document.createElement("lol-uikit-dropdown-option");o.setAttribute("slot","lol-uikit-dropdown-option"),o.addEventListener("click",()=>{Toast.promise(t(),{loading:`Refunding ${e}...`,success:`Refunded ${e}!`,error:`Error refunding ${e}!`})}),o.innerText=e,(c=(d=o.shadowRoot)==null?void 0:d.querySelector("div"))==null||c.style.setProperty("color",n),this.element.appendChild(o)}setupOptions(){for(const e of this.transactions){const n=e.refundable||e.refundabilityMessage==="TOO_SOON_TO_REFUND"?e.requiresToken===void 0?this.yellow:this.green:this.red;this.addOption(this.assets.getItemName(e.itemId),()=>this.store.refund(e),{color:n})}m(this.element).then(e=>{if(!e.querySelector("#controlado-placeholder")){const t=e.querySelector(".ui-dropdown-current"),n=this.getNewPlaceholder();t.appendChild(n)}})}getNewPlaceholder(){const e=document.createElement("div");return e.classList.add("ui-dropdown-current-content"),e.style="overflow: visible; text-align: end;",e.id="controlado-placeholder",e.innerText=this.text,e}async refreshOptions(){this.transactions=await this.store.getTransactions(),this.element.innerHTML="",this.setupOptions()}}const O="1.2.1";const a=new g,l=new T,i=new b("Transactions",a,l);async function I(s,e){const t=document.querySelector(s);!t||t.hasAttribute(e)||(t.setAttribute(e,"true"),await a.wait(),await l.setItems(),await i.refreshOptions(),await y(1e3),document.querySelector(".dodge-button-container")&&(console.debug("refund-purchases: Dodge button detected!"),v("Dodge button detected!"),i.element.style.bottom="96px",i.element.style.width="260px"),t.appendChild(i.element))}window.addEventListener("load",()=>{f("/lol-inventory/v1/wallet",async s=>{s.eventType==="Update"&&(console.debug("refund-purchases: Refreshing dropdown..."),await a.wait(),i.refreshOptions())}),w(()=>I("div.champion-select","refund-purchases")),console.debug(`refund-purchases(${O}): Report bugs to Balaclava#1912`)});