infobar.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Copyright 2010-2019 Gildas Lormeau
  3. * contact : gildas.lormeau <at> gmail.com
  4. *
  5. * This file is part of SingleFile.
  6. *
  7. * The code in this file is free software: you can redistribute it and/or
  8. * modify it under the terms of the GNU Affero General Public License
  9. * (GNU AGPL) as published by the Free Software Foundation, either version 3
  10. * of the License, or (at your option) any later version.
  11. *
  12. * The code in this file 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 GNU Affero
  15. * General Public License for more details.
  16. *
  17. * As additional permission under GNU AGPL version 3 section 7, you may
  18. * distribute UNMODIFIED VERSIONS OF THIS file without the copy of the GNU
  19. * AGPL normally required by section 4, provided you include this license
  20. * notice and a URL through which recipients can access the Corresponding
  21. * Source.
  22. */
  23. /* global browser, document, Node, window, top, getComputedStyle, location, setTimeout */
  24. this.singlefile.infobar = this.singlefile.infobar || (() => {
  25. const INFOBAR_TAGNAME = "singlefile-infobar";
  26. const LINK_ICON = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABIAAAASCAYAAABWzo5XAAAABmJLR0QABQDuAACS38mlAAAACXBIWXMAACfuAAAn7gExzuVDAAAAB3RJTUUH4ggCDDcMnYqGGAAAATtJREFUOMvNk19LwlAYxp+zhOoqpxJ1la3patFVINk/oRDBLuyreiPFMmcj/QQRSOOwpEINDCpwRr7d1HBMc4sufO7Oe877e5/zcA4wbWLDi8urGr2+vXsOFfJZdnPboDtuueoRcQEH6RQDgNBP8bxcpfvmA0QxPHF6u/MMInLVHFDP7kMUwyjks2xU8+ZGkgGAbtSp1e5gRhBc+0KQHHSjTg2TY0tVEItF/wYqV6+pYXKoiox0atvjOuQXYnILqiJj/ztceXUlGEirGGRyC0pCciDDmfm6mlYxiFtNKAkJmb0dV2OxpFGxpNFE0NmFTtxqQpbiHsgojQX1bBuyFMfR4S7zk+PYjE5PcizI0xD+6685jubnZvH41MJwgL+p233B8tKiF7SeXMPnYIB+/8OXg2hERO44wzC1+gJYGGpVbtoqiAAAAABJRU5ErkJggg==";
  27. 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==";
  28. const SINGLEFILE_COMMENT = "SingleFile";
  29. if (window == top && location && location.href && location.href.startsWith("file:///")) {
  30. if (document.readyState == "loading") {
  31. document.addEventListener("DOMContentLoaded", displayIcon, false);
  32. } else {
  33. displayIcon();
  34. }
  35. }
  36. return true;
  37. async function displayIcon() {
  38. let singleFileComment = document.documentElement.childNodes[0];
  39. if (!isSingleFileComment(singleFileComment)) {
  40. singleFileComment = findSingleFileComment();
  41. }
  42. if (singleFileComment) {
  43. const info = singleFileComment.textContent.split("\n");
  44. const [, , url, saveDate, ...infoData] = info;
  45. const options = await browser.runtime.sendMessage({ method: "tabs.getOptions", url });
  46. if (options.displayInfobar) {
  47. initInfobar(url, saveDate, infoData);
  48. }
  49. }
  50. }
  51. function findSingleFileComment(node = document.documentElement) {
  52. return node.childNodes && node.childNodes.length ? Array.from(node.childNodes).find(findSingleFileComment) : isSingleFileComment(node);
  53. }
  54. function isSingleFileComment(node) {
  55. return node.nodeType == Node.COMMENT_NODE && node.textContent.includes(SINGLEFILE_COMMENT);
  56. }
  57. function initInfobar(url, saveDate, infoData) {
  58. let infobarElement = document.querySelector(INFOBAR_TAGNAME);
  59. if (!infobarElement) {
  60. url = url.split("url: ")[1];
  61. saveDate = saveDate.split("saved date: ")[1];
  62. if (infoData && infoData.length > 1) {
  63. let content = infoData[0].split("info: ")[1].trim();
  64. for (let indexLine = 1; indexLine < infoData.length - 1; indexLine++) {
  65. content += "\n" + infoData[indexLine].trim();
  66. }
  67. infoData = content.trim();
  68. } else {
  69. infoData = saveDate;
  70. }
  71. infobarElement = createElement(INFOBAR_TAGNAME, document.body);
  72. infobarElement.style.setProperty("background-color", "#f9f9f9", "important");
  73. infobarElement.style.setProperty("display", "flex", "important");
  74. infobarElement.style.setProperty("position", "fixed", "important");
  75. infobarElement.style.setProperty("top", "16px", "important");
  76. infobarElement.style.setProperty("right", "16px", "important");
  77. infobarElement.style.setProperty("height", "auto", "important");
  78. infobarElement.style.setProperty("min-height", "24px", "important");
  79. infobarElement.style.setProperty("min-width", "24px", "important");
  80. infobarElement.style.setProperty("background-position", "center", "important");
  81. infobarElement.style.setProperty("background-repeat", "no-repeat", "important");
  82. infobarElement.style.setProperty("z-index", 2147483647, "important");
  83. infobarElement.style.setProperty("text-align", "center", "important");
  84. infobarElement.style.setProperty("will-change", "opacity, padding-left, padding-right, width, background-color, color", "important");
  85. infobarElement.style.setProperty("margin", "0 0 0 16px", "important");
  86. const infoElement = createElement("span", infobarElement);
  87. infoElement.style.setProperty("font-family", "Arial", "important");
  88. infoElement.style.setProperty("color", "#9aa0a6", "important");
  89. infoElement.style.setProperty("font-size", "14px", "important");
  90. infoElement.style.setProperty("line-height", "24px", "important");
  91. infoElement.style.setProperty("word-break", "break-word", "important");
  92. infoElement.style.setProperty("white-space", "pre-wrap", "important");
  93. infoElement.textContent = infoData;
  94. const linkElement = createElement("a", infobarElement);
  95. linkElement.style.setProperty("display", "inline-block", "important");
  96. linkElement.style.setProperty("padding-left", "8px", "important");
  97. linkElement.style.setProperty("line-height", "11px", "important");
  98. linkElement.style.setProperty("cursor", "pointer", "important");
  99. linkElement.style.setProperty("user-select", "none", "important");
  100. linkElement.target = "_blank";
  101. linkElement.rel = "noopener noreferrer";
  102. linkElement.title = "Open source URL: " + url;
  103. linkElement.href = url;
  104. const imgElement = createElement("img", linkElement);
  105. imgElement.style.setProperty("padding-top", "4px", "important");
  106. imgElement.style.setProperty("padding-bottom", "2px", "important");
  107. imgElement.style.setProperty("-webkit-padding-after", "2px", "important");
  108. imgElement.style.setProperty("padding-left", "2px", "important");
  109. imgElement.style.setProperty("-webkit-padding-start", "2px", "important");
  110. imgElement.style.setProperty("cursor", "pointer", "important");
  111. infobarElement.style.setProperty("text-align", "right", "important");
  112. imgElement.src = LINK_ICON;
  113. hideInfobar(infobarElement, linkElement, infoElement);
  114. infobarElement.onmouseover = () => infobarElement.style.setProperty("opacity", 1, "important");
  115. document.addEventListener("click", event => {
  116. if (event.button === 0) {
  117. let element = event.target;
  118. while (element && element != infobarElement) {
  119. element = element.parentElement;
  120. }
  121. if (element != infobarElement) {
  122. hideInfobar(infobarElement, linkElement, infoElement);
  123. }
  124. }
  125. });
  126. setTimeout(() => {
  127. infobarElement.style.setProperty("transition-property", "opacity", "important");
  128. infobarElement.style.setProperty("transition-duration", "250ms", "important");
  129. });
  130. }
  131. }
  132. function displayInfobar(infobarElement, linkElement, infoElement) {
  133. infobarElement.style.setProperty("font-size", "13px", "important");
  134. infobarElement.style.setProperty("opacity", 1, "important");
  135. infobarElement.style.setProperty("width", "auto", "important");
  136. infobarElement.style.setProperty("background-color", "#f9f9f9", "important");
  137. infobarElement.style.setProperty("cursor", "auto", "important");
  138. infobarElement.style.setProperty("color", "#9aa0a6", "important");
  139. infobarElement.style.setProperty("padding-left", "8px", "important");
  140. infobarElement.style.setProperty("padding-right", "4px", "important");
  141. infobarElement.style.setProperty("padding-bottom", "2px", "important");
  142. infobarElement.style.setProperty("-webkit-padding-start", "8px", "important");
  143. infobarElement.style.setProperty("-webkit-padding-end", "4px", "important");
  144. infobarElement.style.setProperty("border", "2px solid #555", "important");
  145. infobarElement.style.setProperty("-webkit-border-start", "2px solid #555", "important");
  146. infobarElement.style.setProperty("-webkit-border-before", "2px solid #555", "important");
  147. infobarElement.style.setProperty("-webkit-border-end", "2px solid #555", "important");
  148. infobarElement.style.setProperty("-webkit-border-after", "2px solid #555", "important");
  149. infobarElement.style.setProperty("background-image", "none");
  150. infobarElement.style.setProperty("border-radius", "8px", "important");
  151. infoElement.style.setProperty("display", "inline-block", "important");
  152. linkElement.style.setProperty("display", "inline-block", "important");
  153. infobarElement.style.setProperty("user-select", "initial", "important");
  154. infobarElement.style.setProperty("-moz-user-select", "initial", "important");
  155. infobarElement.onclick = null;
  156. infobarElement.onmouseout = null;
  157. }
  158. function hideInfobar(infobarElement, linkElement, infoElement) {
  159. infobarElement.style.setProperty("user-select", "none", "important");
  160. infobarElement.style.setProperty("-moz-user-select", "none", "important");
  161. infobarElement.style.setProperty("opacity", .7);
  162. infobarElement.onmouseout = () => infobarElement.style.setProperty("opacity", .7);
  163. infobarElement.style.setProperty("width", "24px", "important");
  164. infobarElement.style.setProperty("background-color", "#737373", "important");
  165. infobarElement.style.setProperty("cursor", "pointer", "important");
  166. infobarElement.style.setProperty("color", "white", "important");
  167. infobarElement.style.setProperty("padding-left", 0, "important");
  168. infobarElement.style.setProperty("padding-right", 0, "important");
  169. infobarElement.style.setProperty("-webkit-padding-start", 0, "important");
  170. infobarElement.style.setProperty("-webkit-padding-end", 0, "important");
  171. infobarElement.style.setProperty("border", "2px solid #eee", "important");
  172. infobarElement.style.setProperty("-webkit-border-start", "2px solid #eee", "important");
  173. infobarElement.style.setProperty("-webkit-border-before", "2px solid #eee", "important");
  174. infobarElement.style.setProperty("-webkit-border-end", "2px solid #eee", "important");
  175. infobarElement.style.setProperty("-webkit-border-after", "2px solid #eee", "important");
  176. infobarElement.style.setProperty("background-image", "url(" + IMAGE_ICON + ")");
  177. infobarElement.style.setProperty("background-size", "70% 70%", "important");
  178. infobarElement.style.setProperty("border-radius", "16px", "important");
  179. linkElement.style.setProperty("display", "none", "important");
  180. infoElement.style.setProperty("display", "none", "important");
  181. infobarElement.onclick = event => {
  182. if (event.button === 0) {
  183. displayInfobar(infobarElement, linkElement, infoElement);
  184. return false;
  185. }
  186. };
  187. }
  188. function createElement(tagName, parentElement) {
  189. const element = document.createElement(tagName);
  190. parentElement.appendChild(element);
  191. Array.from(getComputedStyle(element)).forEach(property => element.style.setProperty(property, "initial", "important"));
  192. return element;
  193. }
  194. })();