content-ui.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. lineElement.childNodes[1].textContent = textStatus;
  112. }
  113. function clearLogs() {
  114. logsWindowElement = createLogsWindowElement();
  115. }
  116. function getSelectedArea() {
  117. return new Promise(resolve => {
  118. addEventListener("mousemove", mousemoveListener, true);
  119. addEventListener("click", clickListener, true);
  120. addEventListener("keyup", keypressListener, true);
  121. document.addEventListener("contextmenu", contextmenuListener, true);
  122. function contextmenuListener(event) {
  123. select();
  124. event.preventDefault();
  125. }
  126. function mousemoveListener(event) {
  127. const targetElement = getTarget(event);
  128. if (targetElement) {
  129. selectedAreaElement = targetElement;
  130. moveAreaSelector(targetElement);
  131. }
  132. }
  133. function clickListener(event) {
  134. select(event.button === 0 ? selectedAreaElement : null);
  135. event.preventDefault();
  136. event.stopPropagation();
  137. }
  138. function keypressListener(event) {
  139. if (event.key == "Escape") {
  140. select();
  141. }
  142. }
  143. function select(selectedElement) {
  144. removeEventListener("mousemove", mousemoveListener, true);
  145. removeEventListener("click", clickListener, true);
  146. removeEventListener("keyup", keypressListener, true);
  147. createAreaSelector().remove();
  148. resolve(selectedElement);
  149. selectedAreaElement = null;
  150. setTimeout(() => document.removeEventListener("contextmenu", contextmenuListener, true), 0);
  151. }
  152. });
  153. }
  154. function getTarget(event) {
  155. let newTarget, target = event.target, boundingRect = target.getBoundingClientRect();
  156. newTarget = determineTargetElementFloor(target, event.clientX - boundingRect.left, getMatchedParents(target, "left"));
  157. if (newTarget == target) {
  158. newTarget = determineTargetElementCeil(target, boundingRect.left + boundingRect.width - event.clientX, getMatchedParents(target, "right"));
  159. }
  160. if (newTarget == target) {
  161. newTarget = determineTargetElementFloor(target, event.clientY - boundingRect.top, getMatchedParents(target, "top"));
  162. }
  163. if (newTarget == target) {
  164. newTarget = determineTargetElementCeil(target, boundingRect.top + boundingRect.height - event.clientY, getMatchedParents(target, "bottom"));
  165. }
  166. target = newTarget;
  167. while (target && target.clientWidth <= SELECT_PX_THRESHOLD && target.clientHeight <= SELECT_PX_THRESHOLD) {
  168. target = target.parentElement;
  169. }
  170. return target;
  171. }
  172. function moveAreaSelector(target) {
  173. requestAnimationFrame(() => {
  174. const selectorElement = createAreaSelector();
  175. const boundingRect = target.getBoundingClientRect();
  176. selectorElement.style.setProperty("top", (scrollY + boundingRect.top - 10) + "px");
  177. selectorElement.style.setProperty("left", (scrollX + boundingRect.left - 10) + "px");
  178. selectorElement.style.setProperty("width", (boundingRect.width + 20) + "px");
  179. selectorElement.style.setProperty("height", (boundingRect.height + 20) + "px");
  180. });
  181. }
  182. function createAreaSelector() {
  183. let selectorElement = document.querySelector(SELECTION_ZONE_TAGNAME);
  184. if (!selectorElement) {
  185. selectorElement = createElement(SELECTION_ZONE_TAGNAME, document.body);
  186. selectorElement.style.setProperty("box-sizing", "border-box", "important");
  187. selectorElement.style.setProperty("background-color", "#3ea9d7", "important");
  188. selectorElement.style.setProperty("border", "10px solid #0b4892", "important");
  189. selectorElement.style.setProperty("border-radius", "2px", "important");
  190. selectorElement.style.setProperty("opacity", ".25", "important");
  191. selectorElement.style.setProperty("pointer-events", "none", "important");
  192. selectorElement.style.setProperty("position", "absolute", "important");
  193. selectorElement.style.setProperty("transition", "all 100ms", "important");
  194. selectorElement.style.setProperty("cursor", "pointer", "important");
  195. selectorElement.style.setProperty("z-index", "2147483647", "important");
  196. selectorElement.style.removeProperty("border-inline-end");
  197. selectorElement.style.removeProperty("border-inline-start");
  198. selectorElement.style.removeProperty("inline-size");
  199. selectorElement.style.removeProperty("block-size");
  200. selectorElement.style.removeProperty("inset-block-start");
  201. selectorElement.style.removeProperty("inset-inline-end");
  202. selectorElement.style.removeProperty("inset-block-end");
  203. selectorElement.style.removeProperty("inset-inline-start");
  204. }
  205. return selectorElement;
  206. }
  207. function createMaskElement() {
  208. let maskElement = document.querySelector(MASK_TAGNAME);
  209. if (!maskElement) {
  210. maskElement = createElement(MASK_TAGNAME, document.body);
  211. maskElement.style.setProperty("opacity", 0, "important");
  212. maskElement.style.setProperty("background-color", "transparent", "important");
  213. maskElement.offsetWidth;
  214. maskElement.style.setProperty("position", "fixed", "important");
  215. maskElement.style.setProperty("top", "0", "important");
  216. maskElement.style.setProperty("left", "0", "important");
  217. maskElement.style.setProperty("width", "100%", "important");
  218. maskElement.style.setProperty("height", "100%", "important");
  219. maskElement.style.setProperty("z-index", 2147483646, "important");
  220. maskElement.style.setProperty("transition", "opacity 250ms", "important");
  221. }
  222. return maskElement;
  223. }
  224. function createProgressBarElement(maskElement) {
  225. let progressBarElement = document.querySelector(PROGRESS_BAR_TAGNAME);
  226. if (!progressBarElement) {
  227. progressBarElement = createElement(PROGRESS_BAR_TAGNAME, maskElement);
  228. progressBarElement.style.setProperty("background-color", "white", "important");
  229. progressBarElement.style.setProperty("position", "fixed", "important");
  230. progressBarElement.style.setProperty("top", "0", "important");
  231. progressBarElement.style.setProperty("left", "0", "important");
  232. progressBarElement.style.setProperty("width", "0", "important");
  233. progressBarElement.style.setProperty("height", "8px", "important");
  234. progressBarElement.style.setProperty("transition", "width 100ms", "important");
  235. progressBarElement.style.setProperty("will-change", "width", "important");
  236. }
  237. return progressBarElement;
  238. }
  239. function createLogsWindowElement() {
  240. let logsWindowElement = document.querySelector(LOGS_WINDOW_TAGNAME);
  241. if (!logsWindowElement) {
  242. logsWindowElement = document.createElement(LOGS_WINDOW_TAGNAME);
  243. }
  244. return logsWindowElement;
  245. }
  246. function setLogsWindowStyle() {
  247. initStyle(logsWindowElement);
  248. logsWindowElement.style.setProperty("opacity", "0.9", "important");
  249. logsWindowElement.style.setProperty("padding", "4px", "important");
  250. logsWindowElement.style.setProperty("position", "fixed", "important");
  251. logsWindowElement.style.setProperty("bottom", "24px", "important");
  252. logsWindowElement.style.setProperty("left", "8px", "important");
  253. logsWindowElement.style.setProperty("z-index", 2147483647, "important");
  254. logsWindowElement.style.setProperty("background-color", "white", "important");
  255. logsWindowElement.style.setProperty("min-width", browser.i18n.getMessage("logPanelWidth"), "important");
  256. logsWindowElement.style.setProperty("min-height", "16px", "important");
  257. logsWindowElement.style.setProperty("transition", "height 100ms", "important");
  258. logsWindowElement.style.setProperty("will-change", "height", "important");
  259. }
  260. function getMatchedParents(target, property) {
  261. let element = target, matchedParent, parents = [];
  262. do {
  263. const boundingRect = element.getBoundingClientRect();
  264. if (element.parentElement) {
  265. const parentBoundingRect = element.parentElement.getBoundingClientRect();
  266. matchedParent = Math.abs(parentBoundingRect[property] - boundingRect[property]) <= SELECT_PX_THRESHOLD;
  267. if (matchedParent) {
  268. if (element.parentElement.clientWidth > SELECT_PX_THRESHOLD && element.parentElement.clientHeight > SELECT_PX_THRESHOLD &&
  269. ((element.parentElement.clientWidth - element.clientWidth > SELECT_PX_THRESHOLD) || (element.parentElement.clientHeight - element.clientHeight > SELECT_PX_THRESHOLD))) {
  270. parents.push(element.parentElement);
  271. }
  272. element = element.parentElement;
  273. }
  274. } else {
  275. matchedParent = false;
  276. }
  277. } while (matchedParent && element);
  278. return parents;
  279. }
  280. function determineTargetElementCeil(target, widthDistance, parents) {
  281. if (Math.ceil(widthDistance / SELECT_PX_THRESHOLD) <= parents.length) {
  282. target = parents[parents.length - Math.ceil(widthDistance / SELECT_PX_THRESHOLD) - 1];
  283. }
  284. return target;
  285. }
  286. function determineTargetElementFloor(target, widthDistance, parents) {
  287. if (Math.floor(widthDistance / SELECT_PX_THRESHOLD) <= parents.length) {
  288. target = parents[parents.length - Math.floor(widthDistance / SELECT_PX_THRESHOLD) - 1];
  289. }
  290. return target;
  291. }
  292. function createElement(tagName, parentElement) {
  293. const element = document.createElement(tagName);
  294. parentElement.appendChild(element);
  295. initStyle(element);
  296. return element;
  297. }
  298. function initStyle(element) {
  299. Array.from(getComputedStyle(element)).forEach(property => element.style.setProperty(property, "initial", "important"));
  300. }
  301. })();