Steam Error Widget Helper

For error widgets shows steamdb.info link and game name (if possible).

2021-03-02 يوللانغان نەشرى. ئەڭ يېڭى نەشرىنى كۆرۈش.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name        	Steam Error Widget Helper
// @namespace   	https://greasyfork.org/users/2205
// @description:ru 	Для виджетов с ошибкой отображает линк на steamdb.info и если возможно - название игры.
// @description	 	For error widgets shows steamdb.info link and game name (if possible).
// @include     	http://store.steampowered.com/*
// @include     	https://store.steampowered.com/*
// @run-at      	document-end
// @version     	1.3
// @language        English
// @connect         api.steampowered.com
// @connect         steamcommunity.com
// @grant           GM_xmlhttpRequest
// @grant           GM.xmlhttpRequest
// ==/UserScript==

var appid;

var getJSON = function(url, callback) {
    GM_xmlhttpRequest({
        method: "GET",
        url: url,
        onload: function(response) {
                   //console.log(response);
                   if (response.status === 200) {
                       let res = /<title>.+\s::\s(.+)<\/title>/.exec(response.response);
                       let name = null;
                       if (res != null) {
                           name = res[1];
                       }
                       //console.log(name);
                       callback(name)
                       /*
                         let data = JSON.parse(response.response);
                          if (!('response' in data)) {
                              if (data[appid].success == true) {
                                 if (typeof(data[appid].data.name)!=='undefined') {
                                     callback(data[appid].data.name);
                                     return;
                                 }
                              }
                              getJSONRL('https://api.steampowered.com/IStoreService/GetAppList/v1/?key=7BAC459A4D25F889FAB7813D49069C7B&max_results=1&last_appid='+(appid-1), callback);
                              return;
                         } else {//second request
                             let data = JSON.parse(response.response);
                             if (data.response.last_appid == appid) {
                                 callback(data.response.apps[0].name);
                                 return;
                             }
                             callback(null);
                             return;
                          }*/
                   } else {
                         callback(null);
                   }
        },
        onerror: function() {
           console.log('Error.');
        },
        ontimeout: function() {
           console.log('Error.');
        }
    });

};

//rate limiting
var getJSONRL = function(url, callback) {
  var Rate=1500; //ms between requests;
    var lastCall=localStorage.getItem ('ITCFTRateLimiter');
    if (lastCall!==null) {
      if ((parseInt(lastCall) + Rate) > Date.now()) {
        window.setTimeout(function(){
          getJSONRL(url,callback);
        },parseInt(lastCall)+Rate-Date.now());
      } else { //already time
        getJSON(url,callback);
        localStorage.setItem('ITCFTRateLimiter',Date.now());
      }
    } else { //first call ever
      getJSON(url,callback);
      localStorage.setItem('ITCFTRateLimiter',Date.now());
    }
};

+function(){

      //forum widgets
      var wcontainer=document.getElementById ('widget');
      if (wcontainer!==null) {
        var appimagecontainers=wcontainer.getElementsByClassName('capsule');
        if (appimagecontainers.length===0) {
            appid=window.location.href.match(/(http.{0,1}:\/\/store\.steampowered\.com\/)(.*)\/(\d+)(.*)/)[3];
          //getJSONRL('https://api.steampowered.com/IStoreService/GetAppList/v1/?key=7BAC459A4D25F889FAB7813D49069C7B&max_results=1&last_appid='+appid, function(err, data) {
            //getJSONRL('/api/appdetails?appids='+appid, function(gamename) {
              getJSONRL('https://steamcommunity.com/app/'+appid, function(gamename) {
              let ImageElement = document.createElement("img");
                  //https://steamcdn-a.akamaihd.net/steam/apps/761430/header_292x136.jpg
                  //https://cdn.akamai.steamstatic.com/steam/apps/761430/capsule_184x69.jpg
                  //https://cdn.cloudflare.steamstatic.com/steam/apps/1554300/header.jpg
              ImageElement.setAttribute("style","margin:5px;float:right");
              ImageElement.setAttribute("src","https://cdn.akamai.steamstatic.com/steam/apps/"+appid+"/capsule_184x69.jpg")
              wcontainer.appendChild(ImageElement);
	          let NewElement=document.createElement("div");
              NewElement.setAttribute("class","desc");
              let NameElement=NewElement.appendChild(document.createElement("p"));
              NameElement.setAttribute("style","font-size: 20px !important; line-height: 28px !important");
              let SteamUrlElement=NameElement.appendChild(document.createElement("a"));
              SteamUrlElement.setAttribute("style", "color: #898a8c !important;");
              SteamUrlElement.setAttribute("href","https://store.steampowered.com/app/"+appid);
              SteamUrlElement.setAttribute("target","_blank");
              if (gamename===null) {
                SteamUrlElement.appendChild(document.createTextNode("Unknown app "+appid));
              } else {
                SteamUrlElement.appendChild(document.createTextNode(gamename));
              }
              let UrlElement=NewElement.appendChild(document.createElement("a"));
              UrlElement.setAttribute("href","https://steamdb.info/app/"+appid);
              UrlElement.setAttribute("style","color: #898a8c !important; text-decoration: underline !important; line-height: 20px !important");
              UrlElement.setAttribute("target","_blank");
              UrlElement.appendChild(document.createTextNode("View on Steam Database ("+appid+")"));
              let showplace=wcontainer.appendChild(NewElement);
          });
        }
      }
}();