Document Modification Display

Shows when the document was last modified

  1. // ==UserScript==
  2. // @name Document Modification Display
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.5.2
  5. // @description Shows when the document was last modified
  6. // @author You
  7. // @match *://*/*
  8. // @grant none
  9. // ==/UserScript==
  10. (function() {
  11. 'use strict';
  12.  
  13. buildAndAddModificationDisplay();
  14. })();
  15.  
  16. function buildAndAddModificationDisplay() {
  17. document.getElementsByTagName("BODY")[0].appendChild(createModificationContainer());
  18. }
  19.  
  20. function createModificationContainer() {
  21. var modification_display = document.createElement("div");
  22. modification_display.setAttribute("id", "modification-container");
  23. addMinMaxButton(modification_display);
  24.  
  25. var modification_content = document.createElement("div");
  26. modification_content.setAttribute("id", "modification_content");
  27. modification_display.appendChild(modification_content);
  28.  
  29. addLastModified(modification_content);
  30. addSitemapXml(modification_content);
  31.  
  32. return modification_display;
  33. }
  34.  
  35. var modification_display_open = true;
  36.  
  37. function addMinMaxButton(container) {
  38. var minMaxButton = document.createElement("div");
  39. minMaxButton.setAttribute("id", "min-max-button");
  40. minMaxButton.appendChild(document.createTextNode("-"));
  41. minMaxButton.setAttribute("style", "position: absolute;right: 0;top: 0;line-height: 9px;border-left: solid 2px;border-bottom: solid 2px;height: 14px;width: 15px;text-align: center;");
  42.  
  43. minMaxButton.addEventListener("click", function(e) {
  44. var modContainer = document.getElementById("modification-container");
  45.  
  46. if (modification_display_open) {
  47. minimizeModContainer(modContainer, minMaxButton);
  48. } else {
  49. maximizeModContainer(modContainer, minMaxButton);
  50. }
  51. });
  52.  
  53. container.appendChild(minMaxButton);
  54. maximizeModContainer(container, minMaxButton);
  55. }
  56.  
  57. var cached_modification_content;
  58.  
  59. function minimizeModContainer(elem, button) {
  60. button.innerHTML = '';
  61. button.appendChild(document.createTextNode("+"));
  62.  
  63. cached_modification_content = document.getElementById("modification_content").innerHTML;
  64.  
  65. var minimized_css = "position: fixed;height: 20px;bottom: -3px;left: -3px;padding-right: 20px;border: solid grey;border-radius: 3px;background-color: lightgray;z-index:1001;color:grey!important;";
  66. elem.setAttribute("style", minimized_css);
  67. document.getElementById("modification_content").innerHTML = '';
  68.  
  69. modification_display_open = false;
  70. }
  71.  
  72. function maximizeModContainer(elem, button) {
  73. button.innerHTML = '';
  74. button.appendChild(document.createTextNode("-"));
  75.  
  76. var maximized_css = "position: fixed;bottom: -3px;left: -3px;padding: 5px;padding-right: 20px;border: solid grey;border-radius: 3px;background-color: lightgray;z-index:1001;color:grey!important;";
  77. elem.setAttribute("style", maximized_css);
  78.  
  79. if (cached_modification_content) {
  80. document.getElementById("modification_content").innerHTML = cached_modification_content;
  81. }
  82.  
  83. modification_display_open = true;
  84. }
  85.  
  86. function addLastModified(container) {
  87. var last_modified_container = document.createElement("div");
  88. last_modified_container.setAttribute("id", "last_modified_container");
  89. var last_modfied_text = getLastModifiedText();
  90. var last_modified_content = document.createTextNode(last_modfied_text);
  91. last_modified_container.appendChild(last_modified_content);
  92. if (last_modfied_text.indexOf("Now") !== -1) {
  93. addHintToLastModified(last_modified_container);
  94. }
  95.  
  96. container.appendChild(last_modified_container);
  97. }
  98.  
  99. function getLastModifiedText() {
  100. var now = Date.now();
  101. var lastModifiedDate = Date.parse(document.lastModified);
  102. var secondsSinceModified = (now - lastModifiedDate) / 1000;
  103. var lastModifiedData = secondsSinceModified > 3 ? document.lastModified : "Now";
  104.  
  105. return 'Last Modified: ' + lastModifiedData;
  106. }
  107.  
  108. function addHintToLastModified(container) {
  109. var hintHoverable = document.createElement("sup");
  110. hintHoverable.setAttribute("style", "margin-left: 3px;cursor: help;");
  111. hintHoverable.appendChild(document.createTextNode("?"));
  112. hintHoverable.appendChild(getTooltip());
  113.  
  114. addHintHoverEvents(hintHoverable);
  115.  
  116. container.appendChild(hintHoverable);
  117. }
  118.  
  119. function addHintHoverEvents(hintElem) {
  120. var css_show = "display: block;background: #C8C8C8;margin-left: 28px;padding: 5px;position: fixed;z-index: 1000;width: 183px;left: 95px;bottom: 43px;border: darkgrey dotted;font-size:12px;text-align:center;";
  121.  
  122. hintElem.addEventListener("mouseenter", function(event) {
  123. var tooltip = document.getElementById("now-hint-tooltip");
  124. tooltip.setAttribute("style", css_show);
  125. });
  126.  
  127. hintElem.addEventListener("mouseleave", function(event) {
  128. var tooltip = document.getElementById("now-hint-tooltip");
  129. tooltip.setAttribute("style", "display: none");
  130. });
  131. }
  132.  
  133. function getTooltip() {
  134. var tooltip = document.createElement("div");
  135. tooltip.setAttribute("id", "now-hint-tooltip");
  136. tooltip.setAttribute("style", "display: none");
  137. tooltip.appendChild(document.createTextNode("This is likely a dynamic page."));
  138.  
  139. return tooltip;
  140. }
  141.  
  142. var sitemap_loaded = false;
  143.  
  144. function addSitemapXml(container) {
  145. var getUrl = window.location;
  146. var sitemapUrl = getUrl.protocol + "//" + getUrl.host + "/sitemap.xml";
  147.  
  148. var sitemap_container = document.createElement("div");
  149. sitemap_container.setAttribute("id", "sitemap_xml_link");
  150. animateSitemapLoad(sitemap_container, 1, 0);
  151. container.append(sitemap_container);
  152.  
  153. checkIfSitemapExists(
  154. sitemapUrl,
  155. function() {
  156. addSitemapXmlSuccess(sitemap_container, sitemapUrl);
  157. },
  158. function() {
  159. addSitemapXmlFailure(sitemap_container);
  160. });
  161. }
  162.  
  163. function addSitemapXmlSuccess(container, url) {
  164. var sitemap_link = document.createElement("a");
  165. sitemap_link.setAttribute("href", url);
  166. sitemap_link.setAttribute("target", "_blank");
  167. sitemap_link.setAttribute("style", "color: green!important;text-decoration: underline;");
  168.  
  169. var sitemap_text = document.createTextNode("Sitemap");
  170. sitemap_link.appendChild(sitemap_text);
  171.  
  172. container.innerHTML = '';
  173. container.appendChild(sitemap_link);
  174. }
  175.  
  176. function addSitemapXmlFailure(container) {
  177. var sitemap_link = document.createElement("a");
  178. sitemap_link.setAttribute("style", "color: red!important;text-decoration: underline;text-decoration-style: dotted;");
  179.  
  180. var sitemap_text = document.createTextNode("Sitemap not found");
  181. sitemap_link.appendChild(sitemap_text);
  182.  
  183. container.innerHTML = '';
  184. container.appendChild(sitemap_link);
  185. }
  186.  
  187. function animateSitemapLoad(sitemap_container, direction, iteration) {
  188. sitemap_container.innerHTML = "";
  189. sitemap_container.appendChild(
  190. document.createTextNode(
  191. getLoadingMessageForFrame(iteration)));
  192. sitemap_container.setAttribute("style", "color: darkgrey!important;");
  193.  
  194. iteration += direction;
  195. if (iteration === 3) {
  196. direction = -1;
  197. } else if (iteration === 0) {
  198. direction = 1;
  199. }
  200.  
  201. setTimeout(function () {
  202. if (!sitemap_loaded) {
  203. animateSitemapLoad(sitemap_container, direction, iteration);
  204. }
  205. }, 250);
  206. }
  207.  
  208. function getLoadingMessageForFrame(numDots) {
  209. var msg = "Loading";
  210.  
  211. for (var i = 0; i < numDots; i++) {
  212. msg += ".";
  213. }
  214.  
  215. return msg;
  216. }
  217.  
  218. function checkIfSitemapExists(url, success, failure) {
  219. var xhttp = new XMLHttpRequest();
  220. xhttp.onreadystatechange = function() {
  221. if (this.readyState == 4 && (this.status == 200 || this.status == 302)) {
  222. sitemap_loaded = true;
  223. success();
  224. } else if (this.readyState == 4) {
  225. sitemap_loaded = true;
  226. failure();
  227. }
  228. };
  229. xhttp.open("GET", url, true);
  230. xhttp.send();
  231. }