Advertisement
sparkychild

Custom Avatar Display Script

Nov 19th, 2017
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name       Avatar Change
  3. // @namespace  http://use.i.E.your.homepage/
  4. // @version    1.1
  5. // @description  Pokemon Showdown! moderator's best friend
  6. // @include     http://play.pokemonshowdown.com/
  7. // @include     https://play.pokemonshowdown.com/
  8. // @include     http://play.pokemonshowdown.com/*
  9. // @include     https://play.pokemonshowdown.com/*
  10. // @include     http://*.psim.us/
  11. // @include     https://*.psim.us/
  12. // @include     http://*.psim.us/*
  13. // @include     https://*.psim.us/*
  14. // ==/UserScript==
  15.  
  16. /////////////////////////////////////////
  17. // PS Custom Avi script by sparkychild //
  18. /////////////////////////////////////////
  19.  
  20. // replace the preset url with whatever you want your avatar to display as for yourself.
  21. // please note only you can see the custom avatar!
  22. var customAvatarUrl = "https://i.imgur.com/YxUyTDB.png";
  23.  
  24. Tools.resolveAvatar = function (avatar) {
  25.     if (avatar === customAvatarUrl)
  26.         return customAvatarUrl;
  27.     var avatarnum = Number(avatar);
  28.     if (!isNaN(avatarnum)) {
  29.         // default avatars
  30.         return Tools.resourcePrefix + 'sprites/trainers/' + avatarnum + '.png';
  31.     }
  32.     if (avatar.charAt(0) === '#') {
  33.         return Tools.resourcePrefix + 'sprites/trainers/' + toId(avatar.substr(1)) + '.png';
  34.     }
  35.     if (window.Config && Config.server && Config.server.registered) {
  36.         // custom avatar served by the server
  37.         var protocol = (Config.server.port === 443) ? 'https' : 'http';
  38.         return protocol + '://' + Config.server.host + ':' + Config.server.port +
  39.             '/avatars/' + _.map(avatar.split('?', 2), encodeURIComponent).join('?');
  40.     }
  41.     // just pick a random avatar
  42.     var sprites = [1, 2, 101, 102, 169, 170];
  43.     return Tools.resolveAvatar(sprites[Math.floor(Math.random() * sprites.length)]);
  44. };
  45.  
  46. try {
  47.     function main2() {
  48.         probeInterval2 = setInterval(function () {
  49.             if (unsafeWindow.app && unsafeWindow.app.socket && unsafeWindow.app.socket.onmessage &&
  50.                 (unsafeWindow.app.socket.onmessage.toString().indexOf("self.receive") >= 0 || unsafeWindow.app.socket._onmessage)) {
  51.                 clearInterval(probeInterval2);
  52.                 unsafeWindow.app.socket._onmessage2 = unsafeWindow.app.socket.onmessage;
  53.                 unsafeWindow.app.socket.onmessage = function (msg) {
  54.                     try {
  55.                         var data = JSON.parse(msg.data);
  56.                         // console
  57.                         data = filterLogEntry2(data);
  58.  
  59.                         // init
  60.                         if (data.log)
  61.                             data.log = filterLog2(data.log);
  62.  
  63.                         // update
  64.                         if (/*data.logUpdate || */data.updates)
  65.                             data.updates = filterLog2(data.updates);
  66.  
  67.                         msg.data = JSON.stringify(data);
  68.                     } catch (e) {
  69.                         msg.data = filterLog2(msg.data.split("\n")).join("\n");
  70.                     }
  71.                     return this._onmessage2(msg);
  72.                 };
  73.             }
  74.         }, 0);
  75.     }
  76.     function filterLog2(log) {
  77.         for (var l = 0; l < log.length; ++l)
  78.             log[l] = filterLogEntry2(log[l]);
  79.         return log;
  80.     }
  81.     function filterLogEntry2(entry) {
  82.         if (typeof entry === "string") {
  83.             if (entry.startsWith('|player|')) {
  84.                 var [nothing, nothing2, side, name, avatar] = entry.split('|');
  85.                 if (name === unsafeWindow.app.user.attributes.name) avatar = customAvatarUrl;
  86.                 return [nothing, nothing2, side, name, avatar].join('|');
  87.             } else if (entry.startsWith("|queryresponse|userdetails|")) {
  88.                 var json = JSON.parse(entry.slice(27));
  89.                 if (json.userid === unsafeWindow.app.user.attributes.userid)
  90.                     json.avatar = customAvatarUrl;
  91.                 return "|queryresponse|userdetails|" + JSON.stringify(json);
  92.             }
  93.         }
  94.         return entry;
  95.     }
  96.  
  97.     setTimeout(function () {
  98.         main2();
  99.     }, 2000);
  100. } catch (e) {
  101.     console.log(e);
  102. }
  103. // ==/UserScript==
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement