content-ui-main.js 21 KB

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