Greasy Fork 还支持 简体中文。

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.1
  5. // @description Shows when the document was last modified
  6. // @author You
  7. // @match *://*/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. buildAndAddModificationDisplay();
  15. })();
  16.  
  17. function buildAndAddModificationDisplay() {
  18. var modification_display = createModificationContainer();
  19.  
  20. addLastModified(modification_display);
  21. addSitemapXml(modification_display);
  22.  
  23. var body = document.getElementsByTagName("BODY")[0];
  24. body.appendChild(modification_display);
  25. }
  26.  
  27. function createModificationContainer() {
  28. var modification_display = document.createElement("div");
  29. modification_display.setAttribute("class", "modification-container");
  30. var css = "position: fixed;bottom: -3px;left: -3px;padding: 5px;border: solid grey;border-radius: 3px;background-color: lightgray;z-index:1001;";
  31. modification_display.setAttribute("style", css);
  32.  
  33. return modification_display;
  34. }
  35.  
  36. function addLastModified(container) {
  37. var last_modified_container = document.createElement("div");
  38. var last_modified_content = document.createTextNode(document.lastModified);
  39. last_modified_container.appendChild(last_modified_content);
  40. container.appendChild(last_modified_container);
  41. }
  42.  
  43. var sitemap_loaded = false;
  44.  
  45. function addSitemapXml(container) {
  46. var getUrl = window.location;
  47. var sitemapUrl = getUrl.protocol + "//" + getUrl.host + "/sitemap.xml";
  48.  
  49. var sitemap_container = document.createElement("div");
  50. animateSitemapLoad(sitemap_container, 1, 0);
  51. container.append(sitemap_container);
  52.  
  53. checkIfSitemapExists(
  54. sitemapUrl,
  55. function() {
  56. var sitemap_link = document.createElement("a");
  57. sitemap_link.setAttribute("href", sitemapUrl);
  58. sitemap_link.setAttribute("target", "_blank");
  59. sitemap_link.setAttribute("style", "color: green;text-decoration: underline;");
  60.  
  61. var sitemap_text = document.createTextNode("Sitemap");
  62. sitemap_link.appendChild(sitemap_text);
  63.  
  64. sitemap_container.innerHTML = '';
  65. sitemap_container.appendChild(sitemap_link);
  66. },
  67. function() {
  68. var sitemap_link = document.createElement("a");
  69. sitemap_link.setAttribute("style", "color: red;text-decoration: underline;text-decoration-style: dotted;");
  70.  
  71. var sitemap_text = document.createTextNode("Sitemap not found");
  72. sitemap_link.appendChild(sitemap_text);
  73.  
  74. sitemap_container.innerHTML = '';
  75. sitemap_container.appendChild(sitemap_link);
  76. });
  77. }
  78.  
  79. function animateSitemapLoad(sitemap_container, direction, iteration) {
  80. sitemap_container.innerHTML = "";
  81. sitemap_container.appendChild(
  82. document.createTextNode(
  83. getLoadingMessageForFrame(iteration)));
  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. }