infobar.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 */
  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 SINGLEFILE_COMMENT = "Archive processed by SingleFile";
  25. if (window == top) {
  26. if (document.readyState == "loading") {
  27. document.addEventListener("DOMContentLoaded", displayIcon, false);
  28. } else {
  29. displayIcon();
  30. }
  31. }
  32. return true;
  33. async function displayIcon() {
  34. let singleFileComment = document.documentElement.childNodes[0];
  35. if (!isSingleFileComment(singleFileComment)) {
  36. singleFileComment = findSingleFileComment();
  37. }
  38. if (singleFileComment) {
  39. const info = singleFileComment.textContent.split("\n");
  40. const [, , url, saveDate] = info;
  41. const config = await browser.runtime.sendMessage({ getConfig: true });
  42. if (config.displayInfobar) {
  43. initInfobar(url, saveDate);
  44. }
  45. }
  46. }
  47. function findSingleFileComment(node = document.documentElement) {
  48. return node.childNodes && node.childNodes.length ? Array.from(node.childNodes).find(findSingleFileComment) : isSingleFileComment(node);
  49. }
  50. function isSingleFileComment(node) {
  51. return node.nodeType == Node.COMMENT_NODE && node.textContent.includes(SINGLEFILE_COMMENT);
  52. }
  53. function initInfobar(url, saveDate) {
  54. let infobarElement = document.querySelector(INFOBAR_TAGNAME);
  55. if (!infobarElement) {
  56. infobarElement = createElement(INFOBAR_TAGNAME, document.body);
  57. infobarElement.style.setProperty("background-color", "#f9f9f9", "important");
  58. infobarElement.style.setProperty("color", "#9aa0a6", "important");
  59. infobarElement.style.setProperty("display", "block", "important");
  60. infobarElement.style.setProperty("position", "fixed", "important");
  61. infobarElement.style.setProperty("top", "16px", "important");
  62. infobarElement.style.setProperty("right", "16px", "important");
  63. infobarElement.style.setProperty("height", "auto", "important");
  64. infobarElement.style.setProperty("line-height", "28px", "important");
  65. infobarElement.style.setProperty("border-radius", "16px", "important");
  66. infobarElement.style.setProperty("border", "2px solid #737373", "important");
  67. infobarElement.style.setProperty("-webkit-border-start", "2px solid #737373", "important");
  68. infobarElement.style.setProperty("-webkit-border-before", "2px solid #737373", "important");
  69. infobarElement.style.setProperty("-webkit-border-end", "2px solid #737373", "important");
  70. infobarElement.style.setProperty("-webkit-border-after", "2px solid #737373", "important");
  71. infobarElement.style.setProperty("z-index", 2147483647, "important");
  72. infobarElement.style.setProperty("text-align", "center", "important");
  73. infobarElement.style.setProperty("font-family", "Arial", "important");
  74. infobarElement.style.setProperty("will-change", "opacity, padding-left, padding-right, width, background-color, color", "important");
  75. const linkElement = createElement("a", infobarElement);
  76. linkElement.style.setProperty("display", "inline-block", "important");
  77. linkElement.style.setProperty("padding-left", "8px", "important");
  78. linkElement.style.setProperty("line-height", "28px", "important");
  79. linkElement.style.setProperty("cursor", "pointer", "important");
  80. linkElement.target = "_blank";
  81. linkElement.rel = "noopener noreferrer";
  82. linkElement.title = "Open original page";
  83. linkElement.href = url.split("url: ")[1];
  84. const imgElement = createElement("img", linkElement);
  85. imgElement.style.setProperty("vertical-align", "middle", "important");
  86. imgElement.style.setProperty("padding-bottom", "2px", "important");
  87. imgElement.style.setProperty("-webkit-padding-after", "2px", "important");
  88. imgElement.style.setProperty("padding-left", "2px", "important");
  89. imgElement.style.setProperty("-webkit-padding-start", "2px", "important");
  90. imgElement.style.setProperty("cursor", "pointer", "important");
  91. imgElement.src = LINK_ICON;
  92. hideInfobar(infobarElement, linkElement, saveDate);
  93. infobarElement.onmouseover = () => infobarElement.style.setProperty("opacity", 1, "important");
  94. document.addEventListener("click", event => {
  95. if (event.button === 0) {
  96. let element = event.target;
  97. while (element && element != infobarElement) {
  98. element = element.parentElement;
  99. }
  100. if (element != infobarElement) {
  101. hideInfobar(infobarElement, linkElement, saveDate);
  102. }
  103. }
  104. });
  105. setTimeout(() => {
  106. infobarElement.style.setProperty("transition-property", "opacity, padding-left, padding-right, width, background-color, color", "important");
  107. infobarElement.style.setProperty("transition-duration", "250ms", "important");
  108. });
  109. }
  110. }
  111. function displayInfobar(infobarElement, linkElement, saveDate) {
  112. infobarElement.style.setProperty("font-size", "15px", "important");
  113. infobarElement.style.setProperty("opacity", 1, "important");
  114. infobarElement.style.setProperty("width", "auto", "important");
  115. infobarElement.style.setProperty("background-color", "#f9f9f9", "important");
  116. infobarElement.style.setProperty("cursor", "auto", "important");
  117. infobarElement.style.setProperty("color", "#9aa0a6", "important");
  118. infobarElement.style.setProperty("padding-left", "16px", "important");
  119. infobarElement.style.setProperty("padding-right", "16px", "important");
  120. infobarElement.style.setProperty("-webkit-padding-start", "16px", "important");
  121. infobarElement.style.setProperty("-webkit-padding-end", "16px", "important");
  122. infobarElement.textContent = saveDate.split("saved date: ")[1];
  123. infobarElement.appendChild(linkElement);
  124. infobarElement.onclick = null;
  125. infobarElement.onmouseout = null;
  126. }
  127. function hideInfobar(infobarElement, linkElement, saveDate) {
  128. infobarElement.style.opacity = .7;
  129. infobarElement.onmouseout = () => infobarElement.style.opacity = .7;
  130. infobarElement.style.setProperty("font-size", "24px", "important");
  131. infobarElement.style.setProperty("width", "28px", "important");
  132. infobarElement.style.setProperty("background-color", "#737373", "important");
  133. infobarElement.style.setProperty("cursor", "pointer", "important");
  134. infobarElement.style.setProperty("color", "white", "important");
  135. infobarElement.style.setProperty("padding-left", 0, "important");
  136. infobarElement.style.setProperty("padding-right", 0, "important");
  137. infobarElement.style.setProperty("-webkit-padding-start", 0, "important");
  138. infobarElement.style.setProperty("-webkit-padding-end", 0, "important");
  139. infobarElement.textContent = "ℹ";
  140. infobarElement.onclick = event => {
  141. if (event.button === 0) {
  142. displayInfobar(infobarElement, linkElement, saveDate);
  143. return false;
  144. }
  145. };
  146. }
  147. function createElement(tagName, parentElement) {
  148. const element = document.createElement(tagName);
  149. parentElement.appendChild(element);
  150. Array.from(getComputedStyle(element)).forEach(property => element.style.setProperty(property, "initial", "important"));
  151. return element;
  152. }
  153. })();