Document Modification Display

Shows when the document was last modified

目前為 2020-11-26 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name Document Modification Display
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.3
  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;";
  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_modified_content = document.createTextNode(document.lastModified);
  38. last_modified_container.appendChild(last_modified_content);
  39. container.appendChild(last_modified_container);
  40. }
  41.  
  42. var sitemap_loaded = false;
  43.  
  44. function addSitemapXml(container) {
  45. var getUrl = window.location;
  46. var sitemapUrl = getUrl.protocol + "//" + getUrl.host + "/sitemap.xml";
  47.  
  48. var sitemap_container = document.createElement("div");
  49. animateSitemapLoad(sitemap_container, 1, 0);
  50. container.append(sitemap_container);
  51.  
  52. checkIfSitemapExists(
  53. sitemapUrl,
  54. function() {
  55. var sitemap_link = document.createElement("a");
  56. sitemap_link.setAttribute("href", sitemapUrl);
  57. sitemap_link.setAttribute("target", "_blank");
  58. sitemap_link.setAttribute("style", "color: green!important;text-decoration: underline;");
  59.  
  60. var sitemap_text = document.createTextNode("Sitemap");
  61. sitemap_link.appendChild(sitemap_text);
  62.  
  63. sitemap_container.innerHTML = '';
  64. sitemap_container.appendChild(sitemap_link);
  65. },
  66. function() {
  67. var sitemap_link = document.createElement("a");
  68. sitemap_link.setAttribute("style", "color: red!important;text-decoration: underline;text-decoration-style: dotted;");
  69.  
  70. var sitemap_text = document.createTextNode("Sitemap not found");
  71. sitemap_link.appendChild(sitemap_text);
  72.  
  73. sitemap_container.innerHTML = '';
  74. sitemap_container.appendChild(sitemap_link);
  75. });
  76. }
  77.  
  78. function animateSitemapLoad(sitemap_container, direction, iteration) {
  79. sitemap_container.innerHTML = "";
  80. sitemap_container.appendChild(
  81. document.createTextNode(
  82. getLoadingMessageForFrame(iteration)));
  83. sitemap_container.setAttribute("style", "color: darkgrey!important;");
  84.  
  85. iteration += direction;
  86. if (iteration === 3) {
  87. direction = -1;
  88. } else if (iteration === 0) {
  89. direction = 1;
  90. }
  91.  
  92. setTimeout(function () {
  93. if (!sitemap_loaded) {
  94. animateSitemapLoad(sitemap_container, direction, iteration);
  95. }
  96. }, 250);
  97. }
  98.  
  99. function getLoadingMessageForFrame(numDots) {
  100. var msg = "Loading";
  101.  
  102. for (var i = 0; i < numDots; i++) {
  103. msg += ".";
  104. }
  105.  
  106. return msg;
  107. }
  108.  
  109. function checkIfSitemapExists(url, success, failure) {
  110. var xhttp = new XMLHttpRequest();
  111. xhttp.onreadystatechange = function() {
  112. if (this.readyState == 4 && (this.status == 200 || this.status == 302)) {
  113. sitemap_loaded = true;
  114. success();
  115. } else if (this.readyState == 4) {
  116. sitemap_loaded = true;
  117. failure();
  118. }
  119. };
  120. xhttp.open("GET", url, true);
  121. xhttp.send();
  122. }