infobar.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright 2018 Gildas Lormeau
  3. * contact : gildas.lormeau <at> gmail.com
  4. *
  5. * This file is part of SingleFile.
  6. *
  7. * SingleFile is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * SingleFile is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with SingleFile. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. /* global browser, document, Node, window, top, getComputedStyle, location */
  21. this.singlefile.infobar = this.singlefile.infobar || (() => {
  22. const INFOBAR_TAGNAME = "singlefile-infobar";
  23. const LINK_ICON = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABIAAAASCAYAAABWzo5XAAAABmJLR0QABQDuAACS38mlAAAACXBIWXMAACfuAAAn7gExzuVDAAAAB3RJTUUH4ggCDDcMnYqGGAAAATtJREFUOMvNk19LwlAYxp+zhOoqpxJ1la3patFVINk/oRDBLuyreiPFMmcj/QQRSOOwpEINDCpwRr7d1HBMc4sufO7Oe877e5/zcA4wbWLDi8urGr2+vXsOFfJZdnPboDtuueoRcQEH6RQDgNBP8bxcpfvmA0QxPHF6u/MMInLVHFDP7kMUwyjks2xU8+ZGkgGAbtSp1e5gRhBc+0KQHHSjTg2TY0tVEItF/wYqV6+pYXKoiox0atvjOuQXYnILqiJj/ztceXUlGEirGGRyC0pCciDDmfm6mlYxiFtNKAkJmb0dV2OxpFGxpNFE0NmFTtxqQpbiHsgojQX1bBuyFMfR4S7zk+PYjE5PcizI0xD+6685jubnZvH41MJwgL+p233B8tKiF7SeXMPnYIB+/8OXg2hERO44wzC1+gJYGGpVbtoqiAAAAABJRU5ErkJggg==";
  24. const IMAGE_ICON = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAABIUlEQVQ4y+2TsarCMBSGvxTBRdqiUZAWOrhJB9EXcPKFfCvfQYfulUKHDqXg4CYUJSioYO4mSDX3ttzt3n87fMlHTpIjlsulxpDZbEYYhgghSNOUOI5Ny2mZYBAELBYLer0eAJ7ncTweKYri4x7LJJRS0u12n7XrukgpjSc0CpVSXK/XZ32/31FKNW85z3PW6zXT6RSAJEnIsqy5UGvNZrNhu90CcDqd+C6tT6J+v//2Th+PB2VZ1hN2Oh3G4zGTyQTbtl/YbrdjtVpxu91+Ljyfz0RRhG3bzOfzF+Y4TvNXvlwuaK2pE4tfzr/wzwsty0IIURlL0998KxRCMBqN8H2/wlzXJQxD2u12vVkeDoeUZUkURRU+GAw4HA7s9/sK+wK6CWHasQ/S/wAAAABJRU5ErkJggg==";
  25. const SINGLEFILE_COMMENT = "Archive processed by SingleFile";
  26. if (window == top && location && location.href && location.href.startsWith("file:///")) {
  27. if (document.readyState == "loading") {
  28. document.addEventListener("DOMContentLoaded", displayIcon, false);
  29. } else {
  30. displayIcon();
  31. }
  32. }
  33. return true;
  34. async function displayIcon() {
  35. let singleFileComment = document.documentElement.childNodes[0];
  36. if (!isSingleFileComment(singleFileComment)) {
  37. singleFileComment = findSingleFileComment();
  38. }
  39. if (singleFileComment) {
  40. const info = singleFileComment.textContent.split("\n");
  41. const [, , url, saveDate] = info;
  42. const config = await browser.runtime.sendMessage({ getConfig: true });
  43. if (config.displayInfobar) {
  44. initInfobar(url, saveDate);
  45. }
  46. }
  47. }
  48. function findSingleFileComment(node = document.documentElement) {
  49. return node.childNodes && node.childNodes.length ? Array.from(node.childNodes).find(findSingleFileComment) : isSingleFileComment(node);
  50. }
  51. function isSingleFileComment(node) {
  52. return node.nodeType == Node.COMMENT_NODE && node.textContent.includes(SINGLEFILE_COMMENT);
  53. }
  54. function initInfobar(url, saveDate) {
  55. let infobarElement = document.querySelector(INFOBAR_TAGNAME);
  56. if (!infobarElement) {
  57. url = url.split("url: ")[1];
  58. infobarElement = createElement(INFOBAR_TAGNAME, document.body);
  59. infobarElement.style.setProperty("background-color", "#f9f9f9", "important");
  60. infobarElement.style.setProperty("display", "block", "important");
  61. infobarElement.style.setProperty("position", "fixed", "important");
  62. infobarElement.style.setProperty("top", "16px", "important");
  63. infobarElement.style.setProperty("right", "16px", "important");
  64. infobarElement.style.setProperty("height", "auto", "important");
  65. infobarElement.style.setProperty("min-height", "28px", "important");
  66. infobarElement.style.setProperty("min-width", "28px", "important");
  67. infobarElement.style.setProperty("background-position", "center", "important");
  68. infobarElement.style.setProperty("background-repeat", "no-repeat", "important");
  69. infobarElement.style.setProperty("border-radius", "16px", "important");
  70. infobarElement.style.setProperty("z-index", 2147483647, "important");
  71. infobarElement.style.setProperty("text-align", "center", "important");
  72. infobarElement.style.setProperty("will-change", "opacity, padding-left, padding-right, width, background-color, color", "important");
  73. const infoElement = createElement("span", infobarElement);
  74. infoElement.style.setProperty("font-family", "Arial", "important");
  75. infoElement.style.setProperty("color", "#9aa0a6", "important");
  76. infoElement.style.setProperty("line-height", "28px", "important");
  77. infoElement.textContent = saveDate.split("saved date: ")[1];
  78. const linkElement = createElement("a", infobarElement);
  79. linkElement.style.setProperty("display", "inline-block", "important");
  80. linkElement.style.setProperty("padding-left", "8px", "important");
  81. linkElement.style.setProperty("line-height", "28px", "important");
  82. linkElement.style.setProperty("cursor", "pointer", "important");
  83. linkElement.style.setProperty("user-select", "none", "important");
  84. linkElement.target = "_blank";
  85. linkElement.rel = "noopener noreferrer";
  86. linkElement.title = "Open source URL: " + url;
  87. linkElement.href = url;
  88. const imgElement = createElement("img", linkElement);
  89. imgElement.style.setProperty("vertical-align", "middle", "important");
  90. imgElement.style.setProperty("padding-bottom", "2px", "important");
  91. imgElement.style.setProperty("-webkit-padding-after", "2px", "important");
  92. imgElement.style.setProperty("padding-left", "2px", "important");
  93. imgElement.style.setProperty("-webkit-padding-start", "2px", "important");
  94. imgElement.style.setProperty("cursor", "pointer", "important");
  95. imgElement.src = LINK_ICON;
  96. hideInfobar(infobarElement, linkElement, infoElement, saveDate);
  97. infobarElement.onmouseover = () => infobarElement.style.setProperty("opacity", 1, "important");
  98. document.addEventListener("click", event => {
  99. if (event.button === 0) {
  100. let element = event.target;
  101. while (element && element != infobarElement) {
  102. element = element.parentElement;
  103. }
  104. if (element != infobarElement) {
  105. hideInfobar(infobarElement, linkElement, infoElement, saveDate);
  106. }
  107. }
  108. });
  109. setTimeout(() => {
  110. infobarElement.style.setProperty("transition-property", "opacity", "important");
  111. infobarElement.style.setProperty("transition-duration", "250ms", "important");
  112. });
  113. }
  114. }
  115. function displayInfobar(infobarElement, linkElement, infoElement) {
  116. infobarElement.style.setProperty("font-size", "15px", "important");
  117. infobarElement.style.setProperty("opacity", 1, "important");
  118. infobarElement.style.setProperty("width", "auto", "important");
  119. infobarElement.style.setProperty("background-color", "#f9f9f9", "important");
  120. infobarElement.style.setProperty("cursor", "auto", "important");
  121. infobarElement.style.setProperty("color", "#9aa0a6", "important");
  122. infobarElement.style.setProperty("padding-left", "16px", "important");
  123. infobarElement.style.setProperty("padding-right", "16px", "important");
  124. infobarElement.style.setProperty("-webkit-padding-start", "16px", "important");
  125. infobarElement.style.setProperty("-webkit-padding-end", "16px", "important");
  126. infobarElement.style.setProperty("border", "2px solid #555", "important");
  127. infobarElement.style.setProperty("-webkit-border-start", "2px solid #555", "important");
  128. infobarElement.style.setProperty("-webkit-border-before", "2px solid #555", "important");
  129. infobarElement.style.setProperty("-webkit-border-end", "2px solid #555", "important");
  130. infobarElement.style.setProperty("-webkit-border-after", "2px solid #555", "important");
  131. infobarElement.style.setProperty("background-image", "none");
  132. infoElement.style.setProperty("display", "inline-block", "important");
  133. linkElement.style.setProperty("display", "inline-block", "important");
  134. infobarElement.onclick = null;
  135. infobarElement.onmouseout = null;
  136. }
  137. function hideInfobar(infobarElement, linkElement, infoElement) {
  138. infobarElement.style.opacity = .7;
  139. infobarElement.onmouseout = () => infobarElement.style.opacity = .7;
  140. infobarElement.style.setProperty("font-size", "24px", "important");
  141. infobarElement.style.setProperty("width", "28px", "important");
  142. infobarElement.style.setProperty("background-color", "#737373", "important");
  143. infobarElement.style.setProperty("cursor", "pointer", "important");
  144. infobarElement.style.setProperty("color", "white", "important");
  145. infobarElement.style.setProperty("padding-left", 0, "important");
  146. infobarElement.style.setProperty("padding-right", 0, "important");
  147. infobarElement.style.setProperty("-webkit-padding-start", 0, "important");
  148. infobarElement.style.setProperty("-webkit-padding-end", 0, "important");
  149. infobarElement.style.setProperty("border", "2px solid #eee", "important");
  150. infobarElement.style.setProperty("-webkit-border-start", "2px solid #eee", "important");
  151. infobarElement.style.setProperty("-webkit-border-before", "2px solid #eee", "important");
  152. infobarElement.style.setProperty("-webkit-border-end", "2px solid #eee", "important");
  153. infobarElement.style.setProperty("-webkit-border-after", "2px solid #eee", "important");
  154. infobarElement.style.setProperty("background-image", "url(" + IMAGE_ICON + ")");
  155. linkElement.style.setProperty("display", "none", "important");
  156. infoElement.style.setProperty("display", "none", "important");
  157. infobarElement.onclick = event => {
  158. if (event.button === 0) {
  159. displayInfobar(infobarElement, linkElement, infoElement);
  160. return false;
  161. }
  162. };
  163. }
  164. function createElement(tagName, parentElement) {
  165. const element = document.createElement(tagName);
  166. parentElement.appendChild(element);
  167. Array.from(getComputedStyle(element)).forEach(property => element.style.setProperty(property, "initial", "important"));
  168. return element;
  169. }
  170. })();