1
0

single-file-extension-editor.js 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127
  1. (function () {
  2. 'use strict';
  3. /*
  4. * Copyright 2010-2020 Gildas Lormeau
  5. * contact : gildas.lormeau <at> gmail.com
  6. *
  7. * This file is part of SingleFile.
  8. *
  9. * The code in this file is free software: you can redistribute it and/or
  10. * modify it under the terms of the GNU Affero General Public License
  11. * (GNU AGPL) as published by the Free Software Foundation, either version 3
  12. * of the License, or (at your option) any later version.
  13. *
  14. * The code in this file is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
  17. * General Public License for more details.
  18. *
  19. * As additional permission under GNU AGPL version 3 section 7, you may
  20. * distribute UNMODIFIED VERSIONS OF THIS file without the copy of the GNU
  21. * AGPL normally required by section 4, provided you include this license
  22. * notice and a URL through which recipients can access the Corresponding
  23. * Source.
  24. */
  25. /* global globalThis, window, document, fetch, DOMParser, getComputedStyle, setTimeout, clearTimeout, NodeFilter, Readability, isProbablyReaderable, matchMedia, TextDecoder, Node */
  26. (globalThis => {
  27. const singlefile = globalThis.singlefile;
  28. const FORBIDDEN_TAG_NAMES = ["a", "area", "audio", "base", "br", "col", "command", "embed", "hr", "img", "iframe", "input", "keygen", "link", "meta", "param", "source", "track", "video", "wbr"];
  29. const BUTTON_ANCHOR_URL = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgAgMAAAAOFJJnAAABhGlDQ1BJQ0MgcHJvZmlsZQAAKJF9kT1Iw0AcxV9TtaIVETuIOASsThZERRylikWwUNoKrTqYXPohNGlIUlwcBdeCgx+LVQcXZ10dXAVB8APEydFJ0UVK/F9SaBHjwXE/3t173L0DhFqJqWbbOKBqlpGMRcVMdkUMvKIbfQCG0SExU4+nFtLwHF/38PH1LsKzvM/9OXqUnMkAn0g8y3TDIl4nnt60dM77xCFWlBTic+Ixgy5I/Mh12eU3zgWHBZ4ZMtLJOeIQsVhoYbmFWdFQiaeIw4qqUb6QcVnhvMVZLVVY4578hcGctpziOs0hxLCIOBIQIaOCDZRgIUKrRoqJJO1HPfyDjj9BLplcG2DkmEcZKiTHD/4Hv7s185MTblIwCrS/2PbHCBDYBepV2/4+tu36CeB/Bq60pr9cA2Y+Sa82tfAR0LsNXFw3NXkPuNwBBp50yZAcyU9TyOeB9zP6pizQfwt0rbq9NfZx+gCkqaulG+DgEBgtUPaax7s7W3v790yjvx825XKP2aKCdAAAAAlwSFlzAAAuIwAALiMBeKU/dgAAAAd0SU1FB+QLEQA4M3Y7LzIAAAAZdEVYdENvbW1lbnQAQ3JlYXRlZCB3aXRoIEdJTVBXgQ4XAAAACVBMVEUAAAAAAACKioqjwG1pAAAAAXRSTlMAQObYZgAAAAFiS0dEAmYLfGQAAABkSURBVBjThc47CsNADIThWfD0bnSfbdIroP/+V0mhsN5gTNToK0YPaSvnF9B9wGykG54j/2GF1/hauE4E1AOuNxrBdA5KUXIqdiCnqC1zIZ2mFJQzKJ3wesOhcwDM4+fo7cOuD9C4HTQ9HAAQAAAAAElFTkSuQmCC";
  30. const BUTTON_CLOSE_URL = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgAgMAAAAOFJJnAAABhGlDQ1BJQ0MgcHJvZmlsZQAAKJF9kT1Iw0AcxV9TtaIVETuIOASsThZERRylikWwUNoKrTqYXPohNGlIUlwcBdeCgx+LVQcXZ10dXAVB8APEydFJ0UVK/F9SaBHjwXE/3t173L0DhFqJqWbbOKBqlpGMRcVMdkUMvKIbfQCG0SExU4+nFtLwHF/38PH1LsKzvM/9OXqUnMkAn0g8y3TDIl4nnt60dM77xCFWlBTic+Ixgy5I/Mh12eU3zgWHBZ4ZMtLJOeIQsVhoYbmFWdFQiaeIw4qqUb6QcVnhvMVZLVVY4578hcGctpziOs0hxLCIOBIQIaOCDZRgIUKrRoqJJO1HPfyDjj9BLplcG2DkmEcZKiTHD/4Hv7s185MTblIwCrS/2PbHCBDYBepV2/4+tu36CeB/Bq60pr9cA2Y+Sa82tfAR0LsNXFw3NXkPuNwBBp50yZAcyU9TyOeB9zP6pizQfwt0rbq9NfZx+gCkqaulG+DgEBgtUPaax7s7W3v790yjvx825XKP2aKCdAAAAAlwSFlzAAAuIwAALiMBeKU/dgAAAAd0SU1FB+QLEQA6Na1u6IUAAAAZdEVYdENvbW1lbnQAQ3JlYXRlZCB3aXRoIEdJTVBXgQ4XAAAACVBMVEUAAAAAAACKioqjwG1pAAAAAXRSTlMAQObYZgAAAAFiS0dEAmYLfGQAAABlSURBVBhXTc/BEUQhCAPQ58ES6McSPED/rfwDI7vOMCoJIeGd6CvFgZXiwk47Ia5VUKdrVXcb39kfqxqmTg+I2xJ2tqhVTaGaQjTl7/GgIc/4CL4Vs3RsjLFndcxPnAn4iww8A3yQjRZjti1t6AAAAABJRU5ErkJggg==";
  31. const SHADOWROOT_ATTRIBUTE_NAME = "shadowroot";
  32. const SCRIPT_TEMPLATE_SHADOW_ROOT = "data-template-shadow-root";
  33. const NOTE_TAGNAME = "single-file-note";
  34. const NOTE_CLASS = "note";
  35. const NOTE_MASK_CLASS = "note-mask";
  36. const NOTE_HIDDEN_CLASS = "note-hidden";
  37. const NOTE_ANCHORED_CLASS = "note-anchored";
  38. const NOTE_SELECTED_CLASS = "note-selected";
  39. const NOTE_MOVING_CLASS = "note-moving";
  40. const NOTE_MASK_MOVING_CLASS = "note-mask-moving";
  41. const PAGE_MASK_CLASS = "page-mask";
  42. const MASK_CLASS = "single-file-mask";
  43. const PAGE_MASK_CONTAINER_CLASS = "single-file-page-mask";
  44. const HIGHLIGHT_CLASS = "single-file-highlight";
  45. const REMOVED_CONTENT_CLASS = "single-file-removed";
  46. const HIGHLIGHT_HIDDEN_CLASS = "single-file-highlight-hidden";
  47. const PAGE_MASK_ACTIVE_CLASS = "page-mask-active";
  48. const CUT_HOVER_CLASS = "single-file-cut-hover";
  49. const CUT_OUTER_HOVER_CLASS = "single-file-cut-outer-hover";
  50. const CUT_SELECTED_CLASS = "single-file-cut-selected";
  51. const CUT_OUTER_SELECTED_CLASS = "single-file-cut-outer-selected";
  52. const NOTE_INITIAL_POSITION_X = 20;
  53. const NOTE_INITIAL_POSITION_Y = 20;
  54. const NOTE_INITIAL_WIDTH = 150;
  55. const NOTE_INITIAL_HEIGHT = 150;
  56. const NOTE_HEADER_HEIGHT = 25;
  57. const DISABLED_NOSCRIPT_ATTRIBUTE_NAME = "data-single-file-disabled-noscript";
  58. const COMMENT_HEADER = "Page saved with SingleFile";
  59. const COMMENT_HEADER_LEGACY = "Archive processed by SingleFile";
  60. const STYLE_FORMATTED_PAGE = `
  61. /* This Source Code Form is subject to the terms of the Mozilla Public
  62. * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  63. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  64. /* Avoid adding ID selector rules in this style sheet, since they could
  65. * inadvertently match elements in the article content. */
  66. :root {
  67. --grey-90-a10: rgba(12, 12, 13, 0.1);
  68. --grey-90-a20: rgba(12, 12, 13, 0.2);
  69. --grey-90-a30: rgba(12, 12, 13, 0.3);
  70. --grey-90-a80: rgba(12, 12, 13, 0.8);
  71. --grey-30: #d7d7db;
  72. --blue-40: #45a1ff;
  73. --blue-40-a30: rgba(69, 161, 255, 0.3);
  74. --blue-60: #0060df;
  75. --body-padding: 64px;
  76. --font-size: 12;
  77. --content-width: 70em;
  78. --line-height: 1.6em;
  79. }
  80. body {
  81. --main-background: #fff;
  82. --main-foreground: #333;
  83. --font-color: #000000;
  84. --primary-color: #0B83FF;
  85. --toolbar-border: var(--grey-90-a20);
  86. --toolbar-transparent-border: transparent;
  87. --toolbar-box-shadow: var(--grey-90-a10);
  88. --toolbar-button-background: transparent;
  89. --toolbar-button-background-hover: var(--grey-90-a10);
  90. --toolbar-button-foreground-hover: var(--font-color);
  91. --toolbar-button-background-active: var(--grey-90-a20);
  92. --toolbar-button-foreground-active: var(--primary-color);
  93. --toolbar-button-border: transparent;
  94. --toolbar-button-border-hover: transparent;
  95. --toolbar-button-border-active: transparent;
  96. --tooltip-background: var(--grey-90-a80);
  97. --tooltip-foreground: white;
  98. --tooltip-border: transparent;
  99. --popup-background: white;
  100. --popup-border: rgba(0, 0, 0, 0.12);
  101. --opaque-popup-border: #e0e0e0;
  102. --popup-line: var(--grey-30);
  103. --popup-shadow: rgba(49, 49, 49, 0.3);
  104. --popup-button-background: #edecf0;
  105. --popup-button-background-hover: hsla(0,0%,70%,.4);
  106. --popup-button-foreground-hover: var(--font-color);
  107. --popup-button-background-active: hsla(240,5%,5%,.15);
  108. --selected-background: var(--blue-40-a30);
  109. --selected-border: var(--blue-40);
  110. --font-value-border: var(--grey-30);
  111. --icon-fill: #3b3b3c;
  112. --icon-disabled-fill: #8080807F;
  113. --link-foreground: var(--blue-60);
  114. --link-selected-foreground: #333;
  115. --visited-link-foreground: #b5007f;
  116. /* light colours */
  117. }
  118. body.sepia {
  119. --main-background: #f4ecd8;
  120. --main-foreground: #5b4636;
  121. --toolbar-border: #5b4636;
  122. }
  123. body.dark {
  124. --main-background: rgb(28, 27, 34);
  125. --main-foreground: #eee;
  126. --font-color: #fff;
  127. --toolbar-border: #4a4a4b;
  128. --toolbar-box-shadow: black;
  129. --toolbar-button-background-hover: var(--grey-90-a30);
  130. --toolbar-button-background-active: var(--grey-90-a80);
  131. --tooltip-background: black;
  132. --tooltip-foreground: white;
  133. --popup-background: rgb(66,65,77);
  134. --opaque-popup-border: #434146;
  135. --popup-line: rgb(82, 82, 94);
  136. --popup-button-background: #5c5c61;
  137. --popup-button-background-active: hsla(0,0%,70%,.6);
  138. --selected-background: #3E6D9A;
  139. --font-value-border: #656468;
  140. --icon-fill: #fff;
  141. --icon-disabled-fill: #ffffff66;
  142. --link-foreground: #45a1ff;
  143. --link-selected-foreground: #fff;
  144. --visited-link-foreground: #e675fd;
  145. /* dark colours */
  146. }
  147. body.hcm {
  148. --main-background: Canvas;
  149. --main-foreground: CanvasText;
  150. --font-color: CanvasText;
  151. --primary-color: SelectedItem;
  152. --toolbar-border: CanvasText;
  153. /* We need a true transparent but in HCM this would compute to an actual color,
  154. so select the page's background color instead: */
  155. --toolbar-transparent-border: Canvas;
  156. --toolbar-box-shadow: Canvas;
  157. --toolbar-button-background: ButtonFace;
  158. --toolbar-button-background-hover: ButtonText;
  159. --toolbar-button-foreground-hover: ButtonFace;
  160. --toolbar-button-background-active: SelectedItem;
  161. --toolbar-button-foreground-active: SelectedItemText;
  162. --toolbar-button-border: ButtonText;
  163. --toolbar-button-border-hover: ButtonText;
  164. --toolbar-button-border-active: ButtonText;
  165. --tooltip-background: Canvas;
  166. --tooltip-foreground: CanvasText;
  167. --tooltip-border: CanvasText;
  168. --popup-background: Canvas;
  169. --popup-border: CanvasText;
  170. --opaque-popup-border: CanvasText;
  171. --popup-line: CanvasText;
  172. --popup-button-background: ButtonFace;
  173. --popup-button-background-hover: ButtonText;
  174. --popup-button-foreground-hover: ButtonFace;
  175. --popup-button-background-active: ButtonText;
  176. --selected-background: Canvas;
  177. --selected-border: SelectedItem;
  178. --font-value-border: CanvasText;
  179. --icon-fill: ButtonText;
  180. --icon-disabled-fill: GrayText;
  181. --link-foreground: LinkText;
  182. --link-selected-foreground: ActiveText;
  183. --visited-link-foreground: VisitedText;
  184. }
  185. body {
  186. margin: 0;
  187. padding: var(--body-padding);
  188. background-color: var(--main-background);
  189. color: var(--main-foreground);
  190. }
  191. body.loaded {
  192. transition: color 0.4s, background-color 0.4s;
  193. }
  194. body.dark *::-moz-selection {
  195. background-color: var(--selected-background);
  196. }
  197. a::-moz-selection {
  198. color: var(--link-selected-foreground);
  199. }
  200. body.sans-serif,
  201. body.sans-serif .remove-button {
  202. font-family: Helvetica, Arial, sans-serif;
  203. }
  204. body.serif,
  205. body.serif .remove-button {
  206. font-family: Georgia, "Times New Roman", serif;
  207. }
  208. /* Override some controls and content styles based on color scheme */
  209. body.light > .container > .header > .domain {
  210. border-bottom-color: #333333 !important;
  211. }
  212. body.sepia > .container > .header > .domain {
  213. border-bottom-color: #5b4636 !important;
  214. }
  215. body.dark > .container > .header > .domain {
  216. border-bottom-color: #eeeeee !important;
  217. }
  218. body.light blockquote {
  219. border-inline-start: 2px solid #333333 !important;
  220. }
  221. body.sepia blockquote {
  222. border-inline-start: 2px solid #5b4636 !important;
  223. }
  224. body.dark blockquote {
  225. border-inline-start: 2px solid #eeeeee !important;
  226. }
  227. .light-button {
  228. color: #333333;
  229. background-color: #ffffff;
  230. }
  231. .dark-button {
  232. color: #eeeeee;
  233. background-color: #1c1b22;
  234. }
  235. .sepia-button {
  236. color: #5b4636;
  237. background-color: #f4ecd8;
  238. }
  239. .auto-button {
  240. text-align: center;
  241. }
  242. @media (prefers-color-scheme: dark) {
  243. .auto-button {
  244. background-color: #1c1b22;
  245. color: #eeeeee;
  246. }
  247. }
  248. @media not (prefers-color-scheme: dark) {
  249. .auto-button {
  250. background-color: #ffffff;
  251. color: #333333;
  252. }
  253. }
  254. /* Loading/error message */
  255. .reader-message {
  256. margin-top: 40px;
  257. display: none;
  258. text-align: center;
  259. width: 100%;
  260. font-size: 0.9em;
  261. }
  262. /* Detector element to see if we're at the top of the doc or not. */
  263. .top-anchor {
  264. position: absolute;
  265. top: 0;
  266. width: 0;
  267. height: 5px;
  268. pointer-events: none;
  269. }
  270. /* Header */
  271. .header {
  272. text-align: start;
  273. display: none;
  274. }
  275. .domain {
  276. font-size: 0.9em;
  277. line-height: 1.48em;
  278. padding-bottom: 4px;
  279. font-family: Helvetica, Arial, sans-serif;
  280. text-decoration: none;
  281. border-bottom: 1px solid;
  282. color: var(--link-foreground);
  283. }
  284. .header > h1 {
  285. font-size: 1.6em;
  286. line-height: 1.25em;
  287. width: 100%;
  288. margin: 30px 0;
  289. padding: 0;
  290. }
  291. .header > .credits {
  292. font-size: 0.9em;
  293. line-height: 1.48em;
  294. margin: 0 0 10px;
  295. padding: 0;
  296. font-style: italic;
  297. }
  298. .header > .meta-data {
  299. font-size: 0.65em;
  300. margin: 0 0 15px;
  301. }
  302. .reader-estimated-time {
  303. text-align: match-parent;
  304. }
  305. /* Controls toolbar */
  306. .toolbar-container {
  307. position: sticky;
  308. z-index: 2;
  309. top: 32px;
  310. height: 0; /* take up no space, so body is at the top. */
  311. /* As a stick container, we're positioned relative to the body. Move us to
  312. * the edge of the viewport using margins, and take the width of
  313. * the body padding into account for calculating our width.
  314. */
  315. margin-inline-start: calc(-1 * var(--body-padding));
  316. width: max(var(--body-padding), calc((100% - var(--content-width)) / 2 + var(--body-padding)));
  317. font-size: var(--font-size); /* Needed to ensure 'em' units match, is reset for .reader-controls */
  318. }
  319. .toolbar {
  320. padding-block: 16px;
  321. border: 1px solid var(--toolbar-border);
  322. border-radius: 6px;
  323. box-shadow: 0 2px 8px var(--toolbar-box-shadow);
  324. width: 32px; /* basic width, without padding/border */
  325. /* padding should be 16px, except if there's not enough space for that, in
  326. * which case use half the available space for padding (=25% on each side).
  327. * The 34px here is the width + borders. We use a variable because we need
  328. * to know this size for the margin calculation.
  329. */
  330. --inline-padding: min(16px, calc(25% - 0.25 * 34px));
  331. padding-inline: var(--inline-padding);
  332. /* Keep a maximum of 96px distance to the body, but center once the margin
  333. * gets too small. We need to set the start margin, however...
  334. * To center, we'd want 50% of the container, but we'd subtract half our
  335. * own width (16px) and half the border (1px) and the inline padding.
  336. * When the other margin would be 96px, we want 100% - 96px - the complete
  337. * width of the actual toolbar (34px + 2 * padding)
  338. */
  339. margin-inline-start: max(calc(50% - 17px - var(--inline-padding)), calc(100% - 96px - 34px - 2 * var(--inline-padding)));
  340. font-family: Helvetica, Arial, sans-serif;
  341. list-style: none;
  342. user-select: none;
  343. }
  344. @media (prefers-reduced-motion: no-preference) {
  345. .toolbar {
  346. transition-property: border-color, box-shadow;
  347. transition-duration: 250ms;
  348. }
  349. .toolbar .toolbar-button {
  350. transition-property: opacity;
  351. transition-duration: 250ms;
  352. }
  353. .toolbar-container.scrolled .toolbar:not(:hover, :focus-within) {
  354. border-color: var(--toolbar-transparent-border);
  355. box-shadow: 0 2px 8px transparent;
  356. }
  357. .toolbar-container.scrolled .toolbar:not(:hover, :focus-within) .toolbar-button {
  358. opacity: 0.6;
  359. }
  360. .toolbar-container.transition-location {
  361. transition-duration: 250ms;
  362. transition-property: width;
  363. }
  364. }
  365. .toolbar-container.overlaps .toolbar-button {
  366. opacity: 0.1;
  367. }
  368. .dropdown-open .toolbar {
  369. border-color: var(--toolbar-transparent-border);
  370. box-shadow: 0 2px 8px transparent;
  371. }
  372. .reader-controls {
  373. /* We use 'em's above this node to get it to the right size. However,
  374. * the UI inside the toolbar should use a fixed, smaller size. */
  375. font-size: 11px;
  376. }
  377. button {
  378. -moz-context-properties: fill;
  379. color: var(--font-color);
  380. fill: var(--icon-fill);
  381. }
  382. button:disabled {
  383. fill: var(--icon-disabled-fill);
  384. }
  385. .toolbar button::-moz-focus-inner {
  386. border: 0;
  387. }
  388. .toolbar-button {
  389. position: relative;
  390. width: 32px;
  391. height: 32px;
  392. padding: 0;
  393. border: 1px solid var(--toolbar-button-border);
  394. border-radius: 4px;
  395. margin: 4px 0;
  396. background-color: var(--toolbar-button-background);
  397. background-size: 16px 16px;
  398. background-position: center;
  399. background-repeat: no-repeat;
  400. }
  401. .toolbar-button:hover,
  402. .toolbar-button:focus-visible {
  403. background-color: var(--toolbar-button-background-hover);
  404. border-color: var(--toolbar-button-border-hover);
  405. fill: var(--toolbar-button-foreground-hover);
  406. }
  407. .open .toolbar-button,
  408. .toolbar-button:hover:active {
  409. background-color: var(--toolbar-button-background-active);
  410. border-color: var(--toolbar-button-border-active);
  411. color: var(--toolbar-button-foreground-active);
  412. fill: var(--toolbar-button-foreground-active);
  413. }
  414. .hover-label {
  415. position: absolute;
  416. top: 4px;
  417. inset-inline-start: 36px;
  418. line-height: 16px;
  419. white-space: pre; /* make sure we don't wrap */
  420. background-color: var(--tooltip-background);
  421. color: var(--tooltip-foreground);
  422. padding: 4px 8px;
  423. border: 1px solid var(--tooltip-border);
  424. border-radius: 2px;
  425. visibility: hidden;
  426. pointer-events: none;
  427. /* Put above .dropdown .dropdown-popup, which has z-index: 1000. */
  428. z-index: 1001;
  429. }
  430. /* Show the hover tooltip on non-dropdown buttons. */
  431. .toolbar-button:not(.dropdown-toggle):hover > .hover-label,
  432. .toolbar-button:not(.dropdown-toggle):focus-visible > .hover-label,
  433. /* Show the hover tooltip for dropdown buttons unless its dropdown is open. */
  434. :not(.open) > li > .dropdown-toggle:hover > .hover-label,
  435. :not(.open) > li > .dropdown-toggle:focus-visible > .hover-label {
  436. visibility: visible;
  437. }
  438. .dropdown {
  439. text-align: center;
  440. list-style: none;
  441. margin: 0;
  442. padding: 0;
  443. position: relative;
  444. }
  445. .dropdown li {
  446. margin: 0;
  447. padding: 0;
  448. }
  449. /* Popup */
  450. .dropdown .dropdown-popup {
  451. text-align: start;
  452. position: absolute;
  453. inset-inline-start: 40px;
  454. z-index: 1000;
  455. background-color: var(--popup-background);
  456. visibility: hidden;
  457. border-radius: 4px;
  458. border: 1px solid var(--popup-border);
  459. box-shadow: 0 0 10px 0 var(--popup-shadow);
  460. top: 0;
  461. }
  462. .open > .dropdown-popup {
  463. visibility: visible;
  464. }
  465. .dropdown-arrow {
  466. position: absolute;
  467. height: 24px;
  468. width: 16px;
  469. inset-inline-start: -16px;
  470. background-image: url("chrome://global/skin/reader/RM-Type-Controls-Arrow.svg");
  471. display: block;
  472. -moz-context-properties: fill, stroke;
  473. fill: var(--popup-background);
  474. stroke: var(--opaque-popup-border);
  475. pointer-events: none;
  476. }
  477. .dropdown-arrow:dir(rtl) {
  478. transform: scaleX(-1);
  479. }
  480. /* Align the style dropdown arrow (narrate does its own) */
  481. .style-dropdown .dropdown-arrow {
  482. top: 7px;
  483. }
  484. /* Font style popup */
  485. .radio-button {
  486. /* We visually hide these, but we keep them around so they can be focused
  487. * and changed by interacting with them via the label, or the keyboard, or
  488. * assistive technology.
  489. */
  490. opacity: 0;
  491. pointer-events: none;
  492. position: absolute;
  493. }
  494. .radiorow,
  495. .buttonrow {
  496. display: flex;
  497. align-content: center;
  498. justify-content: center;
  499. }
  500. .content-width-value,
  501. .font-size-value,
  502. .line-height-value {
  503. box-sizing: border-box;
  504. width: 36px;
  505. height: 20px;
  506. line-height: 20px;
  507. display: flex;
  508. justify-content: center;
  509. align-content: center;
  510. margin: auto;
  511. border-radius: 10px;
  512. border: 1px solid var(--font-value-border);
  513. background-color: var(--popup-button-background);
  514. }
  515. .buttonrow > button {
  516. border: 0;
  517. height: 60px;
  518. width: 90px;
  519. background-color: transparent;
  520. background-repeat: no-repeat;
  521. background-position: center;
  522. }
  523. .buttonrow > button:enabled:hover,
  524. .buttonrow > button:enabled:focus-visible {
  525. background-color: var(--popup-button-background-hover);
  526. color: var(--popup-button-foreground-hover);
  527. fill: var(--popup-button-foreground-hover);
  528. }
  529. .buttonrow > button:enabled:hover:active {
  530. background-color: var(--popup-button-background-active);
  531. }
  532. .radiorow:not(:last-child),
  533. .buttonrow:not(:last-child) {
  534. border-bottom: 1px solid var(--popup-line);
  535. }
  536. body.hcm .buttonrow.line-height-buttons {
  537. /* On HCM the .color-scheme-buttons row is hidden, so remove the border from the row above it */
  538. border-bottom: none;
  539. }
  540. .radiorow > label {
  541. position: relative;
  542. box-sizing: border-box;
  543. border-radius: 2px;
  544. border: 2px solid var(--popup-border);
  545. }
  546. .radiorow > label[checked] {
  547. border-color: var(--selected-border);
  548. }
  549. /* For the hover style, we draw a line under the item by means of a
  550. * pseudo-element. Because these items are variable height, and
  551. * because their contents are variable height, position it absolutely,
  552. * and give it a width of 100% (the content width) + 4px for the 2 * 2px
  553. * border width.
  554. */
  555. .radiorow > input[type=radio]:focus-visible + label::after,
  556. .radiorow > label:hover::after {
  557. content: "";
  558. display: block;
  559. border-bottom: 2px solid var(--selected-border);
  560. width: calc(100% + 4px);
  561. position: absolute;
  562. /* to skip the 2 * 2px border + 2px spacing. */
  563. bottom: -6px;
  564. /* Match the start of the 2px border of the element: */
  565. inset-inline-start: -2px;
  566. }
  567. .font-type-buttons > label {
  568. height: 64px;
  569. width: 105px;
  570. /* Slightly more space between these items. */
  571. margin: 10px;
  572. /* Center the Sans-serif / Serif labels */
  573. text-align: center;
  574. background-size: 63px 39px;
  575. background-repeat: no-repeat;
  576. background-position: center 18px;
  577. background-color: var(--popup-button-background);
  578. fill: currentColor;
  579. -moz-context-properties: fill;
  580. /* This mostly matches baselines, but because of differences in font
  581. * baselines between serif and sans-serif, this isn't always enough. */
  582. line-height: 1;
  583. padding-top: 2px;
  584. }
  585. .font-type-buttons > label[checked] {
  586. background-color: var(--selected-background);
  587. }
  588. .sans-serif-button {
  589. font-family: Helvetica, Arial, sans-serif;
  590. background-image: url("chrome://global/skin/reader/RM-Sans-Serif.svg");
  591. }
  592. /* Tweak padding to match the baseline on mac */
  593. :root[platform=macosx] .sans-serif-button {
  594. padding-top: 3px;
  595. }
  596. .serif-button {
  597. font-family: Georgia, "Times New Roman", serif;
  598. background-image: url("chrome://global/skin/reader/RM-Serif.svg");
  599. }
  600. body.hcm .color-scheme-buttons {
  601. /* Disallow selecting themes when HCM is on. */
  602. display: none;
  603. }
  604. .color-scheme-buttons > label {
  605. padding: 12px;
  606. height: 34px;
  607. font-size: 12px;
  608. /* Center the labels horizontally as well as vertically */
  609. display: inline-flex;
  610. align-items: center;
  611. justify-content: center;
  612. /* We want 10px between items, but there's no margin collapsing in flexbox. */
  613. margin: 10px 5px;
  614. }
  615. .color-scheme-buttons > input:first-child + label {
  616. margin-inline-start: 10px;
  617. }
  618. .color-scheme-buttons > label:last-child {
  619. margin-inline-end: 10px;
  620. }
  621. /* Toolbar icons */
  622. .close-button {
  623. background-image: url("chrome://global/skin/icons/close.svg");
  624. }
  625. .style-button {
  626. background-image: url("chrome://global/skin/reader/RM-Type-Controls-24x24.svg");
  627. }
  628. .minus-button {
  629. background-size: 18px 18px;
  630. background-image: url("chrome://global/skin/reader/RM-Minus-24x24.svg");
  631. }
  632. .plus-button {
  633. background-size: 18px 18px;
  634. background-image: url("chrome://global/skin/reader/RM-Plus-24x24.svg");
  635. }
  636. .content-width-minus-button {
  637. background-size: 42px 16px;
  638. background-image: url("chrome://global/skin/reader/RM-Content-Width-Minus-42x16.svg");
  639. }
  640. .content-width-plus-button {
  641. background-size: 44px 16px;
  642. background-image: url("chrome://global/skin/reader/RM-Content-Width-Plus-44x16.svg");
  643. }
  644. .line-height-minus-button {
  645. background-size: 34px 14px;
  646. background-image: url("chrome://global/skin/reader/RM-Line-Height-Minus-38x14.svg");
  647. }
  648. .line-height-plus-button {
  649. background-size: 34px 24px;
  650. background-image: url("chrome://global/skin/reader/RM-Line-Height-Plus-38x24.svg");
  651. }
  652. /* Mirror the line height buttons if the article is RTL. */
  653. .reader-controls[articledir="rtl"] .line-height-minus-button,
  654. .reader-controls[articledir="rtl"] .line-height-plus-button {
  655. transform: scaleX(-1);
  656. }
  657. @media print {
  658. .toolbar {
  659. display: none !important;
  660. }
  661. }
  662. /* Article content */
  663. /* Note that any class names from the original article that we want to match on
  664. * must be added to CLASSES_TO_PRESERVE in ReaderMode.jsm, so that
  665. * Readability.js doesn't strip them out */
  666. .container {
  667. margin: 0 auto;
  668. font-size: var(--font-size);
  669. max-width: var(--content-width);
  670. line-height: var(--line-height);
  671. }
  672. pre {
  673. font-family: inherit;
  674. }
  675. .moz-reader-content {
  676. display: none;
  677. font-size: 1em;
  678. }
  679. @media print {
  680. .moz-reader-content p,
  681. .moz-reader-content code,
  682. .moz-reader-content pre,
  683. .moz-reader-content blockquote,
  684. .moz-reader-content ul,
  685. .moz-reader-content ol,
  686. .moz-reader-content li,
  687. .moz-reader-content figure,
  688. .moz-reader-content .wp-caption {
  689. margin: 0 0 10px !important;
  690. padding: 0 !important;
  691. }
  692. }
  693. .moz-reader-content h1,
  694. .moz-reader-content h2,
  695. .moz-reader-content h3 {
  696. font-weight: bold;
  697. }
  698. .moz-reader-content h1 {
  699. font-size: 1.6em;
  700. line-height: 1.25em;
  701. }
  702. .moz-reader-content h2 {
  703. font-size: 1.2em;
  704. line-height: 1.51em;
  705. }
  706. .moz-reader-content h3 {
  707. font-size: 1em;
  708. line-height: 1.66em;
  709. }
  710. .moz-reader-content a:link {
  711. text-decoration: underline;
  712. font-weight: normal;
  713. }
  714. .moz-reader-content a:link,
  715. .moz-reader-content a:link:hover,
  716. .moz-reader-content a:link:active {
  717. color: var(--link-foreground);
  718. }
  719. .moz-reader-content a:visited {
  720. color: var(--visited-link-foreground);
  721. }
  722. .moz-reader-content * {
  723. max-width: 100%;
  724. height: auto;
  725. }
  726. .moz-reader-content p,
  727. .moz-reader-content p,
  728. .moz-reader-content code,
  729. .moz-reader-content pre,
  730. .moz-reader-content blockquote,
  731. .moz-reader-content ul,
  732. .moz-reader-content ol,
  733. .moz-reader-content li,
  734. .moz-reader-content figure,
  735. .moz-reader-content .wp-caption {
  736. margin: -10px -10px 20px;
  737. padding: 10px;
  738. border-radius: 5px;
  739. }
  740. .moz-reader-content li {
  741. margin-bottom: 0;
  742. }
  743. .moz-reader-content li > ul,
  744. .moz-reader-content li > ol {
  745. margin-bottom: -10px;
  746. }
  747. .moz-reader-content p > img:only-child,
  748. .moz-reader-content p > a:only-child > img:only-child,
  749. .moz-reader-content .wp-caption img,
  750. .moz-reader-content figure img {
  751. display: block;
  752. }
  753. .moz-reader-content img[moz-reader-center] {
  754. margin-inline: auto;
  755. }
  756. .moz-reader-content .caption,
  757. .moz-reader-content .wp-caption-text
  758. .moz-reader-content figcaption {
  759. font-size: 0.9em;
  760. line-height: 1.48em;
  761. font-style: italic;
  762. }
  763. .moz-reader-content pre {
  764. white-space: pre-wrap;
  765. }
  766. .moz-reader-content blockquote {
  767. padding: 0;
  768. padding-inline-start: 16px;
  769. }
  770. .moz-reader-content ul,
  771. .moz-reader-content ol {
  772. padding: 0;
  773. }
  774. .moz-reader-content ul {
  775. padding-inline-start: 30px;
  776. list-style: disc;
  777. }
  778. .moz-reader-content ol {
  779. padding-inline-start: 30px;
  780. }
  781. table,
  782. th,
  783. td {
  784. border: 1px solid currentColor;
  785. border-collapse: collapse;
  786. padding: 6px;
  787. vertical-align: top;
  788. }
  789. table {
  790. margin: 5px;
  791. }
  792. /* Visually hide (but don't display: none) screen reader elements */
  793. .moz-reader-content .visually-hidden,
  794. .moz-reader-content .visuallyhidden,
  795. .moz-reader-content .sr-only {
  796. display: inline-block;
  797. width: 1px;
  798. height: 1px;
  799. margin: -1px;
  800. overflow: hidden;
  801. padding: 0;
  802. border-width: 0;
  803. }
  804. /* Hide elements with common "hidden" class names */
  805. .moz-reader-content .hidden,
  806. .moz-reader-content .invisible {
  807. display: none;
  808. }
  809. /* Enforce wordpress and similar emoji/smileys aren't sized to be full-width,
  810. * see bug 1399616 for context. */
  811. .moz-reader-content img.wp-smiley,
  812. .moz-reader-content img.emoji {
  813. display: inline-block;
  814. border-width: 0;
  815. /* height: auto is implied from '.moz-reader-content *' rule. */
  816. width: 1em;
  817. margin: 0 .07em;
  818. padding: 0;
  819. }
  820. .reader-show-element {
  821. display: initial;
  822. }
  823. /* Provide extra spacing for images that may be aided with accompanying element such as <figcaption> */
  824. .moz-reader-block-img:not(:last-child) {
  825. margin-block-end: 12px;
  826. }
  827. .moz-reader-wide-table {
  828. overflow-x: auto;
  829. display: block;
  830. }
  831. pre code {
  832. background-color: var(--main-background);
  833. border: 1px solid var(--font-color);
  834. display: block;
  835. overflow: auto;
  836. }`;
  837. let NOTES_WEB_STYLESHEET, MASK_WEB_STYLESHEET, HIGHLIGHTS_WEB_STYLESHEET;
  838. let selectedNote, anchorElement, maskNoteElement, maskPageElement, highlightSelectionMode, removeHighlightMode, resizingNoteMode, movingNoteMode, highlightColor, collapseNoteTimeout, cuttingOuterMode, cuttingMode, cuttingPath, cuttingPathIndex, previousContent;
  839. let removedElements = [], removedElementIndex = 0, initScriptContent;
  840. window.onmessage = async event => {
  841. const message = JSON.parse(event.data);
  842. if (message.method == "init") {
  843. await init(message.content);
  844. }
  845. if (message.method == "addNote") {
  846. addNote(message);
  847. }
  848. if (message.method == "displayNotes") {
  849. document.querySelectorAll(NOTE_TAGNAME).forEach(noteElement => noteElement.shadowRoot.querySelector("." + NOTE_CLASS).classList.remove(NOTE_HIDDEN_CLASS));
  850. }
  851. if (message.method == "hideNotes") {
  852. document.querySelectorAll(NOTE_TAGNAME).forEach(noteElement => noteElement.shadowRoot.querySelector("." + NOTE_CLASS).classList.add(NOTE_HIDDEN_CLASS));
  853. }
  854. if (message.method == "enableHighlight") {
  855. if (highlightColor) {
  856. document.documentElement.classList.remove(highlightColor + "-mode");
  857. }
  858. highlightColor = message.color;
  859. highlightSelectionMode = true;
  860. document.documentElement.classList.add(message.color + "-mode");
  861. }
  862. if (message.method == "disableHighlight") {
  863. if (highlightColor) {
  864. document.documentElement.classList.remove(highlightColor + "-mode");
  865. }
  866. highlightSelectionMode = false;
  867. }
  868. if (message.method == "displayHighlights") {
  869. document.querySelectorAll("." + HIGHLIGHT_CLASS).forEach(noteElement => noteElement.classList.remove(HIGHLIGHT_HIDDEN_CLASS));
  870. }
  871. if (message.method == "hideHighlights") {
  872. document.querySelectorAll("." + HIGHLIGHT_CLASS).forEach(noteElement => noteElement.classList.add(HIGHLIGHT_HIDDEN_CLASS));
  873. }
  874. if (message.method == "enableRemoveHighlights") {
  875. removeHighlightMode = true;
  876. document.documentElement.classList.add("single-file-remove-highlights-mode");
  877. }
  878. if (message.method == "disableRemoveHighlights") {
  879. removeHighlightMode = false;
  880. document.documentElement.classList.remove("single-file-remove-highlights-mode");
  881. }
  882. if (message.method == "enableEditPage") {
  883. document.body.contentEditable = true;
  884. onUpdate(false);
  885. }
  886. if (message.method == "formatPage") {
  887. formatPage(true);
  888. }
  889. if (message.method == "formatPageNoTheme") {
  890. formatPage(false);
  891. }
  892. if (message.method == "cancelFormatPage") {
  893. cancelFormatPage();
  894. }
  895. if (message.method == "disableEditPage") {
  896. document.body.contentEditable = false;
  897. }
  898. if (message.method == "enableCutInnerPage") {
  899. cuttingMode = true;
  900. document.documentElement.classList.add("single-file-cut-mode");
  901. }
  902. if (message.method == "enableCutOuterPage") {
  903. cuttingOuterMode = true;
  904. document.documentElement.classList.add("single-file-cut-mode");
  905. }
  906. if (message.method == "disableCutInnerPage") {
  907. cuttingMode = false;
  908. document.documentElement.classList.remove("single-file-cut-mode");
  909. resetSelectedElements();
  910. if (cuttingPath) {
  911. unhighlightCutElement();
  912. cuttingPath = null;
  913. }
  914. }
  915. if (message.method == "disableCutOuterPage") {
  916. cuttingOuterMode = false;
  917. document.documentElement.classList.remove("single-file-cut-mode");
  918. resetSelectedElements();
  919. if (cuttingPath) {
  920. unhighlightCutElement();
  921. cuttingPath = null;
  922. }
  923. }
  924. if (message.method == "undoCutPage") {
  925. undoCutPage();
  926. }
  927. if (message.method == "undoAllCutPage") {
  928. while (removedElementIndex) {
  929. removedElements[removedElementIndex - 1].forEach(element => element.classList.remove(REMOVED_CONTENT_CLASS));
  930. removedElementIndex--;
  931. }
  932. }
  933. if (message.method == "redoCutPage") {
  934. redoCutPage();
  935. }
  936. if (message.method == "getContent") {
  937. onUpdate(true);
  938. let content = getContent(message.compressHTML, message.updatedResources);
  939. if (initScriptContent) {
  940. content = content.replace(/<script data-template-shadow-root src.*?<\/script>/g, initScriptContent);
  941. }
  942. window.parent.postMessage(JSON.stringify({ method: "setContent", content }), "*");
  943. }
  944. if (message.method == "printPage") {
  945. printPage();
  946. }
  947. };
  948. window.onresize = reflowNotes;
  949. document.ondragover = event => event.preventDefault();
  950. document.ondrop = async event => {
  951. if (event.dataTransfer.files && event.dataTransfer.files[0]) {
  952. const file = event.dataTransfer.files[0];
  953. event.preventDefault();
  954. const content = new TextDecoder().decode(await file.arrayBuffer());
  955. await init(content, { filename: file.name });
  956. }
  957. };
  958. async function init(content, { filename, reset } = {}) {
  959. await initConstants();
  960. const initScriptContentMatch = content.match(/<script data-template-shadow-root.*<\/script>/);
  961. if (initScriptContentMatch && initScriptContentMatch[0]) {
  962. initScriptContent = initScriptContentMatch[0];
  963. }
  964. content = content.replace(/<script data-template-shadow-root.*<\/script>/g, "<script data-template-shadow-root src=/lib/single-file-extension-editor-init.js></script>");
  965. const contentDocument = (new DOMParser()).parseFromString(content, "text/html");
  966. if (detectSavedPage(contentDocument)) {
  967. if (contentDocument.doctype) {
  968. if (document.doctype) {
  969. document.replaceChild(contentDocument.doctype, document.doctype);
  970. } else {
  971. document.insertBefore(contentDocument.doctype, document.documentElement);
  972. }
  973. } else if (document.doctype) {
  974. document.doctype.remove();
  975. }
  976. contentDocument.querySelectorAll("noscript").forEach(element => {
  977. element.setAttribute(DISABLED_NOSCRIPT_ATTRIBUTE_NAME, element.innerHTML);
  978. element.textContent = "";
  979. });
  980. contentDocument.querySelectorAll("iframe").forEach(element => {
  981. const pointerEvents = "pointer-events";
  982. element.style.setProperty("-sf-" + pointerEvents, element.style.getPropertyValue(pointerEvents), element.style.getPropertyPriority(pointerEvents));
  983. element.style.setProperty(pointerEvents, "none", "important");
  984. });
  985. document.replaceChild(contentDocument.documentElement, document.documentElement);
  986. deserializeShadowRoots(document);
  987. document.querySelectorAll(NOTE_TAGNAME).forEach(containerElement => attachNoteListeners(containerElement, true));
  988. document.documentElement.appendChild(getStyleElement(HIGHLIGHTS_WEB_STYLESHEET));
  989. maskPageElement = getMaskElement(PAGE_MASK_CLASS, PAGE_MASK_CONTAINER_CLASS);
  990. maskNoteElement = getMaskElement(NOTE_MASK_CLASS);
  991. document.documentElement.onmousedown = document.documentElement.ontouchstart = onMouseDown;
  992. document.documentElement.onmouseup = document.documentElement.ontouchend = onMouseUp;
  993. document.documentElement.onmouseover = onMouseOver;
  994. document.documentElement.onmouseout = onMouseOut;
  995. document.documentElement.onkeydown = onKeyDown;
  996. window.onclick = event => event.preventDefault();
  997. const iconElement = document.querySelector("link[rel*=icon]");
  998. window.parent.postMessage(JSON.stringify({
  999. method: "onInit",
  1000. title: document.title,
  1001. icon: iconElement && iconElement.href,
  1002. filename,
  1003. reset,
  1004. formatPageEnabled: isProbablyReaderable(document)
  1005. }), "*");
  1006. }
  1007. }
  1008. async function initConstants() {
  1009. [NOTES_WEB_STYLESHEET, MASK_WEB_STYLESHEET, HIGHLIGHTS_WEB_STYLESHEET] = await Promise.all([
  1010. minifyText(await ((await fetch("../pages/editor-note-web.css")).text())),
  1011. minifyText(await ((await fetch("../pages/editor-mask-web.css")).text())),
  1012. minifyText(await ((await fetch("../pages/editor-frame-web.css")).text()))
  1013. ]);
  1014. }
  1015. function addNote({ color }) {
  1016. const containerElement = document.createElement(NOTE_TAGNAME);
  1017. const noteElement = document.createElement("div");
  1018. const headerElement = document.createElement("header");
  1019. const mainElement = document.createElement("textarea");
  1020. const resizeElement = document.createElement("div");
  1021. const removeNoteElement = document.createElement("img");
  1022. const anchorIconElement = document.createElement("img");
  1023. const noteShadow = containerElement.attachShadow({ mode: "open" });
  1024. headerElement.appendChild(anchorIconElement);
  1025. headerElement.appendChild(removeNoteElement);
  1026. noteElement.appendChild(headerElement);
  1027. noteElement.appendChild(mainElement);
  1028. noteElement.appendChild(resizeElement);
  1029. noteShadow.appendChild(getStyleElement(NOTES_WEB_STYLESHEET));
  1030. noteShadow.appendChild(noteElement);
  1031. const notesElements = Array.from(document.querySelectorAll(NOTE_TAGNAME));
  1032. const noteId = Math.max.call(Math, 0, ...notesElements.map(noteElement => Number(noteElement.dataset.noteId))) + 1;
  1033. noteElement.classList.add(NOTE_CLASS);
  1034. noteElement.classList.add(NOTE_ANCHORED_CLASS);
  1035. noteElement.classList.add(color);
  1036. noteElement.dataset.color = color;
  1037. mainElement.dir = "auto";
  1038. const boundingRectDocument = document.documentElement.getBoundingClientRect();
  1039. let positionX = NOTE_INITIAL_WIDTH + NOTE_INITIAL_POSITION_X - 1 - boundingRectDocument.x;
  1040. let positionY = NOTE_INITIAL_HEIGHT + NOTE_INITIAL_POSITION_Y - 1 - boundingRectDocument.y;
  1041. while (Array.from(document.elementsFromPoint(positionX - window.scrollX, positionY - window.scrollY)).find(element => element.tagName.toLowerCase() == NOTE_TAGNAME)) {
  1042. positionX += NOTE_INITIAL_POSITION_X;
  1043. positionY += NOTE_INITIAL_POSITION_Y;
  1044. }
  1045. noteElement.style.setProperty("left", (positionX - NOTE_INITIAL_WIDTH - 1) + "px");
  1046. noteElement.style.setProperty("top", (positionY - NOTE_INITIAL_HEIGHT - 1) + "px");
  1047. resizeElement.className = "note-resize";
  1048. resizeElement.ondragstart = event => event.preventDefault();
  1049. removeNoteElement.className = "note-remove";
  1050. removeNoteElement.src = BUTTON_CLOSE_URL;
  1051. removeNoteElement.ondragstart = event => event.preventDefault();
  1052. anchorIconElement.className = "note-anchor";
  1053. anchorIconElement.src = BUTTON_ANCHOR_URL;
  1054. anchorIconElement.ondragstart = event => event.preventDefault();
  1055. containerElement.dataset.noteId = noteId;
  1056. addNoteRef(document.documentElement, noteId);
  1057. attachNoteListeners(containerElement, true);
  1058. document.documentElement.insertBefore(containerElement, maskPageElement.getRootNode().host);
  1059. noteElement.classList.add(NOTE_SELECTED_CLASS);
  1060. selectedNote = noteElement;
  1061. onUpdate(false);
  1062. }
  1063. function attachNoteListeners(containerElement, editable = false) {
  1064. const SELECT_PX_THRESHOLD = 4;
  1065. const COLLAPSING_NOTE_DELAY = 750;
  1066. const noteShadow = containerElement.shadowRoot;
  1067. const noteElement = noteShadow.childNodes[1];
  1068. const headerElement = noteShadow.querySelector("header");
  1069. const mainElement = noteShadow.querySelector("textarea");
  1070. const noteId = containerElement.dataset.noteId;
  1071. const resizeElement = noteShadow.querySelector(".note-resize");
  1072. const anchorIconElement = noteShadow.querySelector(".note-anchor");
  1073. const removeNoteElement = noteShadow.querySelector(".note-remove");
  1074. mainElement.readOnly = !editable;
  1075. if (!editable) {
  1076. anchorIconElement.style.setProperty("display", "none", "important");
  1077. } else {
  1078. anchorIconElement.style.removeProperty("display");
  1079. }
  1080. headerElement.ontouchstart = headerElement.onmousedown = event => {
  1081. if (event.target == headerElement) {
  1082. collapseNoteTimeout = setTimeout(() => {
  1083. noteElement.classList.toggle("note-collapsed");
  1084. hideMaskNote();
  1085. }, COLLAPSING_NOTE_DELAY);
  1086. event.preventDefault();
  1087. const position = getPosition(event);
  1088. const clientX = position.clientX;
  1089. const clientY = position.clientY;
  1090. const boundingRect = noteElement.getBoundingClientRect();
  1091. const deltaX = clientX - boundingRect.left;
  1092. const deltaY = clientY - boundingRect.top;
  1093. maskPageElement.classList.add(PAGE_MASK_ACTIVE_CLASS);
  1094. document.documentElement.style.setProperty("user-select", "none", "important");
  1095. anchorElement = getAnchorElement(containerElement);
  1096. displayMaskNote();
  1097. selectNote(noteElement);
  1098. moveNote(event, deltaX, deltaY);
  1099. movingNoteMode = { event, deltaX, deltaY };
  1100. document.documentElement.ontouchmove = document.documentElement.onmousemove = event => {
  1101. clearTimeout(collapseNoteTimeout);
  1102. if (!movingNoteMode) {
  1103. movingNoteMode = { deltaX, deltaY };
  1104. }
  1105. movingNoteMode.event = event;
  1106. moveNote(event, deltaX, deltaY);
  1107. };
  1108. }
  1109. };
  1110. resizeElement.ontouchstart = resizeElement.onmousedown = event => {
  1111. event.preventDefault();
  1112. resizingNoteMode = true;
  1113. selectNote(noteElement);
  1114. maskPageElement.classList.add(PAGE_MASK_ACTIVE_CLASS);
  1115. document.documentElement.style.setProperty("user-select", "none", "important");
  1116. document.documentElement.ontouchmove = document.documentElement.onmousemove = event => {
  1117. event.preventDefault();
  1118. const { clientX, clientY } = getPosition(event);
  1119. const boundingRectNote = noteElement.getBoundingClientRect();
  1120. noteElement.style.width = clientX - boundingRectNote.left + "px";
  1121. noteElement.style.height = clientY - boundingRectNote.top + "px";
  1122. };
  1123. };
  1124. anchorIconElement.ontouchend = anchorIconElement.onclick = event => {
  1125. event.preventDefault();
  1126. noteElement.classList.toggle(NOTE_ANCHORED_CLASS);
  1127. if (!noteElement.classList.contains(NOTE_ANCHORED_CLASS)) {
  1128. deleteNoteRef(containerElement, noteId);
  1129. addNoteRef(document.documentElement, noteId);
  1130. }
  1131. onUpdate(false);
  1132. };
  1133. removeNoteElement.ontouchend = removeNoteElement.onclick = event => {
  1134. event.preventDefault();
  1135. deleteNoteRef(containerElement, noteId);
  1136. containerElement.remove();
  1137. };
  1138. noteElement.onmousedown = () => {
  1139. selectNote(noteElement);
  1140. };
  1141. function moveNote(event, deltaX, deltaY) {
  1142. event.preventDefault();
  1143. const { clientX, clientY } = getPosition(event);
  1144. noteElement.classList.add(NOTE_MOVING_CLASS);
  1145. if (editable) {
  1146. if (noteElement.classList.contains(NOTE_ANCHORED_CLASS)) {
  1147. deleteNoteRef(containerElement, noteId);
  1148. anchorElement = getTarget(clientX, clientY) || document.documentElement;
  1149. addNoteRef(anchorElement, noteId);
  1150. } else {
  1151. anchorElement = document.documentElement;
  1152. }
  1153. }
  1154. document.documentElement.insertBefore(containerElement, maskPageElement.getRootNode().host);
  1155. noteElement.style.setProperty("left", (clientX - deltaX) + "px");
  1156. noteElement.style.setProperty("top", (clientY - deltaY) + "px");
  1157. noteElement.style.setProperty("position", "fixed");
  1158. displayMaskNote();
  1159. }
  1160. function displayMaskNote() {
  1161. if (anchorElement == document.documentElement || anchorElement == document.documentElement) {
  1162. hideMaskNote();
  1163. } else {
  1164. const boundingRectAnchor = anchorElement.getBoundingClientRect();
  1165. maskNoteElement.classList.add(NOTE_MASK_MOVING_CLASS);
  1166. if (selectedNote) {
  1167. maskNoteElement.classList.add(selectedNote.dataset.color);
  1168. }
  1169. maskNoteElement.style.setProperty("top", (boundingRectAnchor.y - 3) + "px");
  1170. maskNoteElement.style.setProperty("left", (boundingRectAnchor.x - 3) + "px");
  1171. maskNoteElement.style.setProperty("width", (boundingRectAnchor.width + 3) + "px");
  1172. maskNoteElement.style.setProperty("height", (boundingRectAnchor.height + 3) + "px");
  1173. }
  1174. }
  1175. function hideMaskNote() {
  1176. maskNoteElement.classList.remove(NOTE_MASK_MOVING_CLASS);
  1177. if (selectedNote) {
  1178. maskNoteElement.classList.remove(selectedNote.dataset.color);
  1179. }
  1180. }
  1181. function selectNote(noteElement) {
  1182. if (selectedNote) {
  1183. selectedNote.classList.remove(NOTE_SELECTED_CLASS);
  1184. maskNoteElement.classList.remove(selectedNote.dataset.color);
  1185. }
  1186. noteElement.classList.add(NOTE_SELECTED_CLASS);
  1187. noteElement.classList.add(noteElement.dataset.color);
  1188. selectedNote = noteElement;
  1189. }
  1190. function getTarget(clientX, clientY) {
  1191. const targets = Array.from(document.elementsFromPoint(clientX, clientY)).filter(element => element.matches("html *:not(" + NOTE_TAGNAME + "):not(." + MASK_CLASS + ")"));
  1192. if (!targets.includes(document.documentElement)) {
  1193. targets.push(document.documentElement);
  1194. }
  1195. let newTarget, target = targets[0], boundingRect = target.getBoundingClientRect();
  1196. newTarget = determineTargetElement("floor", target, clientX - boundingRect.left, getMatchedParents(target, "left"));
  1197. if (newTarget == target) {
  1198. newTarget = determineTargetElement("ceil", target, boundingRect.left + boundingRect.width - clientX, getMatchedParents(target, "right"));
  1199. }
  1200. if (newTarget == target) {
  1201. newTarget = determineTargetElement("floor", target, clientY - boundingRect.top, getMatchedParents(target, "top"));
  1202. }
  1203. if (newTarget == target) {
  1204. newTarget = determineTargetElement("ceil", target, boundingRect.top + boundingRect.height - clientY, getMatchedParents(target, "bottom"));
  1205. }
  1206. target = newTarget;
  1207. while (boundingRect = target && target.getBoundingClientRect(), boundingRect && boundingRect.width <= SELECT_PX_THRESHOLD && boundingRect.height <= SELECT_PX_THRESHOLD) {
  1208. target = target.parentElement;
  1209. }
  1210. return target;
  1211. }
  1212. function getMatchedParents(target, property) {
  1213. let element = target, matchedParent, parents = [];
  1214. do {
  1215. const boundingRect = element.getBoundingClientRect();
  1216. if (element.parentElement && !element.parentElement.tagName.toLowerCase() != NOTE_TAGNAME && !element.classList.contains(MASK_CLASS)) {
  1217. const parentBoundingRect = element.parentElement.getBoundingClientRect();
  1218. matchedParent = Math.abs(parentBoundingRect[property] - boundingRect[property]) <= SELECT_PX_THRESHOLD;
  1219. if (matchedParent) {
  1220. if (element.parentElement.clientWidth > SELECT_PX_THRESHOLD && element.parentElement.clientHeight > SELECT_PX_THRESHOLD &&
  1221. ((element.parentElement.clientWidth - element.clientWidth > SELECT_PX_THRESHOLD) || (element.parentElement.clientHeight - element.clientHeight > SELECT_PX_THRESHOLD))) {
  1222. parents.push(element.parentElement);
  1223. }
  1224. element = element.parentElement;
  1225. }
  1226. } else {
  1227. matchedParent = false;
  1228. }
  1229. } while (matchedParent && element);
  1230. return parents;
  1231. }
  1232. function determineTargetElement(roundingMethod, target, widthDistance, parents) {
  1233. if (Math[roundingMethod](widthDistance / SELECT_PX_THRESHOLD) <= parents.length) {
  1234. target = parents[parents.length - Math[roundingMethod](widthDistance / SELECT_PX_THRESHOLD) - 1];
  1235. }
  1236. return target;
  1237. }
  1238. }
  1239. function onMouseDown(event) {
  1240. if ((cuttingMode || cuttingOuterMode) && cuttingPath) {
  1241. event.preventDefault();
  1242. }
  1243. }
  1244. function onMouseUp(event) {
  1245. if (highlightSelectionMode) {
  1246. highlightSelection();
  1247. onUpdate(false);
  1248. }
  1249. if (removeHighlightMode) {
  1250. let element = event.target, done;
  1251. while (element && !done) {
  1252. if (element.classList.contains(HIGHLIGHT_CLASS)) {
  1253. document.querySelectorAll("." + HIGHLIGHT_CLASS + "[data-singlefile-highlight-id=" + JSON.stringify(element.dataset.singlefileHighlightId) + "]").forEach(highlightedElement => {
  1254. resetHighlightedElement(highlightedElement);
  1255. onUpdate(false);
  1256. });
  1257. done = true;
  1258. }
  1259. element = element.parentElement;
  1260. }
  1261. }
  1262. if (resizingNoteMode) {
  1263. resizingNoteMode = false;
  1264. document.documentElement.style.removeProperty("user-select");
  1265. maskPageElement.classList.remove(PAGE_MASK_ACTIVE_CLASS);
  1266. document.documentElement.ontouchmove = document.documentElement.onmousemove = null;
  1267. onUpdate(false);
  1268. }
  1269. if (movingNoteMode) {
  1270. anchorNote(movingNoteMode.event || event, selectedNote, movingNoteMode.deltaX, movingNoteMode.deltaY);
  1271. movingNoteMode = null;
  1272. document.documentElement.ontouchmove = document.documentElement.onmousemove = null;
  1273. onUpdate(false);
  1274. }
  1275. if (collapseNoteTimeout) {
  1276. clearTimeout(collapseNoteTimeout);
  1277. collapseNoteTimeout = null;
  1278. }
  1279. if ((cuttingMode || cuttingOuterMode) && cuttingPath) {
  1280. if (event.ctrlKey) {
  1281. const element = cuttingPath[cuttingPathIndex];
  1282. element.classList.toggle(cuttingMode ? CUT_SELECTED_CLASS : CUT_OUTER_SELECTED_CLASS);
  1283. } else {
  1284. validateCutElement(event.shiftKey);
  1285. }
  1286. }
  1287. }
  1288. function onMouseOver(event) {
  1289. if (cuttingMode || cuttingOuterMode) {
  1290. const target = event.target;
  1291. if (target.classList) {
  1292. let ancestorFound;
  1293. document.querySelectorAll("." + (cuttingMode ? CUT_SELECTED_CLASS : CUT_OUTER_SELECTED_CLASS)).forEach(element => {
  1294. if (element == target || isAncestor(element, target) || isAncestor(target, element)) {
  1295. ancestorFound = element;
  1296. }
  1297. });
  1298. if (ancestorFound) {
  1299. cuttingPath = [ancestorFound];
  1300. } else {
  1301. cuttingPath = getParents(event.target);
  1302. }
  1303. cuttingPathIndex = 0;
  1304. highlightCutElement();
  1305. }
  1306. }
  1307. }
  1308. function onMouseOut() {
  1309. if (cuttingMode || cuttingOuterMode) {
  1310. if (cuttingPath) {
  1311. unhighlightCutElement();
  1312. cuttingPath = null;
  1313. }
  1314. }
  1315. }
  1316. function onKeyDown(event) {
  1317. if (cuttingMode || cuttingOuterMode) {
  1318. if (event.code == "Tab") {
  1319. if (cuttingPath) {
  1320. const delta = event.shiftKey ? -1 : 1;
  1321. let element = cuttingPath[cuttingPathIndex];
  1322. let nextElement = cuttingPath[cuttingPathIndex + delta];
  1323. if (nextElement) {
  1324. let pathIndex = cuttingPathIndex + delta;
  1325. while (
  1326. nextElement &&
  1327. (
  1328. (delta == 1 &&
  1329. element.getBoundingClientRect().width >= nextElement.getBoundingClientRect().width &&
  1330. element.getBoundingClientRect().height >= nextElement.getBoundingClientRect().height) ||
  1331. (delta == -1 &&
  1332. element.getBoundingClientRect().width <= nextElement.getBoundingClientRect().width &&
  1333. element.getBoundingClientRect().height <= nextElement.getBoundingClientRect().height))) {
  1334. pathIndex += delta;
  1335. nextElement = cuttingPath[pathIndex];
  1336. }
  1337. if (nextElement && nextElement.classList && nextElement != document.body && nextElement != document.documentElement) {
  1338. unhighlightCutElement();
  1339. cuttingPathIndex = pathIndex;
  1340. highlightCutElement();
  1341. }
  1342. }
  1343. }
  1344. event.preventDefault();
  1345. }
  1346. if (event.code == "Space") {
  1347. if (cuttingPath) {
  1348. if (event.ctrlKey) {
  1349. const element = cuttingPath[cuttingPathIndex];
  1350. element.classList.add(cuttingMode ? CUT_SELECTED_CLASS : CUT_OUTER_SELECTED_CLASS);
  1351. } else {
  1352. validateCutElement(event.shiftKey);
  1353. }
  1354. event.preventDefault();
  1355. }
  1356. }
  1357. if (event.code == "Escape") {
  1358. resetSelectedElements();
  1359. event.preventDefault();
  1360. }
  1361. if (event.key.toLowerCase() == "z" && event.ctrlKey) {
  1362. if (event.shiftKey) {
  1363. redoCutPage();
  1364. } else {
  1365. undoCutPage();
  1366. }
  1367. event.preventDefault();
  1368. }
  1369. }
  1370. if (event.key.toLowerCase() == "s" && event.ctrlKey) {
  1371. window.parent.postMessage(JSON.stringify({ "method": "savePage" }), "*");
  1372. event.preventDefault();
  1373. }
  1374. if (event.key.toLowerCase() == "p" && event.ctrlKey) {
  1375. printPage();
  1376. event.preventDefault();
  1377. }
  1378. }
  1379. function printPage() {
  1380. unhighlightCutElement();
  1381. resetSelectedElements();
  1382. window.print();
  1383. }
  1384. function highlightCutElement() {
  1385. const element = cuttingPath[cuttingPathIndex];
  1386. element.classList.add(cuttingMode ? CUT_HOVER_CLASS : CUT_OUTER_HOVER_CLASS);
  1387. }
  1388. function unhighlightCutElement() {
  1389. if (cuttingPath) {
  1390. const element = cuttingPath[cuttingPathIndex];
  1391. element.classList.remove(CUT_HOVER_CLASS);
  1392. element.classList.remove(CUT_OUTER_HOVER_CLASS);
  1393. }
  1394. }
  1395. function undoCutPage() {
  1396. if (removedElementIndex) {
  1397. removedElements[removedElementIndex - 1].forEach(element => element.classList.remove(REMOVED_CONTENT_CLASS));
  1398. removedElementIndex--;
  1399. }
  1400. }
  1401. function redoCutPage() {
  1402. if (removedElementIndex < removedElements.length) {
  1403. removedElements[removedElementIndex].forEach(element => element.classList.add(REMOVED_CONTENT_CLASS));
  1404. removedElementIndex++;
  1405. }
  1406. }
  1407. function validateCutElement(invert) {
  1408. const selectedElement = cuttingPath[cuttingPathIndex];
  1409. if ((cuttingMode && !invert) || (cuttingOuterMode && invert)) {
  1410. if (document.documentElement != selectedElement && selectedElement.tagName.toLowerCase() != NOTE_TAGNAME) {
  1411. const elementsRemoved = [selectedElement].concat(...document.querySelectorAll("." + CUT_SELECTED_CLASS + ",." + CUT_SELECTED_CLASS + " *,." + CUT_HOVER_CLASS + " *"));
  1412. resetSelectedElements();
  1413. if (elementsRemoved.length) {
  1414. elementsRemoved.forEach(element => {
  1415. if (element.tagName.toLowerCase() == NOTE_TAGNAME) {
  1416. resetAnchorNote(element);
  1417. } else {
  1418. element.classList.add(REMOVED_CONTENT_CLASS);
  1419. }
  1420. });
  1421. removedElements[removedElementIndex] = elementsRemoved;
  1422. removedElementIndex++;
  1423. removedElements.length = removedElementIndex;
  1424. onUpdate(false);
  1425. }
  1426. }
  1427. } else {
  1428. if (document.documentElement != selectedElement && selectedElement.tagName.toLowerCase() != NOTE_TAGNAME) {
  1429. const elements = [];
  1430. const searchSelector = "*:not(style):not(meta):not(." + REMOVED_CONTENT_CLASS + ")";
  1431. const elementsKept = [selectedElement].concat(...document.querySelectorAll("." + CUT_OUTER_SELECTED_CLASS));
  1432. document.body.querySelectorAll(searchSelector).forEach(element => {
  1433. let removed = true;
  1434. elementsKept.forEach(elementKept => removed = removed && (elementKept != element && !isAncestor(elementKept, element) && !isAncestor(element, elementKept)));
  1435. if (removed) {
  1436. if (element.tagName.toLowerCase() == NOTE_TAGNAME) {
  1437. resetAnchorNote(element);
  1438. } else {
  1439. elements.push(element);
  1440. }
  1441. }
  1442. });
  1443. elementsKept.forEach(elementKept => {
  1444. const elementKeptRect = elementKept.getBoundingClientRect();
  1445. elementKept.querySelectorAll(searchSelector).forEach(descendant => {
  1446. const descendantRect = descendant.getBoundingClientRect();
  1447. if (descendantRect.width && descendantRect.height && (
  1448. descendantRect.left + descendantRect.width < elementKeptRect.left ||
  1449. descendantRect.right > elementKeptRect.right + elementKeptRect.width ||
  1450. descendantRect.top + descendantRect.height < elementKeptRect.top ||
  1451. descendantRect.bottom > elementKeptRect.bottom + elementKeptRect.height
  1452. )) {
  1453. elements.push(descendant);
  1454. }
  1455. });
  1456. });
  1457. resetSelectedElements();
  1458. if (elements.length) {
  1459. elements.forEach(element => element.classList.add(REMOVED_CONTENT_CLASS));
  1460. removedElements[removedElementIndex] = elements;
  1461. removedElementIndex++;
  1462. removedElements.length = removedElementIndex;
  1463. onUpdate(false);
  1464. }
  1465. }
  1466. }
  1467. }
  1468. function resetSelectedElements(doc = document) {
  1469. doc.querySelectorAll("." + CUT_OUTER_SELECTED_CLASS + ",." + CUT_SELECTED_CLASS).forEach(element => {
  1470. element.classList.remove(CUT_OUTER_SELECTED_CLASS);
  1471. element.classList.remove(CUT_SELECTED_CLASS);
  1472. });
  1473. }
  1474. function anchorNote(event, noteElement, deltaX, deltaY) {
  1475. event.preventDefault();
  1476. const { clientX, clientY } = getPosition(event);
  1477. document.documentElement.style.removeProperty("user-select");
  1478. noteElement.classList.remove(NOTE_MOVING_CLASS);
  1479. maskNoteElement.classList.remove(NOTE_MASK_MOVING_CLASS);
  1480. maskPageElement.classList.remove(PAGE_MASK_ACTIVE_CLASS);
  1481. maskNoteElement.classList.remove(noteElement.dataset.color);
  1482. const headerElement = noteElement.querySelector("header");
  1483. headerElement.ontouchmove = document.documentElement.onmousemove = null;
  1484. let currentElement = anchorElement;
  1485. let positionedElement;
  1486. while (currentElement.parentElement && !positionedElement) {
  1487. if (!FORBIDDEN_TAG_NAMES.includes(currentElement.tagName.toLowerCase())) {
  1488. const currentElementStyle = getComputedStyle(currentElement);
  1489. if (currentElementStyle.position != "static") {
  1490. positionedElement = currentElement;
  1491. }
  1492. }
  1493. currentElement = currentElement.parentElement;
  1494. }
  1495. if (!positionedElement) {
  1496. positionedElement = document.documentElement;
  1497. }
  1498. const containerElement = noteElement.getRootNode().host;
  1499. if (positionedElement == document.documentElement) {
  1500. const firstMaskElement = document.querySelector("." + MASK_CLASS);
  1501. firstMaskElement.parentElement.insertBefore(containerElement, firstMaskElement);
  1502. } else {
  1503. positionedElement.appendChild(containerElement);
  1504. }
  1505. const boundingRectPositionedElement = positionedElement.getBoundingClientRect();
  1506. const stylePositionedElement = window.getComputedStyle(positionedElement);
  1507. const borderX = parseInt(stylePositionedElement.getPropertyValue("border-left-width"));
  1508. const borderY = parseInt(stylePositionedElement.getPropertyValue("border-top-width"));
  1509. noteElement.style.setProperty("position", "absolute");
  1510. noteElement.style.setProperty("left", (clientX - boundingRectPositionedElement.x - deltaX - borderX) + "px");
  1511. noteElement.style.setProperty("top", (clientY - boundingRectPositionedElement.y - deltaY - borderY) + "px");
  1512. }
  1513. function resetAnchorNote(containerElement) {
  1514. const noteId = containerElement.dataset.noteId;
  1515. const noteElement = containerElement.shadowRoot.childNodes[1];
  1516. noteElement.classList.remove(NOTE_ANCHORED_CLASS);
  1517. deleteNoteRef(containerElement, noteId);
  1518. addNoteRef(document.documentElement, noteId);
  1519. document.documentElement.insertBefore(containerElement, maskPageElement.getRootNode().host);
  1520. }
  1521. function getPosition(event) {
  1522. if (event.touches && event.touches.length) {
  1523. const touch = event.touches[0];
  1524. return touch;
  1525. } else {
  1526. return event;
  1527. }
  1528. }
  1529. function highlightSelection() {
  1530. let highlightId = 0;
  1531. document.querySelectorAll("." + HIGHLIGHT_CLASS).forEach(highlightedElement => highlightId = Math.max(highlightId, highlightedElement.dataset.singlefileHighlightId));
  1532. highlightId++;
  1533. const selection = window.getSelection();
  1534. const highlightedNodes = new Set();
  1535. for (let indexRange = 0; indexRange < selection.rangeCount; indexRange++) {
  1536. const range = selection.getRangeAt(indexRange);
  1537. if (!range.collapsed) {
  1538. if (range.commonAncestorContainer.nodeType == range.commonAncestorContainer.TEXT_NODE) {
  1539. let contentText = range.startContainer.splitText(range.startOffset);
  1540. contentText = contentText.splitText(range.endOffset);
  1541. addHighLightedNode(contentText.previousSibling);
  1542. } else {
  1543. const treeWalker = document.createTreeWalker(range.commonAncestorContainer, NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_TEXT);
  1544. let highlightNodes;
  1545. while (treeWalker.nextNode()) {
  1546. if (highlightNodes && !treeWalker.currentNode.contains(range.endContainer)) {
  1547. addHighLightedNode(treeWalker.currentNode);
  1548. }
  1549. if (treeWalker.currentNode == range.startContainer) {
  1550. if (range.startContainer.nodeType == range.startContainer.TEXT_NODE) {
  1551. const contentText = range.startContainer.splitText(range.startOffset);
  1552. treeWalker.nextNode();
  1553. addHighLightedNode(contentText);
  1554. } else {
  1555. addHighLightedNode(range.startContainer.childNodes[range.startOffset]);
  1556. }
  1557. highlightNodes = true;
  1558. }
  1559. if (treeWalker.currentNode == range.endContainer) {
  1560. if (range.endContainer.nodeType == range.endContainer.TEXT_NODE) {
  1561. const contentText = range.endContainer.splitText(range.endOffset);
  1562. treeWalker.nextNode();
  1563. addHighLightedNode(contentText.previousSibling);
  1564. } else {
  1565. addHighLightedNode(range.endContainer.childNodes[range.endOffset]);
  1566. }
  1567. highlightNodes = false;
  1568. }
  1569. }
  1570. range.collapse();
  1571. }
  1572. }
  1573. }
  1574. highlightedNodes.forEach(node => highlightNode(node));
  1575. function addHighLightedNode(node) {
  1576. if (node && node.textContent.trim()) {
  1577. if (node.nodeType == node.TEXT_NODE && node.parentElement.childNodes.length == 1 && node.parentElement.classList.contains(HIGHLIGHT_CLASS)) {
  1578. highlightedNodes.add(node.parentElement);
  1579. } else {
  1580. highlightedNodes.add(node);
  1581. }
  1582. }
  1583. }
  1584. function highlightNode(node) {
  1585. if (node.nodeType == node.ELEMENT_NODE) {
  1586. resetHighlightedElement(node);
  1587. node.classList.add(HIGHLIGHT_CLASS);
  1588. node.classList.add(highlightColor);
  1589. node.dataset.singlefileHighlightId = highlightId;
  1590. } else if (node.parentElement) {
  1591. highlightTextNode(node);
  1592. }
  1593. }
  1594. function highlightTextNode(node) {
  1595. const spanElement = document.createElement("span");
  1596. spanElement.classList.add(HIGHLIGHT_CLASS);
  1597. spanElement.classList.add(highlightColor);
  1598. spanElement.textContent = node.textContent;
  1599. spanElement.dataset.singlefileHighlightId = highlightId;
  1600. node.parentNode.replaceChild(spanElement, node);
  1601. return spanElement;
  1602. }
  1603. }
  1604. function getParents(element) {
  1605. const path = [];
  1606. while (element) {
  1607. path.push(element);
  1608. element = element.parentElement;
  1609. }
  1610. return path;
  1611. }
  1612. function formatPage(applySystemTheme) {
  1613. previousContent = getContent(false, []);
  1614. const shadowRoots = {};
  1615. const classesToPreserve = ["single-file-highlight", "single-file-highlight-yellow", "single-file-highlight-green", "single-file-highlight-pink", "single-file-highlight-blue"];
  1616. document.querySelectorAll(NOTE_TAGNAME).forEach(containerElement => {
  1617. shadowRoots[containerElement.dataset.noteId] = containerElement.shadowRoot;
  1618. const className = "singlefile-note-id-" + containerElement.dataset.noteId;
  1619. containerElement.classList.add(className);
  1620. classesToPreserve.push(className);
  1621. });
  1622. const article = new Readability(document, { classesToPreserve }).parse();
  1623. removedElements = [];
  1624. removedElementIndex = 0;
  1625. document.body.innerHTML = "";
  1626. const domParser = new DOMParser();
  1627. const doc = domParser.parseFromString(article.content, "text/html");
  1628. const contentEditable = document.body.contentEditable;
  1629. document.documentElement.replaceChild(doc.body, document.body);
  1630. document.querySelectorAll(NOTE_TAGNAME).forEach(containerElement => {
  1631. const noteId = (Array.from(containerElement.classList).find(className => /singlefile-note-id-\d+/.test(className))).split("singlefile-note-id-")[1];
  1632. containerElement.classList.remove("singlefile-note-id-" + noteId);
  1633. containerElement.dataset.noteId = noteId;
  1634. if (!containerElement.shadowRoot) {
  1635. containerElement.attachShadow({ mode: "open" });
  1636. containerElement.shadowRoot.appendChild(shadowRoots[noteId]);
  1637. }
  1638. });
  1639. document.querySelectorAll(NOTE_TAGNAME).forEach(containerElement => shadowRoots[containerElement.dataset.noteId].childNodes.forEach(node => containerElement.shadowRoot.appendChild(node)));
  1640. document.body.contentEditable = contentEditable;
  1641. document.head.querySelectorAll("style").forEach(styleElement => styleElement.remove());
  1642. const styleElement = document.createElement("style");
  1643. styleElement.textContent = STYLE_FORMATTED_PAGE;
  1644. document.head.appendChild(styleElement);
  1645. document.body.classList.add("moz-reader-content");
  1646. document.body.classList.add("content-width6");
  1647. document.body.classList.add("reader-show-element");
  1648. document.body.classList.add("sans-serif");
  1649. document.body.classList.add("container");
  1650. document.body.classList.add("line-height4");
  1651. const prefersColorSchemeDark = matchMedia("(prefers-color-scheme: dark)");
  1652. if (applySystemTheme && prefersColorSchemeDark && prefersColorSchemeDark.matches) {
  1653. document.body.classList.add("dark");
  1654. }
  1655. document.body.style.setProperty("display", "block");
  1656. document.body.style.setProperty("padding", "24px");
  1657. const titleElement = document.createElement("h1");
  1658. titleElement.classList.add("reader-title");
  1659. titleElement.textContent = article.title;
  1660. document.body.insertBefore(titleElement, document.body.firstChild);
  1661. document.querySelectorAll("a[href]").forEach(element => {
  1662. const href = element.getAttribute("href").trim();
  1663. if (href.startsWith(document.baseURI + "#")) {
  1664. element.setAttribute("href", href.substring(document.baseURI.length));
  1665. }
  1666. });
  1667. document.documentElement.appendChild(getStyleElement(HIGHLIGHTS_WEB_STYLESHEET));
  1668. maskPageElement = getMaskElement(PAGE_MASK_CLASS, PAGE_MASK_CONTAINER_CLASS);
  1669. maskNoteElement = getMaskElement(NOTE_MASK_CLASS);
  1670. reflowNotes();
  1671. onUpdate(false);
  1672. }
  1673. async function cancelFormatPage() {
  1674. if (previousContent) {
  1675. const contentEditable = document.body.contentEditable;
  1676. await init(previousContent, { reset: true });
  1677. document.body.contentEditable = contentEditable;
  1678. onUpdate(false);
  1679. previousContent = null;
  1680. }
  1681. }
  1682. function getContent(compressHTML, updatedResources) {
  1683. unhighlightCutElement();
  1684. serializeShadowRoots(document);
  1685. const doc = document.cloneNode(true);
  1686. resetSelectedElements(doc);
  1687. deserializeShadowRoots(doc);
  1688. deserializeShadowRoots(document);
  1689. doc.querySelectorAll("[" + DISABLED_NOSCRIPT_ATTRIBUTE_NAME + "]").forEach(element => {
  1690. element.textContent = element.getAttribute(DISABLED_NOSCRIPT_ATTRIBUTE_NAME);
  1691. element.removeAttribute(DISABLED_NOSCRIPT_ATTRIBUTE_NAME);
  1692. });
  1693. doc.querySelectorAll("." + MASK_CLASS + ", ." + REMOVED_CONTENT_CLASS).forEach(maskElement => maskElement.remove());
  1694. doc.querySelectorAll("." + HIGHLIGHT_CLASS).forEach(noteElement => noteElement.classList.remove(HIGHLIGHT_HIDDEN_CLASS));
  1695. doc.querySelectorAll(`template[${SHADOWROOT_ATTRIBUTE_NAME}]`).forEach(templateElement => {
  1696. const noteElement = templateElement.querySelector("." + NOTE_CLASS);
  1697. if (noteElement) {
  1698. noteElement.classList.remove(NOTE_HIDDEN_CLASS);
  1699. }
  1700. const mainElement = templateElement.querySelector("textarea");
  1701. if (mainElement) {
  1702. mainElement.textContent = mainElement.value;
  1703. }
  1704. });
  1705. doc.querySelectorAll("iframe").forEach(element => {
  1706. const pointerEvents = "pointer-events";
  1707. element.style.setProperty(pointerEvents, element.style.getPropertyValue("-sf-" + pointerEvents), element.style.getPropertyPriority("-sf-" + pointerEvents));
  1708. element.style.removeProperty("-sf-" + pointerEvents);
  1709. });
  1710. doc.body.removeAttribute("contentEditable");
  1711. const scriptElement = doc.createElement("script");
  1712. scriptElement.setAttribute(SCRIPT_TEMPLATE_SHADOW_ROOT, "");
  1713. scriptElement.textContent = getEmbedScript();
  1714. doc.body.appendChild(scriptElement);
  1715. const newResources = Object.keys(updatedResources).filter(url => updatedResources[url].type == "stylesheet").map(url => updatedResources[url]);
  1716. newResources.forEach(resource => {
  1717. const element = doc.createElement("style");
  1718. doc.body.appendChild(element);
  1719. element.textContent = resource.content;
  1720. });
  1721. return singlefile.helper.serialize(doc, compressHTML);
  1722. }
  1723. function onUpdate(saved) {
  1724. window.parent.postMessage(JSON.stringify({ "method": "onUpdate", saved }), "*");
  1725. }
  1726. function reflowNotes() {
  1727. document.querySelectorAll(NOTE_TAGNAME).forEach(containerElement => {
  1728. const noteElement = containerElement.shadowRoot.querySelector("." + NOTE_CLASS);
  1729. const noteBoundingRect = noteElement.getBoundingClientRect();
  1730. const anchorElement = getAnchorElement(containerElement);
  1731. const anchorBoundingRect = anchorElement.getBoundingClientRect();
  1732. const maxX = anchorBoundingRect.x + Math.max(0, anchorBoundingRect.width - noteBoundingRect.width);
  1733. const minX = anchorBoundingRect.x;
  1734. const maxY = anchorBoundingRect.y + Math.max(0, anchorBoundingRect.height - NOTE_HEADER_HEIGHT);
  1735. const minY = anchorBoundingRect.y;
  1736. let left = parseInt(noteElement.style.getPropertyValue("left"));
  1737. let top = parseInt(noteElement.style.getPropertyValue("top"));
  1738. if (noteBoundingRect.x > maxX) {
  1739. left -= noteBoundingRect.x - maxX;
  1740. }
  1741. if (noteBoundingRect.x < minX) {
  1742. left += minX - noteBoundingRect.x;
  1743. }
  1744. if (noteBoundingRect.y > maxY) {
  1745. top -= noteBoundingRect.y - maxY;
  1746. }
  1747. if (noteBoundingRect.y < minY) {
  1748. top += minY - noteBoundingRect.y;
  1749. }
  1750. noteElement.style.setProperty("position", "absolute");
  1751. noteElement.style.setProperty("left", left + "px");
  1752. noteElement.style.setProperty("top", top + "px");
  1753. });
  1754. }
  1755. function resetHighlightedElement(element) {
  1756. element.classList.remove(HIGHLIGHT_CLASS);
  1757. element.classList.remove("single-file-highlight-yellow");
  1758. element.classList.remove("single-file-highlight-pink");
  1759. element.classList.remove("single-file-highlight-blue");
  1760. element.classList.remove("single-file-highlight-green");
  1761. delete element.dataset.singlefileHighlightId;
  1762. }
  1763. function serializeShadowRoots(node) {
  1764. node.querySelectorAll("*").forEach(element => {
  1765. const shadowRoot = getShadowRoot(element);
  1766. if (shadowRoot) {
  1767. serializeShadowRoots(shadowRoot);
  1768. const templateElement = document.createElement("template");
  1769. templateElement.setAttribute(SHADOWROOT_ATTRIBUTE_NAME, "open");
  1770. templateElement.appendChild(shadowRoot);
  1771. element.appendChild(templateElement);
  1772. }
  1773. });
  1774. }
  1775. function deserializeShadowRoots(node) {
  1776. node.querySelectorAll(`template[${SHADOWROOT_ATTRIBUTE_NAME}]`).forEach(element => {
  1777. if (element.parentElement) {
  1778. let shadowRoot = getShadowRoot(element.parentElement);
  1779. if (shadowRoot) {
  1780. Array.from(element.childNodes).forEach(node => shadowRoot.appendChild(node));
  1781. element.remove();
  1782. } else {
  1783. try {
  1784. shadowRoot = element.parentElement.attachShadow({ mode: "open" });
  1785. const contentDocument = (new DOMParser()).parseFromString(element.innerHTML, "text/html");
  1786. Array.from(contentDocument.head.childNodes).forEach(node => shadowRoot.appendChild(node));
  1787. Array.from(contentDocument.body.childNodes).forEach(node => shadowRoot.appendChild(node));
  1788. } catch (error) {
  1789. // ignored
  1790. }
  1791. }
  1792. if (shadowRoot) {
  1793. deserializeShadowRoots(shadowRoot);
  1794. }
  1795. }
  1796. });
  1797. }
  1798. function getMaskElement(className, containerClassName) {
  1799. let maskElement = document.documentElement.querySelector("." + className);
  1800. if (!maskElement) {
  1801. maskElement = document.createElement("div");
  1802. const maskContainerElement = document.createElement("div");
  1803. if (containerClassName) {
  1804. maskContainerElement.classList.add(containerClassName);
  1805. }
  1806. maskContainerElement.classList.add(MASK_CLASS);
  1807. const firstNote = document.querySelector(NOTE_TAGNAME);
  1808. if (firstNote && firstNote.parentElement == document.documentElement) {
  1809. document.documentElement.insertBefore(maskContainerElement, firstNote);
  1810. } else {
  1811. document.documentElement.appendChild(maskContainerElement);
  1812. }
  1813. maskElement.classList.add(className);
  1814. const maskShadow = maskContainerElement.attachShadow({ mode: "open" });
  1815. maskShadow.appendChild(getStyleElement(MASK_WEB_STYLESHEET));
  1816. maskShadow.appendChild(maskElement);
  1817. return maskElement;
  1818. }
  1819. }
  1820. function getEmbedScript() {
  1821. return minifyText(`(() => {
  1822. document.currentScript.remove();
  1823. const processNode = node => {
  1824. node.querySelectorAll("template[${SHADOWROOT_ATTRIBUTE_NAME}]").forEach(element=>{
  1825. let shadowRoot = getShadowRoot(element.parentElement);
  1826. if (!shadowRoot) {
  1827. try {
  1828. shadowRoot = element.parentElement.attachShadow({mode:element.getAttribute("${SHADOWROOT_ATTRIBUTE_NAME}")});
  1829. shadowRoot.innerHTML = element.innerHTML;
  1830. element.remove();
  1831. } catch (error) {}
  1832. if (shadowRoot) {
  1833. processNode(shadowRoot);
  1834. }
  1835. }
  1836. })
  1837. };
  1838. const FORBIDDEN_TAG_NAMES = ${JSON.stringify(FORBIDDEN_TAG_NAMES)};
  1839. const NOTE_TAGNAME = ${JSON.stringify(NOTE_TAGNAME)};
  1840. const NOTE_CLASS = ${JSON.stringify(NOTE_CLASS)};
  1841. const NOTE_ANCHORED_CLASS = ${JSON.stringify(NOTE_ANCHORED_CLASS)};
  1842. const NOTE_SELECTED_CLASS = ${JSON.stringify(NOTE_SELECTED_CLASS)};
  1843. const NOTE_MOVING_CLASS = ${JSON.stringify(NOTE_MOVING_CLASS)};
  1844. const NOTE_MASK_MOVING_CLASS = ${JSON.stringify(NOTE_MASK_MOVING_CLASS)};
  1845. const MASK_CLASS = ${JSON.stringify(MASK_CLASS)};
  1846. const HIGHLIGHT_CLASS = ${JSON.stringify(HIGHLIGHT_CLASS)};
  1847. const NOTES_WEB_STYLESHEET = ${JSON.stringify(NOTES_WEB_STYLESHEET)};
  1848. const MASK_WEB_STYLESHEET = ${JSON.stringify(MASK_WEB_STYLESHEET)};
  1849. const NOTE_HEADER_HEIGHT = ${JSON.stringify(NOTE_HEADER_HEIGHT)};
  1850. const PAGE_MASK_ACTIVE_CLASS = ${JSON.stringify(PAGE_MASK_ACTIVE_CLASS)};
  1851. const REMOVED_CONTENT_CLASS = ${JSON.stringify(REMOVED_CONTENT_CLASS)};
  1852. const reflowNotes = ${minifyText(reflowNotes.toString())};
  1853. const addNoteRef = ${minifyText(addNoteRef.toString())};
  1854. const deleteNoteRef = ${minifyText(deleteNoteRef.toString())};
  1855. const getNoteRefs = ${minifyText(getNoteRefs.toString())};
  1856. const setNoteRefs = ${minifyText(setNoteRefs.toString())};
  1857. const getAnchorElement = ${minifyText(getAnchorElement.toString())};
  1858. const getMaskElement = ${minifyText(getMaskElement.toString())};
  1859. const getStyleElement = ${minifyText(getStyleElement.toString())};
  1860. const attachNoteListeners = ${minifyText(attachNoteListeners.toString())};
  1861. const anchorNote = ${minifyText(anchorNote.toString())};
  1862. const getPosition = ${minifyText(getPosition.toString())};
  1863. const onMouseUp = ${minifyText(onMouseUp.toString())};
  1864. const getShadowRoot = ${minifyText(getShadowRoot.toString())};
  1865. const maskNoteElement = getMaskElement(${JSON.stringify(NOTE_MASK_CLASS)});
  1866. const maskPageElement = getMaskElement(${JSON.stringify(PAGE_MASK_CLASS)}, ${JSON.stringify(PAGE_MASK_CONTAINER_CLASS)});
  1867. let selectedNote, highlightSelectionMode, removeHighlightMode, resizingNoteMode, movingNoteMode, collapseNoteTimeout, cuttingMode, cuttingOuterMode;
  1868. window.onresize = reflowNotes;
  1869. window.onUpdate = () => {};
  1870. document.documentElement.onmouseup = document.documentElement.ontouchend = onMouseUp;
  1871. window.addEventListener("DOMContentLoaded", () => {
  1872. processNode(document);
  1873. reflowNotes();
  1874. document.querySelectorAll(${JSON.stringify(NOTE_TAGNAME)}).forEach(noteElement => attachNoteListeners(noteElement));
  1875. });
  1876. })()`);
  1877. }
  1878. function getStyleElement(stylesheet) {
  1879. const linkElement = document.createElement("style");
  1880. linkElement.textContent = stylesheet;
  1881. return linkElement;
  1882. }
  1883. function getAnchorElement(containerElement) {
  1884. return document.querySelector("[data-single-file-note-refs^=" + JSON.stringify(containerElement.dataset.noteId) + "], [data-single-file-note-refs$=" + JSON.stringify(containerElement.dataset.noteId) + "], [data-single-file-note-refs*=" + JSON.stringify("," + containerElement.dataset.noteId + ",") + "]")
  1885. || document.documentElement;
  1886. }
  1887. function addNoteRef(anchorElement, noteId) {
  1888. const noteRefs = getNoteRefs(anchorElement);
  1889. noteRefs.push(noteId);
  1890. setNoteRefs(anchorElement, noteRefs);
  1891. }
  1892. function deleteNoteRef(containerElement, noteId) {
  1893. const anchorElement = getAnchorElement(containerElement);
  1894. const noteRefs = getNoteRefs(anchorElement).filter(noteRefs => noteRefs != noteId);
  1895. if (noteRefs.length) {
  1896. setNoteRefs(anchorElement, noteRefs);
  1897. } else {
  1898. delete anchorElement.dataset.singleFileNoteRefs;
  1899. }
  1900. }
  1901. function getNoteRefs(anchorElement) {
  1902. return JSON.parse("[" + (anchorElement.dataset.singleFileNoteRefs || "") + "]");
  1903. }
  1904. function setNoteRefs(anchorElement, noteRefs) {
  1905. anchorElement.dataset.singleFileNoteRefs = noteRefs.toString();
  1906. }
  1907. function minifyText(text) {
  1908. return text.replace(/[\n\t\s]+/g, " ");
  1909. }
  1910. function isAncestor(element, otherElement) {
  1911. return otherElement.parentElement && (element == otherElement.parentElement || isAncestor(element, otherElement.parentElement));
  1912. }
  1913. function getShadowRoot(element) {
  1914. const chrome = window.chrome;
  1915. if (element.openOrClosedShadowRoot) {
  1916. return element.openOrClosedShadowRoot;
  1917. } else if (chrome && chrome.dom && chrome.dom.openOrClosedShadowRoot) {
  1918. try {
  1919. return chrome.dom.openOrClosedShadowRoot(element);
  1920. } catch (error) {
  1921. return element.shadowRoot;
  1922. }
  1923. } else {
  1924. return element.shadowRoot;
  1925. }
  1926. }
  1927. function detectSavedPage(document) {
  1928. const firstDocumentChild = document.documentElement.firstChild;
  1929. return firstDocumentChild.nodeType == Node.COMMENT_NODE &&
  1930. (firstDocumentChild.textContent.includes(COMMENT_HEADER) || firstDocumentChild.textContent.includes(COMMENT_HEADER_LEGACY));
  1931. }
  1932. })(typeof globalThis == "object" ? globalThis : window);
  1933. })();