extension-editor.js 71 KB

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