content-ui.js 14 KB

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