Document Modification Display

Shows when the document was last modified

当前为 2020-11-27 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Document Modification Display
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.4.1
  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. var modification_display = createModificationContainer();
  18.  
  19. addLastModified(modification_display);
  20. addSitemapXml(modification_display);
  21.  
  22. var body = document.getElementsByTagName("BODY")[0];
  23. body.appendChild(modification_display);
  24. }
  25.  
  26. function createModificationContainer() {
  27. var modification_display = document.createElement("div");
  28. modification_display.setAttribute("class", "modification-container");
  29. var css = "position: fixed;bottom: -3px;left: -3px;padding: 5px;border: solid grey;border-radius: 3px;background-color: lightgray;z-index:1001;color:grey!important;";
  30. modification_display.setAttribute("style", css);
  31.  
  32. return modification_display;
  33. }
  34.  
  35. function addLastModified(container) {
  36. var last_modified_container = document.createElement("div");
  37. var last_modfied_text = getLastModifiedText();
  38. var last_modified_content = document.createTextNode(last_modfied_text);
  39. last_modified_container.appendChild(last_modified_content);
  40. if (last_modfied_text.indexOf("Now") !== -1) {
  41. addHintToLastModified(last_modified_container);
  42. }
  43.  
  44. container.appendChild(last_modified_container);
  45. }
  46.  
  47. function getLastModifiedText() {
  48. var now = Date.now();
  49. var lastModifiedDate = Date.parse(document.lastModified);
  50. var secondsSinceModified = (now - lastModifiedDate) / 1000;
  51. var lastModifiedData = secondsSinceModified > 3 ? document.lastModified : "Now";
  52.  
  53. return 'Last Modified: ' + lastModifiedData;
  54. }
  55.  
  56. function addHintToLastModified(container) {
  57. 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;";
  58. var css_hide = "display: none";
  59.  
  60. var tooltip = document.createElement("div");
  61. tooltip.setAttribute("id", "now-hint-tooltip");
  62. tooltip.setAttribute("style", css_hide);
  63. tooltip.appendChild(document.createTextNode("This is likely a dynamic page."));
  64.  
  65. var hintHoverable = document.createElement("sup");
  66. hintHoverable.setAttribute("style", "margin-left: 3px;cursor: help;");
  67. hintHoverable.appendChild(document.createTextNode("?"));
  68. hintHoverable.appendChild(tooltip);
  69.  
  70. hintHoverable.addEventListener("mouseenter", function(event) {
  71. var tooltip = document.getElementById("now-hint-tooltip");
  72. tooltip.setAttribute("style", css_show);
  73. });
  74.  
  75. hintHoverable.addEventListener("mouseleave", function(event) {
  76. var tooltip = document.getElementById("now-hint-tooltip");
  77. tooltip.setAttribute("style", css_hide);
  78. });
  79.  
  80. container.appendChild(hintHoverable);
  81. }
  82.  
  83. var sitemap_loaded = false;
  84.  
  85. function addSitemapXml(container) {
  86. var getUrl = window.location;
  87. var sitemapUrl = getUrl.protocol + "//" + getUrl.host + "/sitemap.xml";
  88.  
  89. var sitemap_container = document.createElement("div");
  90. animateSitemapLoad(sitemap_container, 1, 0);
  91. container.append(sitemap_container);
  92.  
  93. checkIfSitemapExists(
  94. sitemapUrl,
  95. function() {
  96. addSitemapXmlSuccess(sitemap_container, sitemapUrl);
  97. },
  98. function() {
  99. addSitemapXmlFailure(sitemap_container);
  100. });
  101. }
  102.  
  103. function addSitemapXmlSuccess(container, url) {
  104. var sitemap_link = document.createElement("a");
  105. sitemap_link.setAttribute("href", url);
  106. sitemap_link.setAttribute("target", "_blank");
  107. sitemap_link.setAttribute("style", "color: green!important;text-decoration: underline;");
  108.  
  109. var sitemap_text = document.createTextNode("Sitemap");
  110. sitemap_link.appendChild(sitemap_text);
  111.  
  112. container.innerHTML = '';
  113. container.appendChild(sitemap_link);
  114. }
  115.  
  116. function addSitemapXmlFailure(container) {
  117. var sitemap_link = document.createElement("a");
  118. sitemap_link.setAttribute("style", "color: red!important;text-decoration: underline;text-decoration-style: dotted;");
  119.  
  120. var sitemap_text = document.createTextNode("Sitemap not found");
  121. sitemap_link.appendChild(sitemap_text);
  122.  
  123. container.innerHTML = '';
  124. container.appendChild(sitemap_link);
  125. }
  126.  
  127. function animateSitemapLoad(sitemap_container, direction, iteration) {
  128. sitemap_container.innerHTML = "";
  129. sitemap_container.appendChild(
  130. document.createTextNode(
  131. getLoadingMessageForFrame(iteration)));
  132. sitemap_container.setAttribute("style", "color: darkgrey!important;");
  133.  
  134. iteration += direction;
  135. if (iteration === 3) {
  136. direction = -1;
  137. } else if (iteration === 0) {
  138. direction = 1;
  139. }
  140.  
  141. setTimeout(function () {
  142. if (!sitemap_loaded) {
  143. animateSitemapLoad(sitemap_container, direction, iteration);
  144. }
  145. }, 250);
  146. }
  147.  
  148. function getLoadingMessageForFrame(numDots) {
  149. var msg = "Loading";
  150.  
  151. for (var i = 0; i < numDots; i++) {
  152. msg += ".";
  153. }
  154.  
  155. return msg;
  156. }
  157.  
  158. function checkIfSitemapExists(url, success, failure) {
  159. var xhttp = new XMLHttpRequest();
  160. xhttp.onreadystatechange = function() {
  161. if (this.readyState == 4 && (this.status == 200 || this.status == 302)) {
  162. sitemap_loaded = true;
  163. success();
  164. } else if (this.readyState == 4) {
  165. sitemap_loaded = true;
  166. failure();
  167. }
  168. };
  169. xhttp.open("GET", url, true);
  170. xhttp.send();
  171. }