content-ui.js 15 KB

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