/** * SteamCards - Steam Cards for your website * * Module: Steam User Widget * * Visit: https://github.com/danielbrendel */ const STEAMCARDS_USER_ENDPOINT = 'http://localhost:8000'; const STEAMCARDS_USER_VERSION = 'v1'; /** * Class SteamUserElem * * Handle custom HTML element to render Steam user widgets */ class SteamUserElem extends HTMLElement { storedData = {}; oldHeader = ''; connectedCallback() { var steamid = (typeof this.attributes.steamid !== 'undefined') ? this.attributes.steamid.value : null; var header = (typeof this.attributes.header !== 'undefined') ? this.attributes.header.value : ''; var width = (typeof this.attributes.width !== 'undefined') ? this.attributes.width.value : null; var height = (typeof this.attributes.height !== 'undefined') ? this.attributes.height.value : null; var online_yes = (typeof this.attributes['online-yes'] !== 'undefined') ? this.attributes['online-yes'].value : 'online'; var online_no = (typeof this.attributes['online-no'] !== 'undefined') ? this.attributes['online-no'].value : 'offline'; var member_since = (typeof this.attributes['member-since'] !== 'undefined') ? this.attributes['member-since'].value : 'Member since: :year-:month-:day'; var viewtext = (typeof this.attributes.viewtext !== 'undefined') ? this.attributes.viewtext.value : 'View'; var styleBorder = (typeof this.attributes['style-border'] !== 'undefined') ? this.attributes['style-border'].value : null; var styleShadow = (typeof this.attributes['style-shadow'] !== 'undefined') ? parseInt(this.attributes['style-shadow'].value) : 1; var styleColorBackground = (typeof this.attributes['style-color-background'] !== 'undefined') ? this.attributes['style-color-background'].value : null; var styleColorTextBright = (typeof this.attributes['style-color-text-bright'] !== 'undefined') ? this.attributes['style-color-text-bright'].value : null; var styleColorTextDark = (typeof this.attributes['style-color-text-dark'] !== 'undefined') ? this.attributes['style-color-text-dark'].value : null; if (steamid !== null) { this.storedData = { steamid: steamid, header: header, width: width, height: height, online_yes: online_yes, online_no: online_no, member_since: member_since, viewtext: viewtext, styleBorder: styleBorder, styleShadow: styleShadow, styleColorBackground: styleColorBackground, styleColorTextBright, styleColorTextDark }; this.setupCard( steamid, header, width, height, online_yes, online_no, member_since, viewtext, styleBorder, styleShadow, styleColorBackground, styleColorTextBright, styleColorTextDark ); } } setupCard(steamid, header, width, height, online_yes, online_no, member_since, viewtext, styleBorder, styleShadow, styleColorBackground, styleColorTextBright, styleColorTextDark) { var req = new XMLHttpRequest(); var self = this; req.onreadystatechange = function() { if (req.readyState == XMLHttpRequest.DONE) { let json = JSON.parse(req.responseText); if (!document.getElementById('steamcards-user-styles')) { let link = document.createElement('link'); link.id = 'steamcards-user-styles'; link.rel = 'stylesheet'; link.type = 'text/css'; link.href = STEAMCARDS_USER_ENDPOINT + '/api/resource/query?type=css&module=user&version=' + STEAMCARDS_USER_VERSION; document.getElementsByTagName('head')[0].appendChild(link); } let cardImageStyle = ''; let cardStyle = ''; if ((width !== null) || (styleBorder !== null) || (styleShadow !== true) || (styleColorBackground !== null)) { let widthCode = ''; if (width !== null) { widthCode = 'max-width: ' + width + 'px;'; } let borderCode = ''; if (styleBorder !== null) { if (styleBorder === 'max') { borderCode = 'border-radius: 25px;'; cardImageStyle = 'border-top-left-radius: 25px; border-top-right-radius: 25px;'; } else if (styleBorder === 'small') { borderCode = 'border-radius: 4px;'; cardImageStyle = 'border-top-left-radius: 4px; border-top-right-radius: 4px;'; } else if (styleBorder === 'none') { borderCode = 'border-radius: unset;'; cardImageStyle = 'border-top-left-radius: unset; border-top-right-radius: unset;'; } } let shadowCode = ''; if (!styleShadow) { shadowCode = 'box-shadow: unset;'; } let bgColor = ''; if (styleColorBackground !== null) { bgColor = 'background-color: ' + styleColorBackground + ';'; } cardStyle = 'style="' + widthCode + borderCode + shadowCode + bgColor + '"'; } let bgimage = ''; if (header !== '') { bgimage = 'background-image: url(\'' + header + '\');'; } let onstatus = ''; let oncolor = ''; if (json.data.personastate) { onstatus = online_yes; oncolor = 'steam-user-infos-right-online-green'; } else { onstatus = online_no; oncolor = ''; } let regdate = new Date(json.data.timecreated * 1000); member_since = member_since.replace(':year', regdate.getFullYear()); member_since = member_since.replace(':month', regdate.getMonth() + 1); member_since = member_since.replace(':day', regdate.getDate()); let html = `