VK-Gif

Gif на аватар VK

  1. // ==UserScript==
  2. // @name VK-Gif
  3. // @description Gif на аватар VK
  4. // @author Last8Exile
  5. // @license MIT
  6. // @version 1.35
  7. // @noframes
  8. // @match *://vk.com/*
  9. // @namespace https://greasyfork.org/users/61164
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14. if (window.top != window.self)
  15. return;
  16.  
  17. document.body.addEventListener("DOMNodeInserted",refresh);
  18. refresh();
  19.  
  20. function refresh()
  21. {
  22. var profile = document.querySelector(".Profile");
  23. var updated;
  24. if (profile === null)
  25. return;
  26.  
  27. updated = profile.getAttribute("avatar");
  28. if (updated !== null)
  29. return;
  30.  
  31. var statusBar = document.querySelector(".ProfileInfo__status");
  32. if (statusBar === null)
  33. return;
  34. var statusText = statusBar.innerText;
  35. var posDocument = statusText.lastIndexOf("<!>");
  36. var posInternet = statusText.lastIndexOf("<?>");
  37.  
  38. var avatar = document.querySelector(".vkuiImageBase__img");
  39. if (avatar === null)
  40. return;
  41.  
  42. if (posDocument >= 0)
  43. {
  44. var link = statusText.slice(posDocument+3);
  45.  
  46. var request = new XMLHttpRequest();
  47.  
  48. var page = document.createElement("div");
  49.  
  50. var qr = new XMLHttpRequest();
  51. qr.open('get',link);
  52. qr.send();
  53. qr.onreadystatechange=function()
  54. {
  55. if (this.responseText === "")
  56. return;
  57. updated = profile.getAttribute("avatar");
  58. if (updated !== null)
  59. return;
  60.  
  61. page.innerHTML=this.responseText;
  62. var image = page.querySelector("img");
  63. var imageSrc = image.src;
  64. var questPos = imageSrc.lastIndexOf("?");
  65. var gifLink = imageSrc.slice(0,questPos);
  66.  
  67. GetMeta(gifLink,function(w,h) {avatar.height = CalcHeight(w,h,avatar.width);});
  68. avatar.src = gifLink;
  69. statusBar.innerText = statusText.slice(0,posDocument); //Удалите эту строчку чтобы скрипт оставлял ссылку на картинку в статусе.
  70. profile.setAttribute("avatar","updated");
  71. };
  72. }
  73. else if (posInternet >= 0)
  74. {
  75. var gifLink = statusText.slice(posInternet+3);
  76. GetMeta(gifLink,function(w,h) {avatar.height = CalcHeight(w,h,avatar.width);});
  77. avatar.src = gifLink;
  78. statusBar.innerText = statusText.slice(0,posInternet); //Удалите эту строчку чтобы скрипт оставлял ссылку на картинку в статусе.
  79. profile.setAttribute("avatar","updated");
  80. }
  81. else
  82. {
  83. profile.setAttribute("avatar","updated");
  84. return;
  85. }
  86. }
  87. function CalcHeight(nWidth, nHeight, defWidth)
  88. {
  89. var coef = 1.0*nWidth/defWidth;
  90. return nHeight/coef;
  91. }
  92. function GetMeta(url, callback)
  93. {
  94. var img = new Image();
  95. img.src = url;
  96. img.onload = function() { callback(this.width, this.height); };
  97. }
  98. })();