picMF

show MetaFilter profile pictures next to names in posts and comments

目前為 2014-10-04 提交的版本,檢視 最新版本

  1. // picMF.user.js
  2. //
  3. // Written by: Michael Devore
  4. // Released to the public domain
  5. //
  6. // This is a Greasemonkey script.
  7. // See http://www.greasespot.net/ for more information on Greasemonkey.
  8. //
  9. // ==UserScript==
  10. // @name picMF
  11. // @namespace http://www.devoresoftware.com/gm/picMF
  12. // @description show MetaFilter profile pictures next to names in posts and comments
  13. // @match https://*.metafilter.com/*
  14. // @match http://*.metafilter.com/*
  15. // @grant GM_xmlhttpRequest
  16. // @run-at document-end
  17. // @version 1.0
  18. // ==/UserScript==
  19. //
  20.  
  21. "use strict";
  22.  
  23. var theWidth = "64px";
  24. var theHeight = "64px";
  25. var zoomWidth = "256px";
  26. var zoomHeight = "256px";
  27. var serverPrefix = "//s3-us-west-2.amazonaws.com/mefi.profile/";
  28.  
  29. function onLoaded()
  30. {
  31. var xpath = "//DIV/SPAN[starts-with(text(),'posted by') and (@class='smallcopy' or @class='smallcopy postbyline')]";
  32. var postNodes = document.evaluate(
  33. xpath,
  34. document,
  35. null,
  36. XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
  37. null
  38. );
  39. var total = postNodes.snapshotLength;
  40. for (var i = 0; i < total; i++)
  41. {
  42. // not much validation here, cuts performance overhead by avoiding extra tests against the nodes
  43. // tighten it down later if it conflicts with other add-ons or Metafilter bling
  44. var userSpan = postNodes.snapshotItem(i);
  45. var currentNode = userSpan.firstChild;
  46. var found = false;
  47. var userLink;
  48. var linkNode;
  49. while (currentNode && !found)
  50. {
  51. if (currentNode.nodeName === "A")
  52. {
  53. var href_value = currentNode.getAttribute('href');
  54. var result = href_value.match(/\/user\/(\d+)/);
  55. if (result && result[1])
  56. {
  57. var userNumber = result[1];
  58. var serverLink = serverPrefix+userNumber+".jpg";
  59. var img = document.createElement("img");
  60. img.setAttribute("src", serverLink);
  61. setTimeout(function(x, y)
  62. {
  63. return function()
  64. {
  65. if (x.height > 0)
  66. {
  67. x.setAttribute("height", theHeight);
  68. x.setAttribute("width", theWidth);
  69. y.insertBefore(document.createTextNode(" "), y.firstChild);
  70. y.insertBefore(x, y.firstChild);
  71. x.addEventListener('mouseover', picHover, false);
  72. x.addEventListener('mouseout', picRestore, false);
  73. }
  74. };
  75. }(img, currentNode), 1000);
  76. break;
  77. }
  78. }
  79. currentNode = currentNode.nextSibling;
  80. }
  81. }
  82. }
  83.  
  84. function picHover(evt)
  85. {
  86. var picNode = evt['target'];
  87. picNode.setAttribute("height", zoomHeight);
  88. picNode.setAttribute("width", zoomWidth);
  89. }
  90.  
  91. function picRestore(evt)
  92. {
  93. var picNode = evt['target'];
  94. picNode.setAttribute("height", theHeight);
  95. picNode.setAttribute("width", theWidth);
  96. }
  97.  
  98. document.addEventListener('DOMContentLoaded',onLoaded,true);