1
0

content-ui.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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, getComputedStyle, addEventListener, removeEventListener, requestAnimationFrame, setTimeout */
  21. this.singlefile.ui = this.singlefile.ui || (() => {
  22. const MASK_TAGNAME = "singlefile-mask";
  23. const PROGRESS_BAR_TAGNAME = "singlefile-progress-bar";
  24. const PROGRESS_CURSOR_TAGNAME = "singlefile-progress-cursor";
  25. const SELECTION_ZONE_TAGNAME = "single-file-selection-zone";
  26. const LOGS_WINDOW_TAGNAME = "singlefile-logs-window";
  27. const LOGS_LINE_TAGNAME = "singlefile-logs-line";
  28. const LOGS_LINE_ELEMENT_TAGNAME = "singlefile-logs-element";
  29. const SINGLE_FILE_UI_ELEMENT_CLASS = "single-file-ui-element";
  30. const SELECT_PX_THRESHOLD = 8;
  31. let selectedAreaElement;
  32. let logsWindowElement = createLogsWindowElement();
  33. const allProperties = new Set();
  34. Array.from(getComputedStyle(document.body)).forEach(property => allProperties.add(property));
  35. return {
  36. getSelectedArea,
  37. onStartPage() {
  38. let maskElement = document.querySelector(MASK_TAGNAME);
  39. if (!maskElement) {
  40. requestAnimationFrame(() => {
  41. const maskElement = createMaskElement();
  42. createProgressBarElement(maskElement);
  43. document.body.appendChild(logsWindowElement);
  44. setLogsWindowStyle();
  45. maskElement.offsetWidth;
  46. maskElement.style.setProperty("background-color", "black", "important");
  47. maskElement.style.setProperty("opacity", .3, "important");
  48. document.body.offsetWidth;
  49. });
  50. }
  51. },
  52. onEndPage() {
  53. const maskElement = document.querySelector(MASK_TAGNAME);
  54. logsWindowElement.remove();
  55. clearLogs();
  56. if (maskElement) {
  57. requestAnimationFrame(() => maskElement.remove());
  58. }
  59. },
  60. onLoadResource(index, maxIndex) {
  61. const progressBarElement = document.querySelector(PROGRESS_BAR_TAGNAME);
  62. if (progressBarElement && maxIndex) {
  63. const width = Math.floor((index / maxIndex) * 100) + "%";
  64. if (progressBarElement.style.width != width) {
  65. requestAnimationFrame(() => progressBarElement.style.setProperty("width", Math.floor((index / maxIndex) * 100) + "%", "important"));
  66. }
  67. }
  68. },
  69. onLoadingDeferResources() {
  70. updateLog("load-deferred-images", browser.i18n.getMessage("logPanelDeferredImages"), "…");
  71. },
  72. onLoadDeferResources() {
  73. updateLog("load-deferred-images", browser.i18n.getMessage("logPanelDeferredImages"), "✓");
  74. },
  75. onLoadingFrames() {
  76. updateLog("load-frames", browser.i18n.getMessage("logPanelFrameContents"), "…");
  77. },
  78. onLoadFrames() {
  79. updateLog("load-frames", browser.i18n.getMessage("logPanelFrameContents"), "✓");
  80. },
  81. onStartStage(step) {
  82. updateLog("step-" + step, `${browser.i18n.getMessage("logPanelStep")} ${step + 1} / 3`, "…");
  83. },
  84. onEndStage(step) {
  85. updateLog("step-" + step, `${browser.i18n.getMessage("logPanelStep")} ${step + 1} / 3`, "✓");
  86. },
  87. onPageLoading() { },
  88. onLoadPage() { },
  89. onStartStageTask() { },
  90. onEndStageTask() { }
  91. };
  92. function getSelectedArea() {
  93. return new Promise(resolve => {
  94. addEventListener("mousemove", mousemoveListener, true);
  95. addEventListener("click", clickListener, true);
  96. addEventListener("keyup", keypressListener, true);
  97. document.addEventListener("contextmenu", contextmenuListener, true);
  98. function contextmenuListener(event) {
  99. select();
  100. event.preventDefault();
  101. }
  102. function mousemoveListener(event) {
  103. const targetElement = getTarget(event);
  104. if (targetElement) {
  105. selectedAreaElement = targetElement;
  106. moveAreaSelector(targetElement);
  107. }
  108. }
  109. function clickListener(event) {
  110. select(event.button === 0 ? selectedAreaElement : null);
  111. event.preventDefault();
  112. event.stopPropagation();
  113. }
  114. function keypressListener(event) {
  115. if (event.key == "Escape") {
  116. select();
  117. }
  118. }
  119. function select(selectedElement) {
  120. removeEventListener("mousemove", mousemoveListener, true);
  121. removeEventListener("click", clickListener, true);
  122. removeEventListener("keyup", keypressListener, true);
  123. createAreaSelector().remove();
  124. resolve(selectedElement);
  125. selectedAreaElement = null;
  126. setTimeout(() => document.removeEventListener("contextmenu", contextmenuListener, true), 0);
  127. }
  128. });
  129. }
  130. function getTarget(event) {
  131. let newTarget, target = event.target, boundingRect = target.getBoundingClientRect();
  132. newTarget = determineTargetElementFloor(target, event.clientX - boundingRect.left, getMatchedParents(target, "left"));
  133. if (newTarget == target) {
  134. newTarget = determineTargetElementCeil(target, boundingRect.left + boundingRect.width - event.clientX, getMatchedParents(target, "right"));
  135. }
  136. if (newTarget == target) {
  137. newTarget = determineTargetElementFloor(target, event.clientY - boundingRect.top, getMatchedParents(target, "top"));
  138. }
  139. if (newTarget == target) {
  140. newTarget = determineTargetElementCeil(target, boundingRect.top + boundingRect.height - event.clientY, getMatchedParents(target, "bottom"));
  141. }
  142. target = newTarget;
  143. while (target && target.clientWidth <= SELECT_PX_THRESHOLD && target.clientHeight <= SELECT_PX_THRESHOLD) {
  144. target = target.parentElement;
  145. }
  146. return target;
  147. }
  148. function moveAreaSelector(target) {
  149. requestAnimationFrame(() => {
  150. const selectorElement = createAreaSelector();
  151. const boundingRect = target.getBoundingClientRect();
  152. selectorElement.style.setProperty("top", (document.documentElement.scrollTop + boundingRect.top - 10) + "px");
  153. selectorElement.style.setProperty("left", (document.documentElement.scrollLeft + boundingRect.left - 10) + "px");
  154. selectorElement.style.setProperty("width", (boundingRect.width + 20) + "px");
  155. selectorElement.style.setProperty("height", (boundingRect.height + 20) + "px");
  156. });
  157. }
  158. function createAreaSelector() {
  159. let selectorElement = document.querySelector(SELECTION_ZONE_TAGNAME);
  160. if (!selectorElement) {
  161. selectorElement = createElement(SELECTION_ZONE_TAGNAME, document.body);
  162. selectorElement.style.setProperty("box-sizing", "border-box", "important");
  163. selectorElement.style.setProperty("background-color", "#3ea9d7", "important");
  164. selectorElement.style.setProperty("border", "10px solid #0b4892", "important");
  165. selectorElement.style.setProperty("border-radius", "2px", "important");
  166. selectorElement.style.setProperty("opacity", ".25", "important");
  167. selectorElement.style.setProperty("pointer-events", "none", "important");
  168. selectorElement.style.setProperty("position", "absolute", "important");
  169. selectorElement.style.setProperty("transition", "all 100ms", "important");
  170. selectorElement.style.setProperty("cursor", "pointer", "important");
  171. selectorElement.style.setProperty("z-index", "2147483647", "important");
  172. selectorElement.style.removeProperty("border-inline-end");
  173. selectorElement.style.removeProperty("border-inline-start");
  174. selectorElement.style.removeProperty("inline-size");
  175. selectorElement.style.removeProperty("block-size");
  176. selectorElement.style.removeProperty("inset-block-start");
  177. selectorElement.style.removeProperty("inset-inline-end");
  178. selectorElement.style.removeProperty("inset-block-end");
  179. selectorElement.style.removeProperty("inset-inline-start");
  180. }
  181. return selectorElement;
  182. }
  183. function createMaskElement() {
  184. let maskElement = document.querySelector(MASK_TAGNAME);
  185. if (!maskElement) {
  186. maskElement = createElement(MASK_TAGNAME, document.body);
  187. maskElement.style.setProperty("opacity", 0, "important");
  188. maskElement.style.setProperty("background-color", "transparent", "important");
  189. maskElement.offsetWidth;
  190. maskElement.style.setProperty("position", "fixed", "important");
  191. maskElement.style.setProperty("top", "0", "important");
  192. maskElement.style.setProperty("left", "0", "important");
  193. maskElement.style.setProperty("width", "100%", "important");
  194. maskElement.style.setProperty("height", "100%", "important");
  195. maskElement.style.setProperty("z-index", 2147483646, "important");
  196. maskElement.style.setProperty("transition", "opacity 250ms", "important");
  197. }
  198. return maskElement;
  199. }
  200. function createProgressBarElement(maskElement) {
  201. let progressBarElementContainer = document.querySelector(PROGRESS_BAR_TAGNAME);
  202. if (!progressBarElementContainer) {
  203. progressBarElementContainer = createElement(PROGRESS_BAR_TAGNAME, maskElement);
  204. const styleElement = document.createElement("style");
  205. styleElement.textContent = "@keyframes single-file-progress { 0% { left: -50px } 100% { left: 0 }";
  206. maskElement.appendChild(styleElement);
  207. progressBarElementContainer.style.setProperty("position", "fixed", "important");
  208. progressBarElementContainer.style.setProperty("top", "0", "important");
  209. progressBarElementContainer.style.setProperty("left", "0", "important");
  210. progressBarElementContainer.style.setProperty("width", "0", "important");
  211. progressBarElementContainer.style.setProperty("height", "8px", "important");
  212. progressBarElementContainer.style.setProperty("overflow", "hidden", "important");
  213. progressBarElementContainer.style.setProperty("will-change", "width", "important");
  214. const progressBarElement = createElement(PROGRESS_CURSOR_TAGNAME, progressBarElementContainer);
  215. progressBarElement.style.setProperty("position", "absolute", "important");
  216. progressBarElement.style.setProperty("left", "0");
  217. progressBarElement.style.setProperty("animation", "single-file-progress 5s linear infinite reverse", "important");
  218. progressBarElement.style.setProperty("background", "white linear-gradient(-45deg, rgba(0, 0, 0, 0.1) 25%, transparent 25%, transparent 50%, rgba(0, 0, 0, 0.1) 50%, rgba(0, 0, 0, 0.1) 75%, transparent 75%, transparent) repeat scroll 0% 0% / 50px 50px padding-box border-box", "important");
  219. progressBarElement.style.setProperty("transition", "width 50ms", "important");
  220. progressBarElement.style.setProperty("width", "calc(100% + 50px)", "important");
  221. progressBarElement.style.setProperty("height", "100%", "important");
  222. progressBarElement.style.setProperty("inset-inline-start", "auto");
  223. }
  224. return progressBarElementContainer;
  225. }
  226. function createLogsWindowElement() {
  227. let logsWindowElement = document.querySelector(LOGS_WINDOW_TAGNAME);
  228. if (!logsWindowElement) {
  229. logsWindowElement = document.createElement(LOGS_WINDOW_TAGNAME);
  230. logsWindowElement.className = SINGLE_FILE_UI_ELEMENT_CLASS;
  231. }
  232. const styleElement = document.createElement("style");
  233. logsWindowElement.appendChild(styleElement);
  234. styleElement.textContent = "@keyframes single-file-pulse { 0% { opacity: .5 } 100% { opacity: 1 }";
  235. return logsWindowElement;
  236. }
  237. function setLogsWindowStyle() {
  238. initStyle(logsWindowElement);
  239. logsWindowElement.style.setProperty("opacity", "0.9", "important");
  240. logsWindowElement.style.setProperty("padding", "4px", "important");
  241. logsWindowElement.style.setProperty("position", "fixed", "important");
  242. logsWindowElement.style.setProperty("bottom", "24px", "important");
  243. logsWindowElement.style.setProperty("left", "8px", "important");
  244. logsWindowElement.style.setProperty("z-index", 2147483647, "important");
  245. logsWindowElement.style.setProperty("background-color", "white", "important");
  246. logsWindowElement.style.setProperty("min-width", browser.i18n.getMessage("logPanelWidth") + "px", "important");
  247. logsWindowElement.style.setProperty("min-height", "18px", "important");
  248. logsWindowElement.style.setProperty("transition", "height 100ms", "important");
  249. logsWindowElement.style.setProperty("will-change", "height", "important");
  250. }
  251. function updateLog(id, textContent, textStatus) {
  252. let lineElement = logsWindowElement.querySelector("[data-id='" + id + "']");
  253. if (!lineElement) {
  254. lineElement = createElement(LOGS_LINE_TAGNAME, logsWindowElement);
  255. lineElement.setAttribute("data-id", id);
  256. lineElement.style.setProperty("display", "flex");
  257. lineElement.style.setProperty("justify-content", "space-between");
  258. const textElement = createElement(LOGS_LINE_ELEMENT_TAGNAME, lineElement);
  259. textElement.style.setProperty("font-size", "13px", "important");
  260. textElement.style.setProperty("font-family", "arial, sans-serif", "important");
  261. textElement.style.setProperty("color", "black", "important");
  262. textElement.style.setProperty("background-color", "white", "important");
  263. textElement.style.setProperty("opacity", "1", "important");
  264. textElement.style.setProperty("transition", "opacity 200ms", "important");
  265. textElement.textContent = textContent;
  266. const statusElement = createElement(LOGS_LINE_ELEMENT_TAGNAME, lineElement);
  267. statusElement.style.setProperty("font-size", "11px", "important");
  268. statusElement.style.setProperty("font-family", "arial, sans-serif", "important");
  269. statusElement.style.setProperty("color", "black", "important");
  270. statusElement.style.setProperty("background-color", "white", "important");
  271. statusElement.style.setProperty("min-width", "15px", "important");
  272. statusElement.style.setProperty("text-align", "center", "important");
  273. statusElement.style.setProperty("will-change", "opacity", "important");
  274. }
  275. updateLogLine(lineElement, textContent, textStatus);
  276. }
  277. function updateLogLine(lineElement, textContent, textStatus) {
  278. const textElement = lineElement.childNodes[0];
  279. const statusElement = lineElement.childNodes[1];
  280. textElement.textContent = textContent;
  281. statusElement.style.setProperty("color", textStatus == "✓" ? "#055000" : "black", "important");
  282. if (textStatus == "✓") {
  283. textElement.style.setProperty("opacity", ".5", "important");
  284. statusElement.style.setProperty("animation", "none", "important");
  285. } else {
  286. statusElement.style.setProperty("opacity", ".5", "important");
  287. statusElement.style.setProperty("animation", "single-file-pulse 1s linear infinite alternate", "important");
  288. }
  289. statusElement.textContent = textStatus;
  290. }
  291. function clearLogs() {
  292. logsWindowElement = createLogsWindowElement();
  293. }
  294. function getMatchedParents(target, property) {
  295. let element = target, matchedParent, parents = [];
  296. do {
  297. const boundingRect = element.getBoundingClientRect();
  298. if (element.parentElement) {
  299. const parentBoundingRect = element.parentElement.getBoundingClientRect();
  300. matchedParent = Math.abs(parentBoundingRect[property] - boundingRect[property]) <= SELECT_PX_THRESHOLD;
  301. if (matchedParent) {
  302. if (element.parentElement.clientWidth > SELECT_PX_THRESHOLD && element.parentElement.clientHeight > SELECT_PX_THRESHOLD &&
  303. ((element.parentElement.clientWidth - element.clientWidth > SELECT_PX_THRESHOLD) || (element.parentElement.clientHeight - element.clientHeight > SELECT_PX_THRESHOLD))) {
  304. parents.push(element.parentElement);
  305. }
  306. element = element.parentElement;
  307. }
  308. } else {
  309. matchedParent = false;
  310. }
  311. } while (matchedParent && element);
  312. return parents;
  313. }
  314. function determineTargetElementCeil(target, widthDistance, parents) {
  315. if (Math.ceil(widthDistance / SELECT_PX_THRESHOLD) <= parents.length) {
  316. target = parents[parents.length - Math.ceil(widthDistance / SELECT_PX_THRESHOLD) - 1];
  317. }
  318. return target;
  319. }
  320. function determineTargetElementFloor(target, widthDistance, parents) {
  321. if (Math.floor(widthDistance / SELECT_PX_THRESHOLD) <= parents.length) {
  322. target = parents[parents.length - Math.floor(widthDistance / SELECT_PX_THRESHOLD) - 1];
  323. }
  324. return target;
  325. }
  326. function createElement(tagName, parentElement) {
  327. const element = document.createElement(tagName);
  328. element.className = SINGLE_FILE_UI_ELEMENT_CLASS;
  329. parentElement.appendChild(element);
  330. initStyle(element);
  331. return element;
  332. }
  333. function initStyle(element) {
  334. allProperties.forEach(property => element.style.setProperty(property, "initial", "important"));
  335. }
  336. })();