content-ui.js 14 KB

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