content-ui-main.js 21 KB

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