content-infobar-web.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Copyright 2010-2020 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 document, Node, window, top, getComputedStyle, XPathResult */
  24. (() => {
  25. const INFOBAR_TAGNAME = "singlefile-infobar";
  26. const LINK_ICON = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABAAgMAAADXB5lNAAABhmlDQ1BJQ0MgcHJvZmlsZQAAKJF9kj1Iw0AYht+mSkUrDnYQcchQnSyIijqWKhbBQmkrtOpgcukfNGlIUlwcBdeCgz+LVQcXZ10dXAVB8AfEydFJ0UVK/C4ptIjx4LiH9+59+e67A4RGhalm1wSgapaRisfEbG5VDLyiDwEAvZiVmKkn0osZeI6ve/j4ehfhWd7n/hz9St5kgE8kjjLdsIg3iGc2LZ3zPnGIlSSF+Jx43KACiR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P/gb/O6tWZiadJOCMaD7xbY/RoHALtCs2/b3sW03TwD/M3Cltf3VBjD3SXq9rYWPgIFt4OK6rcl7wOUOMPSkS4bkSH6aQqEAvJ/RM+WAwVv6EGtu31r7OH0AMtSr5Rvg4BAYK1L2use9ezr79u+ZVv9+AFlNcp0UUpiqAAAACXBIWXMAAC4jAAAuIwF4pT92AAAAB3RJTUUH5AsHADIRLMaOHwAAABl0RVh0Q29tbWVudABDcmVhdGVkIHdpdGggR0lNUFeBDhcAAAAPUExURQAAAIqKioyNjY2OjvDw8L2y1DEAAAABdFJOUwBA5thmAAAAAWJLR0QB/wIt3gAAAGNJREFUSMdjYCAJsLi4OBCQx6/CBQwIGIDPCBcXAkYQUsACU+AwlBVQHg6Eg5pgZBGOboIJZugDFwRwoJECJCUOhJI1wZwzqmBUwagCuipgIqTABG9h7YIKaKGAURAFEF/6AQAO4HqSoDP8bgAAAABJRU5ErkJggg==";
  29. const SINGLEFILE_COMMENT = "SingleFile";
  30. const SINGLE_FILE_UI_ELEMENT_CLASS = "single-file-ui-element";
  31. const browser = this.browser;
  32. if (window == top) {
  33. if (document.readyState == "loading") {
  34. document.addEventListener("DOMContentLoaded", displayIcon, false);
  35. } else {
  36. displayIcon();
  37. }
  38. }
  39. async function displayIcon() {
  40. const result = document.evaluate("//comment()", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
  41. let singleFileComment = result && result.singleNodeValue;
  42. if (singleFileComment && isSingleFileComment(singleFileComment)) {
  43. const info = singleFileComment.textContent.split("\n");
  44. const [, , url, saveDate, ...infoData] = info;
  45. if (url && saveDate) {
  46. let options;
  47. if (browser && browser.runtime && browser.runtime.sendMessage) {
  48. options = await browser.runtime.sendMessage({ method: "tabs.getOptions", url });
  49. } else {
  50. options = { displayInfobar: true };
  51. }
  52. if (options.displayInfobar) {
  53. initInfobar(url, saveDate, infoData);
  54. }
  55. }
  56. }
  57. }
  58. function isSingleFileComment(node) {
  59. return node.nodeType == Node.COMMENT_NODE && node.textContent.includes(SINGLEFILE_COMMENT);
  60. }
  61. function initInfobar(url, saveDate, infoData) {
  62. let infobarElement = document.querySelector(INFOBAR_TAGNAME);
  63. if (!infobarElement) {
  64. url = url.split("url: ")[1];
  65. saveDate = saveDate.split("saved date: ")[1];
  66. if (infoData && infoData.length > 1) {
  67. let content = infoData[0].split("info: ")[1].trim();
  68. for (let indexLine = 1; indexLine < infoData.length - 1; indexLine++) {
  69. content += "\n" + infoData[indexLine].trim();
  70. }
  71. infoData = content.trim();
  72. } else {
  73. infoData = saveDate;
  74. }
  75. infobarElement = createElement(INFOBAR_TAGNAME, document.body);
  76. infobarElement.className = SINGLE_FILE_UI_ELEMENT_CLASS;
  77. const shadowRoot = infobarElement.attachShadow({ mode: "open" });
  78. const styleElement = document.createElement("style");
  79. styleElement.textContent = `
  80. .infobar {
  81. background-color: #737373;
  82. color: white;
  83. display: flex;
  84. position: fixed;
  85. top: 16px;
  86. right: 16px;
  87. height: auto;
  88. width: auto;
  89. min-height: 24px;
  90. min-width: 24px;
  91. background-position: center;
  92. background-repeat: no-repeat;
  93. z-index: 2147483647;
  94. text-align: center;
  95. margin: 0 0 0 16px;
  96. background-image: url(${IMAGE_ICON});
  97. border-radius: 16px;
  98. user-select: none;
  99. -moz-user-select: none;
  100. opacity: .7;
  101. cursor: pointer;
  102. padding-left: 0;
  103. padding-right: 0;
  104. padding-top: 0;
  105. padding-bottom: 0;
  106. border: 2px solid #eee;
  107. background-size: 70% 70%;
  108. transition: all 250ms;
  109. font-size: 13px;
  110. }
  111. .infobar:hover {
  112. opacity: 1;
  113. }
  114. .infobar-open {
  115. opacity: 1;
  116. background-color: #f9f9f9;
  117. cursor: auto;
  118. color: #2d2d2d;
  119. padding-top: 2px;
  120. padding-bottom: 2px;
  121. border: 2px solid #878787;
  122. background-image: none;
  123. border-radius: 8px;
  124. user-select: initial;
  125. -moz-user-select: initial;
  126. }
  127. .infobar-close-button {
  128. display: none;
  129. opacity: .7;
  130. padding-top: 4px;
  131. padding-left: 8px;
  132. padding-right: 8px;
  133. cursor: pointer;
  134. transition: opacity 250ms;
  135. height: 16px;
  136. }
  137. .infobar-close-button:hover {
  138. opacity: 1;
  139. }
  140. .infobar-content {
  141. display: none;
  142. font-family: Arial;
  143. font-size: 14px;
  144. line-height: 22px;
  145. word-break: break-word;
  146. white-space: pre-wrap;
  147. position: relative;
  148. top: 1px;
  149. }
  150. .infobar-link {
  151. display: none;
  152. padding-left: 8px;
  153. padding-right: 8px;
  154. line-height: 11px;
  155. cursor: pointer;
  156. user-select: none;
  157. }
  158. .infobar-link-icon {
  159. padding-top: 4px;
  160. padding-left: 2px;
  161. cursor: pointer;
  162. opacity: .7;
  163. transition: opacity 250ms;
  164. height: 16px;
  165. }
  166. .infobar-link-icon:hover {
  167. opacity: 1;
  168. }
  169. .infobar-open .infobar-close-button, .infobar-open .infobar-content, .infobar-open .infobar-link {
  170. display: inline-block;
  171. }
  172. `;
  173. shadowRoot.appendChild(styleElement);
  174. const infobarContent = document.createElement("div");
  175. infobarContent.classList.add("infobar");
  176. shadowRoot.appendChild(infobarContent);
  177. const closeElement = document.createElement("img");
  178. closeElement.classList.add("infobar-close-button");
  179. infobarContent.appendChild(closeElement);
  180. closeElement.src = CLOSE_ICON;
  181. closeElement.onclick = event => {
  182. if (event.button === 0) {
  183. infobarElement.remove();
  184. }
  185. };
  186. const infoElement = document.createElement("span");
  187. infobarContent.appendChild(infoElement);
  188. infoElement.classList.add("infobar-content");
  189. infoElement.textContent = infoData;
  190. const linkElement = document.createElement("a");
  191. linkElement.classList.add("infobar-link");
  192. infobarContent.appendChild(linkElement);
  193. linkElement.target = "_blank";
  194. linkElement.rel = "noopener noreferrer";
  195. linkElement.title = "Open source URL: " + url;
  196. linkElement.href = url;
  197. const imgElement = document.createElement("img");
  198. imgElement.classList.add("infobar-link-icon");
  199. linkElement.appendChild(imgElement);
  200. imgElement.src = LINK_ICON;
  201. hideInfobar(infobarContent);
  202. document.addEventListener("click", event => {
  203. if (event.button === 0) {
  204. let element = event.target;
  205. while (element && element != infobarElement) {
  206. element = element.parentElement;
  207. }
  208. if (element != infobarElement) {
  209. hideInfobar(infobarContent);
  210. }
  211. }
  212. });
  213. }
  214. }
  215. function displayInfobar(infobarContent) {
  216. infobarContent.classList.add("infobar-open");
  217. infobarContent.onclick = null;
  218. infobarContent.onmouseout = null;
  219. }
  220. function hideInfobar(infobarContent) {
  221. infobarContent.classList.remove("infobar-open");
  222. infobarContent.onclick = event => {
  223. if (event.button === 0) {
  224. displayInfobar(infobarContent);
  225. return false;
  226. }
  227. };
  228. }
  229. function createElement(tagName, parentElement) {
  230. const element = document.createElement(tagName);
  231. parentElement.appendChild(element);
  232. Array.from(getComputedStyle(element)).forEach(property => element.style.setProperty(property, "initial", "important"));
  233. return element;
  234. }
  235. })();