Link Tooltips

Adds and/or modifies link and image title attributes to include more informaton

  1. // ==UserScript==
  2. // @name Link Tooltips
  3. // @namespace Cromagnon
  4. // @description Adds and/or modifies link and image title attributes to include more informaton
  5. // @include *
  6. // @version 1.2.0202
  7. // ==/UserScript==
  8.  
  9.  
  10.  
  11. //go through and add or modify each link's title
  12. //
  13. // !Link.title -> Link.href
  14. //
  15. // !Link.href
  16. //
  17. // Link.title -> unchanged
  18. //
  19. // Link.href = #
  20. //
  21. // Link.title -> unchanged
  22. //
  23. // Link.title -> Link.title + ( Link.href )
  24. //
  25. // !Img.title
  26. //
  27. // !Img.alt
  28. //
  29. // Img.title -> Link.title
  30. //
  31. // !Link.href
  32. //
  33. // Img.title -> Img.alt
  34. //
  35. // Link.href = #
  36. //
  37. // Img.title -> Img.alt
  38. //
  39. // Img.title -> Img.alt + ( Link.href )
  40. //
  41. // !Link.href
  42. //
  43. // Img.title -> unchanged
  44. //
  45. // Link.href = #
  46. //
  47. // Img.title -> unchanged
  48. //
  49. // Img.title -> Img.title + ( Link.href )
  50. //
  51. //
  52. function checkLinkTitles(e) {
  53.  
  54. var target = (e == null) ? document : e.target;
  55. var links = target.getElementsByTagName("a");
  56. if (links.length == 0) {
  57. if (target.nodeName != "A") return;
  58. links = [];
  59. links.push(target);
  60.  
  61. };
  62. for (var i = 0; link = links[i]; ++i) {
  63. //if (link.getAttribute("modified-by") == "Link Tooltips") continue;
  64. //link.setAttribute("modified-by", "Link Tooltips");
  65. var href = link.getAttribute("href");
  66. var title = link.getAttribute("title");
  67. if (!title) {
  68. if (href != null) link.setAttribute("title", href);
  69. } else if ((href != null) && (href != "#")) {
  70.  
  71. if ((title != href) && (title.indexOf("(" + href + ")") == -1))
  72. link.setAttribute("title", title + "\n (" + href + ")");
  73. };
  74.  
  75.  
  76. //in the latest FireFox versions, titles on image elements override those on enclosing links
  77. var imgs = link.getElementsByTagName("img");
  78. for(var j = 0; img = imgs[j]; ++j) {
  79. var title = img.getAttribute("title");
  80. var alt = img.getAttribute("alt");
  81. if (!title) {
  82. if (!alt)
  83. img.setAttribute("title", link.getAttribute("title"));
  84. else if ((href != null) && (href != "#"))
  85. img.setAttribute("title", alt + "\n (" + href + ")");
  86. else img.setAttribute("title", alt);
  87. }
  88. else if ((href != null) && (href != "#")) {
  89. if ((title != href) && (title.indexOf("(" + href + ")") == -1))
  90. img.setAttribute("title", title + "\n (" + href + ")");
  91. }
  92. }
  93. }
  94. };
  95.  
  96.  
  97.  
  98. // Main function
  99. //
  100. function init() {
  101.  
  102. checkLinkTitles();
  103.  
  104. document.addEventListener("DOMSubtreeModified", checkLinkTitles, false);
  105.  
  106. };
  107.  
  108.  
  109.  
  110. // Call our main function.
  111. //
  112.  
  113. init();
  114.  
  115. //end