content-ui-main.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. /*
  2. * Copyright 2010-2020 Gildas Lormeau
  3. * contact : gildas.lormeau <at> gmail.com
  4. *
  5. * This file is part of SingleFile.
  6. *
  7. * The code in this file is free software: you can redistribute it and/or
  8. * modify it under the terms of the GNU Affero General Public License
  9. * (GNU AGPL) as published by the Free Software Foundation, either version 3
  10. * of the License, or (at your option) any later version.
  11. *
  12. * The code in this file 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 GNU Affero
  15. * General Public License for more details.
  16. *
  17. * As additional permission under GNU AGPL version 3 section 7, you may
  18. * distribute UNMODIFIED VERSIONS OF THIS file without the copy of the GNU
  19. * AGPL normally required by section 4, provided you include this license
  20. * notice and a URL through which recipients can access the Corresponding
  21. * Source.
  22. */
  23. /* global browser, document, prompt, getComputedStyle, addEventListener, removeEventListener, requestAnimationFrame, setTimeout, getSelection, Node */
  24. this.singlefile.extension.ui.content.main = this.singlefile.extension.ui.content.main || (() => {
  25. const SELECTED_CONTENT_ATTRIBUTE_NAME = this.singlefile.lib.helper.SELECTED_CONTENT_ATTRIBUTE_NAME;
  26. const MASK_TAGNAME = "singlefile-mask";
  27. const PROGRESS_BAR_TAGNAME = "singlefile-progress-bar";
  28. const PROGRESS_CURSOR_TAGNAME = "singlefile-progress-cursor";
  29. const SELECTION_ZONE_TAGNAME = "single-file-selection-zone";
  30. const LOGS_WINDOW_TAGNAME = "singlefile-logs-window";
  31. const LOGS_LINE_TAGNAME = "singlefile-logs-line";
  32. const LOGS_LINE_ELEMENT_TAGNAME = "singlefile-logs-element";
  33. const SINGLE_FILE_UI_ELEMENT_CLASS = "single-file-ui-element";
  34. const SELECT_PX_THRESHOLD = 8;
  35. const LOG_PANEL_DEFERRED_IMAGES_MESSAGE = browser.i18n.getMessage("logPanelDeferredImages");
  36. const LOG_PANEL_FRAME_CONTENTS_MESSAGE = browser.i18n.getMessage("logPanelFrameContents");
  37. const LOG_PANEL_STEP_MESSAGE = browser.i18n.getMessage("logPanelStep");
  38. const LOG_PANEL_WIDTH = browser.i18n.getMessage("logPanelWidth");
  39. let selectedAreaElement;
  40. let logsWindowElement = createLogsWindowElement();
  41. const allProperties = new Set();
  42. Array.from(getComputedStyle(document.body)).forEach(property => allProperties.add(property));
  43. return {
  44. getSelectedLinks,
  45. markSelection,
  46. unmarkSelection,
  47. prompt(message, defaultValue) {
  48. return prompt(message, defaultValue);
  49. },
  50. onStartPage(options) {
  51. let maskElement = document.querySelector(MASK_TAGNAME);
  52. if (!maskElement) {
  53. if (options.logsEnabled) {
  54. setLogsWindowStyle();
  55. document.body.appendChild(logsWindowElement);
  56. }
  57. if (options.shadowEnabled) {
  58. const maskElement = createMaskElement();
  59. if (options.progressBarEnabled) {
  60. createProgressBarElement(maskElement);
  61. }
  62. maskElement.offsetWidth;
  63. maskElement.style.setProperty("background-color", "black", "important");
  64. maskElement.style.setProperty("opacity", .3, "important");
  65. document.body.offsetWidth;
  66. }
  67. }
  68. },
  69. onEndPage() {
  70. const maskElement = document.querySelector(MASK_TAGNAME);
  71. if (maskElement) {
  72. maskElement.remove();
  73. }
  74. logsWindowElement.remove();
  75. clearLogs();
  76. },
  77. onLoadResource(index, maxIndex, options) {
  78. if (options.shadowEnabled && options.progressBarEnabled) {
  79. const progressBarElement = document.querySelector(PROGRESS_BAR_TAGNAME);
  80. if (progressBarElement && maxIndex) {
  81. const width = Math.floor((index / maxIndex) * 100) + "%";
  82. if (progressBarElement.style.getPropertyValue("width") != width) {
  83. requestAnimationFrame(() => progressBarElement.style.setProperty("width", Math.floor((index / maxIndex) * 100) + "%", "important"));
  84. }
  85. }
  86. }
  87. },
  88. onLoadingDeferResources(options) {
  89. updateLog("load-deferred-images", LOG_PANEL_DEFERRED_IMAGES_MESSAGE, "…", options);
  90. },
  91. onLoadDeferResources(options) {
  92. updateLog("load-deferred-images", LOG_PANEL_DEFERRED_IMAGES_MESSAGE, "✓", options);
  93. },
  94. onLoadingFrames(options) {
  95. updateLog("load-frames", LOG_PANEL_FRAME_CONTENTS_MESSAGE, "…", options);
  96. },
  97. onLoadFrames(options) {
  98. updateLog("load-frames", LOG_PANEL_FRAME_CONTENTS_MESSAGE, "✓", options);
  99. },
  100. onStartStage(step, options) {
  101. updateLog("step-" + step, `${LOG_PANEL_STEP_MESSAGE} ${step + 1} / 3`, "…", options);
  102. },
  103. onEndStage(step, options) {
  104. updateLog("step-" + step, `${LOG_PANEL_STEP_MESSAGE} ${step + 1} / 3`, "✓", options);
  105. },
  106. onPageLoading() { },
  107. onLoadPage() { },
  108. onStartStageTask() { },
  109. onEndStageTask() { }
  110. };
  111. function getSelectedLinks() {
  112. let selectionFound;
  113. const links = [];
  114. const selection = getSelection();
  115. for (let indexRange = 0; indexRange < selection.rangeCount; indexRange++) {
  116. let range = selection.getRangeAt(indexRange);
  117. if (range && range.commonAncestorContainer) {
  118. const treeWalker = document.createTreeWalker(range.commonAncestorContainer);
  119. let rangeSelectionFound = false;
  120. let finished = false;
  121. while (!finished) {
  122. if (rangeSelectionFound || treeWalker.currentNode == range.startContainer || treeWalker.currentNode == range.endContainer) {
  123. rangeSelectionFound = true;
  124. if (range.startContainer != range.endContainer || range.startOffset != range.endOffset) {
  125. selectionFound = true;
  126. if (treeWalker.currentNode.tagName == "A" && treeWalker.currentNode.href) {
  127. links.push(treeWalker.currentNode.href);
  128. }
  129. }
  130. }
  131. if (treeWalker.currentNode == range.endContainer) {
  132. finished = true;
  133. } else {
  134. treeWalker.nextNode();
  135. }
  136. }
  137. if (selectionFound && treeWalker.currentNode == range.endContainer && treeWalker.currentNode.querySelectorAll) {
  138. treeWalker.currentNode.querySelectorAll("*").forEach(descendantElement => {
  139. if (descendantElement.tagName == "A" && descendantElement.href) {
  140. links.push(treeWalker.currentNode.href);
  141. }
  142. });
  143. }
  144. }
  145. }
  146. return Array.from(new Set(links));
  147. }
  148. async function markSelection(optionallySelected) {
  149. let selectionFound = markSelectedContent();
  150. if (selectionFound || optionallySelected) {
  151. return selectionFound;
  152. } else {
  153. selectionFound = await selectArea();
  154. if (selectionFound) {
  155. return markSelectedContent();
  156. }
  157. }
  158. }
  159. function markSelectedContent() {
  160. const selection = getSelection();
  161. let selectionFound;
  162. for (let indexRange = 0; indexRange < selection.rangeCount; indexRange++) {
  163. let range = selection.getRangeAt(indexRange);
  164. if (range && range.commonAncestorContainer) {
  165. const treeWalker = document.createTreeWalker(range.commonAncestorContainer);
  166. let rangeSelectionFound = false;
  167. let finished = false;
  168. while (!finished) {
  169. if (rangeSelectionFound || treeWalker.currentNode == range.startContainer || treeWalker.currentNode == range.endContainer) {
  170. rangeSelectionFound = true;
  171. if (range.startContainer != range.endContainer || range.startOffset != range.endOffset) {
  172. selectionFound = true;
  173. markSelectedNode(treeWalker.currentNode);
  174. }
  175. }
  176. if (selectionFound && treeWalker.currentNode == range.startContainer) {
  177. markSelectedParents(treeWalker.currentNode);
  178. }
  179. if (treeWalker.currentNode == range.endContainer) {
  180. finished = true;
  181. } else {
  182. treeWalker.nextNode();
  183. }
  184. }
  185. if (selectionFound && treeWalker.currentNode == range.endContainer && treeWalker.currentNode.querySelectorAll) {
  186. treeWalker.currentNode.querySelectorAll("*").forEach(descendantElement => markSelectedNode(descendantElement));
  187. }
  188. }
  189. }
  190. return selectionFound;
  191. }
  192. function markSelectedNode(node) {
  193. const element = node.nodeType == Node.ELEMENT_NODE ? node : node.parentElement;
  194. element.setAttribute(SELECTED_CONTENT_ATTRIBUTE_NAME, "");
  195. }
  196. function markSelectedParents(node) {
  197. if (node.parentElement) {
  198. markSelectedNode(node);
  199. markSelectedParents(node.parentElement);
  200. }
  201. }
  202. function unmarkSelection() {
  203. document.querySelectorAll("[" + SELECTED_CONTENT_ATTRIBUTE_NAME + "]").forEach(selectedContent => selectedContent.removeAttribute(SELECTED_CONTENT_ATTRIBUTE_NAME));
  204. }
  205. function selectArea() {
  206. return new Promise(resolve => {
  207. let selectedRanges = [];
  208. addEventListener("mousemove", mousemoveListener, true);
  209. addEventListener("click", clickListener, true);
  210. addEventListener("keyup", keypressListener, true);
  211. document.addEventListener("contextmenu", contextmenuListener, true);
  212. getSelection().removeAllRanges();
  213. function contextmenuListener(event) {
  214. selectedRanges = [];
  215. select();
  216. event.preventDefault();
  217. }
  218. function mousemoveListener(event) {
  219. const targetElement = getTarget(event);
  220. if (targetElement) {
  221. selectedAreaElement = targetElement;
  222. moveAreaSelector(targetElement);
  223. }
  224. }
  225. function clickListener(event) {
  226. event.preventDefault();
  227. event.stopPropagation();
  228. if (event.button == 0) {
  229. select(selectedAreaElement, event.ctrlKey);
  230. } else {
  231. cancel();
  232. }
  233. }
  234. function keypressListener(event) {
  235. if (event.key == "Escape") {
  236. cancel();
  237. }
  238. }
  239. function cancel() {
  240. if (selectedRanges.length) {
  241. getSelection().removeAllRanges();
  242. }
  243. selectedRanges = [];
  244. cleanupAndResolve();
  245. }
  246. function select(selectedElement, multiSelect) {
  247. if (selectedElement) {
  248. if (!multiSelect) {
  249. restoreSelectedRanges();
  250. }
  251. const range = document.createRange();
  252. range.selectNodeContents(selectedElement);
  253. cleanupSelectionRanges();
  254. getSelection().addRange(range);
  255. saveSelectedRanges();
  256. if (!multiSelect) {
  257. cleanupAndResolve();
  258. }
  259. } else {
  260. cleanupAndResolve();
  261. }
  262. }
  263. function cleanupSelectionRanges() {
  264. const selection = getSelection();
  265. for (let indexRange = selection.rangeCount - 1; indexRange >= 0; indexRange--) {
  266. const range = selection.getRangeAt(indexRange);
  267. if (range.startOffset == range.endOffset) {
  268. selection.removeRange(range);
  269. indexRange--;
  270. }
  271. }
  272. }
  273. function cleanupAndResolve() {
  274. getAreaSelector().remove();
  275. removeEventListener("mousemove", mousemoveListener, true);
  276. removeEventListener("click", clickListener, true);
  277. removeEventListener("keyup", keypressListener, true);
  278. selectedAreaElement = null;
  279. resolve(Boolean(selectedRanges.length));
  280. setTimeout(() => document.removeEventListener("contextmenu", contextmenuListener, true), 0);
  281. }
  282. function restoreSelectedRanges() {
  283. getSelection().removeAllRanges();
  284. selectedRanges.forEach(range => getSelection().addRange(range));
  285. }
  286. function saveSelectedRanges() {
  287. selectedRanges = [];
  288. for (let indexRange = 0; indexRange < getSelection().rangeCount; indexRange++) {
  289. const range = getSelection().getRangeAt(indexRange);
  290. selectedRanges.push(range);
  291. }
  292. }
  293. });
  294. }
  295. function getTarget(event) {
  296. let newTarget, target = event.target, boundingRect = target.getBoundingClientRect();
  297. newTarget = determineTargetElement("floor", target, event.clientX - boundingRect.left, getMatchedParents(target, "left"));
  298. if (newTarget == target) {
  299. newTarget = determineTargetElement("ceil", target, boundingRect.left + boundingRect.width - event.clientX, getMatchedParents(target, "right"));
  300. }
  301. if (newTarget == target) {
  302. newTarget = determineTargetElement("floor", target, event.clientY - boundingRect.top, getMatchedParents(target, "top"));
  303. }
  304. if (newTarget == target) {
  305. newTarget = determineTargetElement("ceil", target, boundingRect.top + boundingRect.height - event.clientY, getMatchedParents(target, "bottom"));
  306. }
  307. target = newTarget;
  308. while (target && target.clientWidth <= SELECT_PX_THRESHOLD && target.clientHeight <= SELECT_PX_THRESHOLD) {
  309. target = target.parentElement;
  310. }
  311. return target;
  312. }
  313. function moveAreaSelector(target) {
  314. requestAnimationFrame(() => {
  315. const selectorElement = getAreaSelector();
  316. const boundingRect = target.getBoundingClientRect();
  317. const scrollingElement = document.scrollingElement || document.documentElement;
  318. selectorElement.style.setProperty("top", (scrollingElement.scrollTop + boundingRect.top - 10) + "px");
  319. selectorElement.style.setProperty("left", (scrollingElement.scrollLeft + boundingRect.left - 10) + "px");
  320. selectorElement.style.setProperty("width", (boundingRect.width + 20) + "px");
  321. selectorElement.style.setProperty("height", (boundingRect.height + 20) + "px");
  322. });
  323. }
  324. function getAreaSelector() {
  325. let selectorElement = document.querySelector(SELECTION_ZONE_TAGNAME);
  326. if (!selectorElement) {
  327. selectorElement = createElement(SELECTION_ZONE_TAGNAME, document.body);
  328. selectorElement.style.setProperty("box-sizing", "border-box", "important");
  329. selectorElement.style.setProperty("background-color", "#3ea9d7", "important");
  330. selectorElement.style.setProperty("border", "10px solid #0b4892", "important");
  331. selectorElement.style.setProperty("border-radius", "2px", "important");
  332. selectorElement.style.setProperty("opacity", ".25", "important");
  333. selectorElement.style.setProperty("pointer-events", "none", "important");
  334. selectorElement.style.setProperty("position", "absolute", "important");
  335. selectorElement.style.setProperty("transition", "all 100ms", "important");
  336. selectorElement.style.setProperty("cursor", "pointer", "important");
  337. selectorElement.style.setProperty("z-index", "2147483647", "important");
  338. selectorElement.style.removeProperty("border-inline-end");
  339. selectorElement.style.removeProperty("border-inline-start");
  340. selectorElement.style.removeProperty("inline-size");
  341. selectorElement.style.removeProperty("block-size");
  342. selectorElement.style.removeProperty("inset-block-start");
  343. selectorElement.style.removeProperty("inset-inline-end");
  344. selectorElement.style.removeProperty("inset-block-end");
  345. selectorElement.style.removeProperty("inset-inline-start");
  346. }
  347. return selectorElement;
  348. }
  349. function createMaskElement() {
  350. let maskElement = document.querySelector(MASK_TAGNAME);
  351. if (!maskElement) {
  352. maskElement = createElement(MASK_TAGNAME);
  353. maskElement.style.setProperty("opacity", 0, "important");
  354. maskElement.style.setProperty("background-color", "transparent", "important");
  355. maskElement.style.setProperty("position", "fixed", "important");
  356. maskElement.style.setProperty("top", "0", "important");
  357. maskElement.style.setProperty("left", "0", "important");
  358. maskElement.style.setProperty("width", "100%", "important");
  359. maskElement.style.setProperty("height", "100%", "important");
  360. maskElement.style.setProperty("z-index", 2147483646, "important");
  361. maskElement.style.setProperty("transition", "opacity 250ms", "important");
  362. document.body.appendChild(maskElement);
  363. maskElement.offsetWidth;
  364. }
  365. return maskElement;
  366. }
  367. function createProgressBarElement(maskElement) {
  368. let progressBarElementContainer = document.querySelector(PROGRESS_BAR_TAGNAME);
  369. if (!progressBarElementContainer) {
  370. progressBarElementContainer = createElement(PROGRESS_BAR_TAGNAME, maskElement);
  371. const styleElement = document.createElement("style");
  372. styleElement.textContent = "@keyframes single-file-progress { 0% { left: -50px } 100% { left: 0 }";
  373. maskElement.appendChild(styleElement);
  374. progressBarElementContainer.style.setProperty("position", "fixed", "important");
  375. progressBarElementContainer.style.setProperty("top", "0", "important");
  376. progressBarElementContainer.style.setProperty("left", "0", "important");
  377. progressBarElementContainer.style.setProperty("width", "0", "important");
  378. progressBarElementContainer.style.setProperty("height", "8px", "important");
  379. progressBarElementContainer.style.setProperty("overflow", "hidden", "important");
  380. progressBarElementContainer.style.setProperty("transition", "width 200ms", "important");
  381. progressBarElementContainer.style.setProperty("will-change", "width", "important");
  382. const progressBarElement = createElement(PROGRESS_CURSOR_TAGNAME, progressBarElementContainer);
  383. progressBarElement.style.setProperty("position", "absolute", "important");
  384. progressBarElement.style.setProperty("left", "0");
  385. progressBarElement.style.setProperty("animation", "single-file-progress 5s linear infinite reverse", "important");
  386. 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");
  387. progressBarElement.style.setProperty("width", "calc(100% + 50px)", "important");
  388. progressBarElement.style.setProperty("height", "100%", "important");
  389. progressBarElement.style.setProperty("inset-inline-start", "auto");
  390. }
  391. return progressBarElementContainer;
  392. }
  393. function createLogsWindowElement() {
  394. let logsWindowElement = document.querySelector(LOGS_WINDOW_TAGNAME);
  395. if (!logsWindowElement) {
  396. logsWindowElement = document.createElement(LOGS_WINDOW_TAGNAME);
  397. logsWindowElement.className = SINGLE_FILE_UI_ELEMENT_CLASS;
  398. }
  399. const styleElement = document.createElement("style");
  400. logsWindowElement.appendChild(styleElement);
  401. styleElement.textContent = "@keyframes single-file-pulse { 0% { opacity: .5 } 100% { opacity: 1 }";
  402. return logsWindowElement;
  403. }
  404. function setLogsWindowStyle() {
  405. initStyle(logsWindowElement);
  406. logsWindowElement.style.setProperty("opacity", "0.9", "important");
  407. logsWindowElement.style.setProperty("padding", "4px", "important");
  408. logsWindowElement.style.setProperty("position", "fixed", "important");
  409. logsWindowElement.style.setProperty("bottom", "24px", "important");
  410. logsWindowElement.style.setProperty("left", "8px", "important");
  411. logsWindowElement.style.setProperty("z-index", 2147483647, "important");
  412. logsWindowElement.style.setProperty("background-color", "white", "important");
  413. logsWindowElement.style.setProperty("min-width", LOG_PANEL_WIDTH + "px", "important");
  414. logsWindowElement.style.setProperty("min-height", "16px", "important");
  415. logsWindowElement.style.setProperty("transition", "height 100ms", "important");
  416. logsWindowElement.style.setProperty("will-change", "height", "important");
  417. }
  418. function updateLog(id, textContent, textStatus, options) {
  419. if (options.logsEnabled) {
  420. let lineElement = logsWindowElement.querySelector("[data-id='" + id + "']");
  421. if (!lineElement) {
  422. lineElement = createElement(LOGS_LINE_TAGNAME, logsWindowElement);
  423. lineElement.setAttribute("data-id", id);
  424. lineElement.style.setProperty("display", "flex", "important");
  425. lineElement.style.setProperty("justify-content", "space-between", "important");
  426. lineElement.style.setProperty("padding", "2px", "important");
  427. const textElement = createElement(LOGS_LINE_ELEMENT_TAGNAME, lineElement);
  428. textElement.style.setProperty("font-size", "13px", "important");
  429. textElement.style.setProperty("font-family", "arial, sans-serif", "important");
  430. textElement.style.setProperty("color", "black", "important");
  431. textElement.style.setProperty("background-color", "white", "important");
  432. textElement.style.setProperty("opacity", "1", "important");
  433. textElement.style.setProperty("transition", "opacity 200ms", "important");
  434. textElement.textContent = textContent;
  435. const statusElement = createElement(LOGS_LINE_ELEMENT_TAGNAME, lineElement);
  436. statusElement.style.setProperty("font-size", "11px", "important");
  437. statusElement.style.setProperty("font-family", "arial, sans-serif", "important");
  438. statusElement.style.setProperty("color", "black", "important");
  439. statusElement.style.setProperty("background-color", "white", "important");
  440. statusElement.style.setProperty("min-width", "15px", "important");
  441. statusElement.style.setProperty("text-align", "center", "important");
  442. statusElement.style.setProperty("position", "relative", "important");
  443. statusElement.style.setProperty("top", "1px", "important");
  444. statusElement.style.setProperty("will-change", "opacity", "important");
  445. }
  446. updateLogLine(lineElement, textContent, textStatus);
  447. }
  448. }
  449. function updateLogLine(lineElement, textContent, textStatus) {
  450. const textElement = lineElement.childNodes[0];
  451. const statusElement = lineElement.childNodes[1];
  452. textElement.textContent = textContent;
  453. statusElement.style.setProperty("color", textStatus == "✓" ? "#055000" : "black", "important");
  454. if (textStatus == "✓") {
  455. textElement.style.setProperty("opacity", ".5", "important");
  456. statusElement.style.setProperty("animation", "none", "important");
  457. } else {
  458. statusElement.style.setProperty("opacity", ".5", "important");
  459. statusElement.style.setProperty("animation", "single-file-pulse 1s linear infinite alternate", "important");
  460. }
  461. statusElement.textContent = textStatus;
  462. }
  463. function clearLogs() {
  464. logsWindowElement = createLogsWindowElement();
  465. }
  466. function getMatchedParents(target, property) {
  467. let element = target, matchedParent, parents = [];
  468. do {
  469. const boundingRect = element.getBoundingClientRect();
  470. if (element.parentElement) {
  471. const parentBoundingRect = element.parentElement.getBoundingClientRect();
  472. matchedParent = Math.abs(parentBoundingRect[property] - boundingRect[property]) <= SELECT_PX_THRESHOLD;
  473. if (matchedParent) {
  474. if (element.parentElement.clientWidth > SELECT_PX_THRESHOLD && element.parentElement.clientHeight > SELECT_PX_THRESHOLD &&
  475. ((element.parentElement.clientWidth - element.clientWidth > SELECT_PX_THRESHOLD) || (element.parentElement.clientHeight - element.clientHeight > SELECT_PX_THRESHOLD))) {
  476. parents.push(element.parentElement);
  477. }
  478. element = element.parentElement;
  479. }
  480. } else {
  481. matchedParent = false;
  482. }
  483. } while (matchedParent && element);
  484. return parents;
  485. }
  486. function determineTargetElement(roundingMethod, target, widthDistance, parents) {
  487. if (Math[roundingMethod](widthDistance / SELECT_PX_THRESHOLD) <= parents.length) {
  488. target = parents[parents.length - Math[roundingMethod](widthDistance / SELECT_PX_THRESHOLD) - 1];
  489. }
  490. return target;
  491. }
  492. function createElement(tagName, parentElement) {
  493. const element = document.createElement(tagName);
  494. element.className = SINGLE_FILE_UI_ELEMENT_CLASS;
  495. if (parentElement) {
  496. parentElement.appendChild(element);
  497. }
  498. initStyle(element);
  499. return element;
  500. }
  501. function initStyle(element) {
  502. allProperties.forEach(property => element.style.setProperty(property, "initial", "important"));
  503. }
  504. })();