content-ui.js 18 KB

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