content-ui-editor-web.js 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578
  1. /*
  2. * Copyright 2010-2020 Gildas Lormeau
  3. * contact : gildas.lormeau <at> gmail.com
  4. *
  5. * This file is part of SingleFile.
  6. *
  7. * The code in this file is free software: you can redistribute it and/or
  8. * modify it under the terms of the GNU Affero General Public License
  9. * (GNU AGPL) as published by the Free Software Foundation, either version 3
  10. * of the License, or (at your option) any later version.
  11. *
  12. * The code in this file is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
  15. * General Public License for more details.
  16. *
  17. * As additional permission under GNU AGPL version 3 section 7, you may
  18. * distribute UNMODIFIED VERSIONS OF THIS file without the copy of the GNU
  19. * AGPL normally required by section 4, provided you include this license
  20. * notice and a URL through which recipients can access the Corresponding
  21. * Source.
  22. */
  23. /* global singlefile, window, document, fetch, DOMParser, getComputedStyle, setTimeout, clearTimeout, NodeFilter, Readability, isProbablyReaderable, matchMedia */
  24. (() => {
  25. const FORBIDDEN_TAG_NAMES = ["a", "area", "audio", "base", "br", "col", "command", "embed", "hr", "img", "iframe", "input", "keygen", "link", "meta", "param", "source", "track", "video", "wbr"];
  26. const BUTTON_ANCHOR_URL = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAQAAAC1+jfqAAAAAmJLR0QA/4ePzL8AAAAJcEhZcwAADsQAAA7EAZUrDhsAAAAHdElNRQfjCh0VAjZTpsLvAAAA1klEQVQoz3XQMS8EURQF4O9txN9QSCjoNYRiy01EomDb3UbnR+j8BpSKrbaaQvwAyTZ2Qyg0WiwJiZjmKmasmWTnvObe8849971DFSe+/TjVgL6wpyMczxcMXYIzV/9kqyIY27Vsyb5J05KREMa1sQoS3r2W1Vxkcrms6Tph6q3usFARBJJUVjPBgZ6Je49u5ELCog0r1qy7YFMI4RxMfZRZFOxOa/bn28oi8rK7K6hrITw48uVT37MQBn9vOcS2l9K0OE9W0atHsqWtIwxlRk1ZdHXrxC+ueUcydrdI6QAAAABJRU5ErkJggg==";
  27. const BUTTON_CLOSE_URL = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4wodFQUaLj84ywAAANxJREFUOMuN08tKAzEUgOGPSrU6fTL3PpuuperCSr22OFX0hQSXYkfHzRkIcW4Hskg4/5/kJAf2cIZ77BuOORa4aBZOUcfYDEjmuIncXSO5SwQ11h2SIoFrVHgRyU8Dkjb4GdMmoU9SYJnBZQqnknUmKXE1Bu6T1EnRtn1wKikz+AfvbfCkRTDFR7ZW47Mj/99TLbOdq2T+iNlYeBfHvs2u89AmKXCdVbsp2EHsnEsO0+/ZBTcx65OcD8Bdkl+sJtGNVRTsDcdx/zy+cBL/BL5DBC7xOrKdj6L1V/AHRf5yO+i79cQAAAAASUVORK5CYII=";
  28. const SHADOW_MODE_ATTRIBUTE_NAME = "shadowmode";
  29. const SHADOW_DELEGATE_FOCUS_ATTRIBUTE_NAME = "delegatesfocus";
  30. const SCRIPT_TEMPLATE_SHADOW_ROOT = "data-template-shadow-root";
  31. const NOTE_TAGNAME = "single-file-note";
  32. const NOTE_CLASS = "note";
  33. const NOTE_MASK_CLASS = "note-mask";
  34. const NOTE_HIDDEN_CLASS = "note-hidden";
  35. const NOTE_ANCHORED_CLASS = "note-anchored";
  36. const NOTE_SELECTED_CLASS = "note-selected";
  37. const NOTE_MOVING_CLASS = "note-moving";
  38. const NOTE_MASK_MOVING_CLASS = "note-mask-moving";
  39. const PAGE_MASK_CLASS = "page-mask";
  40. const MASK_CLASS = "single-file-mask";
  41. const PAGE_MASK_CONTAINER_CLASS = "single-file-page-mask";
  42. const HIGHLIGHT_CLASS = "single-file-highlight";
  43. const REMOVED_CONTENT_CLASS = "single-file-removed";
  44. const HIGHLIGHT_HIDDEN_CLASS = "single-file-highlight-hidden";
  45. const PAGE_MASK_ACTIVE_CLASS = "page-mask-active";
  46. const NOTE_INITIAL_POSITION_X = 20;
  47. const NOTE_INITIAL_POSITION_Y = 20;
  48. const NOTE_INITIAL_WIDTH = 150;
  49. const NOTE_INITIAL_HEIGHT = 150;
  50. const NOTE_HEADER_HEIGHT = 25;
  51. const DISABLED_NOSCRIPT_ATTRIBUTE_NAME = "data-single-file-disabled-noscript";
  52. const STYLE_FORMATTED_PAGE = `
  53. /* This Source Code Form is subject to the terms of the Mozilla Public
  54. * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  55. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  56. /* Avoid adding ID selector rules in this style sheet, since they could
  57. * inadvertently match elements in the article content. */
  58. :root {
  59. --close-button-hover: #d94141;
  60. }
  61. body {
  62. --toolbar-bgcolor: #fbfbfb;
  63. --toolbar-border: #b5b5b5;
  64. --toolbar-hover: #ebebeb;
  65. --popup-bgcolor: #fbfbfb;
  66. --popup-border: #b5b5b5;
  67. --font-color: #4c4c4c;
  68. --icon-fill: #808080;
  69. /* light colours */
  70. }
  71. body.dark {
  72. --toolbar-bgcolor: #2a2a2d;
  73. --toolbar-border: #4B4A50;
  74. --toolbar-hover: #737373;
  75. --popup-bgcolor: #4b4a50;
  76. --popup-border: #65646a;
  77. --font-color: #fff;
  78. --icon-fill: #fff;
  79. /* dark colours */
  80. }
  81. body {
  82. padding: 64px 51px;
  83. }
  84. body.loaded {
  85. transition: color 0.4s, background-color 0.4s;
  86. }
  87. body.light {
  88. color: #333333;
  89. background-color: #ffffff;
  90. }
  91. body.dark {
  92. color: #eeeeee;
  93. background-color: #333333;
  94. }
  95. body.dark *::-moz-selection {
  96. background-color: #FFFFFF;
  97. color: #0095DD;
  98. }
  99. body.dark a::-moz-selection {
  100. color: #DD4800;
  101. }
  102. body.sepia {
  103. color: #5b4636;
  104. background-color: #f4ecd8;
  105. }
  106. body.sans-serif,
  107. body.sans-serif .remove-button {
  108. font-family: Helvetica, Arial, sans-serif;
  109. }
  110. body.serif,
  111. body.serif .remove-button {
  112. font-family: Georgia, "Times New Roman", serif;
  113. }
  114. .container {
  115. --font-size: 12;
  116. max-width: 30em;
  117. margin: 0 auto;
  118. font-size: var(--font-size);
  119. }
  120. .container.content-width1 {
  121. max-width: 20em;
  122. }
  123. .container.content-width2 {
  124. max-width: 25em;
  125. }
  126. .container.content-width3 {
  127. max-width: 30em;
  128. }
  129. .container.content-width4 {
  130. max-width: 35em;
  131. }
  132. .container.content-width5 {
  133. max-width: 40em;
  134. }
  135. .container.content-width6 {
  136. max-width: 45em;
  137. }
  138. .container.content-width7 {
  139. max-width: 50em;
  140. }
  141. .container.content-width8 {
  142. max-width: 55em;
  143. }
  144. .container.content-width9 {
  145. max-width: 60em;
  146. }
  147. /* Override some controls and content styles based on color scheme */
  148. body.light > .container > .header > .domain {
  149. border-bottom-color: #333333 !important;
  150. }
  151. body.sepia > .container > .header > .domain {
  152. border-bottom-color: #5b4636 !important;
  153. }
  154. body.dark > .container > .header > .domain {
  155. border-bottom-color: #eeeeee !important;
  156. }
  157. body.sepia > .container > .footer {
  158. background-color: #dedad4 !important;
  159. }
  160. body.light blockquote {
  161. border-inline-start: 2px solid #333333 !important;
  162. }
  163. body.sepia blockquote {
  164. border-inline-start: 2px solid #5b4636 !important;
  165. }
  166. body.dark blockquote {
  167. border-inline-start: 2px solid #eeeeee !important;
  168. }
  169. /* Add toolbar transition base on loaded class */
  170. body.loaded .toolbar {
  171. transition: transform 0.3s ease-out;
  172. }
  173. body:not(.loaded) .toolbar:-moz-locale-dir(ltr) {
  174. transform: translateX(-100%);
  175. }
  176. body:not(.loaded) .toolbar:-moz-locale-dir(rtl) {
  177. transform: translateX(100%);
  178. }
  179. .light-button {
  180. color: #333333;
  181. background-color: #ffffff;
  182. }
  183. .dark-button {
  184. color: #eeeeee;
  185. background-color: #333333;
  186. }
  187. .sepia-button {
  188. color: #5b4636;
  189. background-color: #f4ecd8;
  190. }
  191. .sans-serif-button {
  192. font-family: Helvetica, Arial, sans-serif;
  193. }
  194. .serif-button {
  195. font-family: Georgia, "Times New Roman", serif;
  196. }
  197. /* Loading/error message */
  198. .reader-message {
  199. margin-top: 40px;
  200. display: none;
  201. text-align: center;
  202. width: 100%;
  203. font-size: 0.9em;
  204. }
  205. /* Header */
  206. .header {
  207. text-align: start;
  208. display: none;
  209. }
  210. .domain {
  211. font-size: 0.9em;
  212. line-height: 1.48em;
  213. padding-bottom: 4px;
  214. font-family: Helvetica, Arial, sans-serif;
  215. text-decoration: none;
  216. border-bottom: 1px solid;
  217. color: #0095dd;
  218. }
  219. .header > h1 {
  220. font-size: 1.6em;
  221. line-height: 1.25em;
  222. width: 100%;
  223. margin: 30px 0;
  224. padding: 0;
  225. }
  226. .header > .credits {
  227. font-size: 0.9em;
  228. line-height: 1.48em;
  229. margin: 0 0 10px 0;
  230. padding: 0;
  231. font-style: italic;
  232. }
  233. .header > .meta-data {
  234. font-size: 0.65em;
  235. margin: 0 0 15px 0;
  236. }
  237. /*======= Controls toolbar =======*/
  238. .toolbar {
  239. font-family: Helvetica, Arial, sans-serif;
  240. position: fixed;
  241. height: 100%;
  242. top: 0;
  243. left: 0;
  244. margin: 0;
  245. padding: 0;
  246. list-style: none;
  247. background-color: var(--toolbar-bgcolor);
  248. -moz-user-select: none;
  249. border-right: 1px solid var(--toolbar-border);
  250. z-index: 1;
  251. }
  252. .button {
  253. display: block;
  254. background-size: 24px 24px;
  255. background-repeat: no-repeat;
  256. color: #333;
  257. background-color: var(--toolbar-bgcolor);
  258. height: 40px;
  259. padding: 0;
  260. }
  261. button {
  262. -moz-context-properties: fill;
  263. color: var(--font-color);
  264. fill: var(--icon-fill);
  265. }
  266. .toolbar .button {
  267. width: 40px;
  268. background-position: center;
  269. margin-right: -1px;
  270. border-top: 0;
  271. border-left: 0;
  272. border-right: 1px solid var(--toolbar-border);
  273. border-bottom: 1px solid var(--toolbar-border);
  274. background-color: var(--toolbar-bgcolor);
  275. }
  276. .button[hidden] {
  277. display: none;
  278. }
  279. .dropdown {
  280. text-align: center;
  281. list-style: none;
  282. margin: 0;
  283. padding: 0;
  284. }
  285. .dropdown li {
  286. margin: 0;
  287. padding: 0;
  288. }
  289. /*======= Popup =======*/
  290. .dropdown-popup {
  291. min-width: 300px;
  292. text-align: start;
  293. position: absolute;
  294. left: 48px; /* offset to account for toolbar width */
  295. z-index: 1000;
  296. background-color: var(--popup-bgcolor);
  297. visibility: hidden;
  298. border-radius: 4px;
  299. border: 1px solid var(--popup-border);
  300. border-bottom-width: 0;
  301. box-shadow: 0 1px 3px #c1c1c1;
  302. }
  303. .keep-open .dropdown-popup {
  304. z-index: initial;
  305. }
  306. .dropdown-popup > hr {
  307. display: none;
  308. }
  309. .open > .dropdown-popup {
  310. visibility: visible;
  311. }
  312. .dropdown-arrow {
  313. position: absolute;
  314. top: 30px; /* offset arrow from top of popup */
  315. left: -16px;
  316. width: 16px;
  317. height: 24px;
  318. background-image: url("chrome://global/skin/reader/RM-Type-Controls-Arrow.svg");
  319. display: block;
  320. -moz-context-properties: fill, stroke;
  321. fill: var(--popup-bgcolor);
  322. stroke: var(--popup-border);
  323. }
  324. /*======= Font style popup =======*/
  325. .font-type-buttons,
  326. .font-size-buttons,
  327. .color-scheme-buttons,
  328. .content-width-buttons,
  329. .line-height-buttons {
  330. display: flex;
  331. flex-direction: row;
  332. }
  333. .font-type-buttons > button:first-child {
  334. border-top-left-radius: 3px;
  335. }
  336. .font-type-buttons > button:last-child {
  337. border-top-right-radius: 3px;
  338. }
  339. .color-scheme-buttons > button:first-child {
  340. border-bottom-left-radius: 3px;
  341. }
  342. .color-scheme-buttons > button:last-child {
  343. border-bottom-right-radius: 3px;
  344. }
  345. .font-type-buttons > button,
  346. .font-size-buttons > button,
  347. .color-scheme-buttons > button,
  348. .content-width-buttons > button,
  349. .line-height-buttons > button {
  350. text-align: center;
  351. border: 0;
  352. }
  353. .font-type-buttons > button,
  354. .font-size-buttons > button,
  355. .content-width-buttons > button,
  356. .line-height-buttons > button {
  357. width: 50%;
  358. background-color: transparent;
  359. border-left: 1px solid var(--popup-border);
  360. border-bottom: 1px solid var(--popup-border);
  361. }
  362. .color-scheme-buttons > button {
  363. width: 33.33%;
  364. font-size: 14px;
  365. }
  366. .color-scheme-buttons > .dark-button {
  367. margin-top: -1px;
  368. height: 61px;
  369. }
  370. .font-type-buttons > button:first-child,
  371. .font-size-buttons > button:first-child,
  372. .content-width-buttons > button:first-child,
  373. .line-height-buttons > button:first-child {
  374. border-left: 0;
  375. }
  376. .font-type-buttons > button {
  377. display: inline-block;
  378. font-size: 62px;
  379. height: 100px;
  380. }
  381. .font-size-buttons > button,
  382. .color-scheme-buttons > button,
  383. .content-width-buttons > button,
  384. .line-height-buttons > button {
  385. height: 60px;
  386. }
  387. .font-type-buttons > button:active:hover,
  388. .font-type-buttons > button.selected,
  389. .color-scheme-buttons > button:active:hover,
  390. .color-scheme-buttons > button.selected {
  391. box-shadow: inset 0 -3px 0 0 #fc6420;
  392. }
  393. .font-type-buttons > button:active:hover,
  394. .font-type-buttons > button.selected {
  395. border-bottom: 1px solid #FC6420;
  396. }
  397. /* Make the serif button content the same size as the sans-serif button content. */
  398. .font-type-buttons > button > .description {
  399. font-size: 12px;
  400. margin-top: -5px;
  401. }
  402. /* Font sizes are different per-platform, so we need custom CSS to line them up. */
  403. .font-type-buttons > .sans-serif-button > .name {
  404. margin-top: 2px;
  405. }
  406. .font-type-buttons > .sans-serif-button > .description {
  407. margin-top: -4px;
  408. }
  409. .font-type-buttons > .serif-button > .name {
  410. font-size: 63px;
  411. }
  412. .button:hover,
  413. .font-size-buttons > button:hover,
  414. .font-type-buttons > button:hover,
  415. .content-width-buttons > button:hover,
  416. .line-height-buttons > button:hover {
  417. background-color: var(--toolbar-hover);
  418. }
  419. .dropdown.open,
  420. .button:active,
  421. .font-size-buttons > button:active,
  422. .font-size-buttons > button.selected,
  423. .content-width-buttons > button:active,
  424. .content-width-buttons > button.selected,
  425. .line-height-buttons > button:active,
  426. .line-height-buttons > button.selected {
  427. background-color: #dadada;
  428. }
  429. /* Only used on Android */
  430. .font-size-sample {
  431. display: none;
  432. }
  433. .minus-button,
  434. .plus-button,
  435. .content-width-minus-button,
  436. .content-width-plus-button,
  437. .line-height-minus-button,
  438. .line-height-plus-button {
  439. background-color: transparent;
  440. border: 0;
  441. background-size: 18px 18px;
  442. background-repeat: no-repeat;
  443. background-position: center;
  444. }
  445. /*======= Toolbar icons =======*/
  446. .close-button {
  447. background-image: url("chrome://global/skin/reader/RM-Close-24x24.svg");
  448. height: 68px;
  449. background-position: center 8px;
  450. }
  451. .close-button:hover {
  452. fill: #fff;
  453. background-color: var(--close-button-hover);
  454. border-bottom: 1px solid var(--close-button-hover);
  455. border-right: 1px solid var(--close-button-hover);
  456. }
  457. .close-button:hover:active {
  458. background-color: #AE2325;
  459. border-bottom: 1px solid #AE2325;
  460. border-right: 1px solid #AE2325;
  461. }
  462. .style-button {
  463. background-image: url("chrome://global/skin/reader/RM-Type-Controls-24x24.svg");
  464. }
  465. .minus-button {
  466. background-image: url("chrome://global/skin/reader/RM-Minus-24x24.svg");
  467. }
  468. .plus-button {
  469. background-image: url("chrome://global/skin/reader/RM-Plus-24x24.svg");
  470. }
  471. .content-width-minus-button {
  472. background-size: 42px 16px;
  473. background-image: url("chrome://global/skin/reader/RM-Content-Width-Minus-42x16.svg");
  474. }
  475. .content-width-plus-button {
  476. background-size: 44px 16px;
  477. background-image: url("chrome://global/skin/reader/RM-Content-Width-Plus-44x16.svg");
  478. }
  479. .line-height-minus-button {
  480. background-size: 34px 14px;
  481. background-image: url("chrome://global/skin/reader/RM-Line-Height-Minus-38x14.svg");
  482. }
  483. .line-height-plus-button {
  484. background-size: 34px 24px;
  485. background-image: url("chrome://global/skin/reader/RM-Line-Height-Plus-38x24.svg");
  486. }
  487. @media print {
  488. .toolbar {
  489. display: none !important;
  490. }
  491. }
  492. /*======= Article content =======*/
  493. /* Note that any class names from the original article that we want to match on
  494. * must be added to CLASSES_TO_PRESERVE in ReaderMode.jsm, so that
  495. * Readability.js doesn't strip them out */
  496. .moz-reader-content {
  497. display: none;
  498. font-size: 1em;
  499. line-height: 1.6em;
  500. }
  501. .moz-reader-content.line-height1 {
  502. line-height: 1em;
  503. }
  504. .moz-reader-content.line-height2 {
  505. line-height: 1.2em;
  506. }
  507. .moz-reader-content.line-height3 {
  508. line-height: 1.4em;
  509. }
  510. .moz-reader-content.line-height4 {
  511. line-height: 1.6em;
  512. }
  513. .moz-reader-content.line-height5 {
  514. line-height: 1.8em;
  515. }
  516. .moz-reader-content.line-height6 {
  517. line-height: 2.0em;
  518. }
  519. .moz-reader-content.line-height7 {
  520. line-height: 2.2em;
  521. }
  522. .moz-reader-content.line-height8 {
  523. line-height: 2.4em;
  524. }
  525. .moz-reader-content.line-height9 {
  526. line-height: 2.6em;
  527. }
  528. @media print {
  529. .moz-reader-content p,
  530. .moz-reader-content code,
  531. .moz-reader-content pre,
  532. .moz-reader-content blockquote,
  533. .moz-reader-content ul,
  534. .moz-reader-content ol,
  535. .moz-reader-content li,
  536. .moz-reader-content figure,
  537. .moz-reader-content .wp-caption {
  538. margin: 0 0 10px 0 !important;
  539. padding: 0 !important;
  540. }
  541. }
  542. .moz-reader-content h1,
  543. .moz-reader-content h2,
  544. .moz-reader-content h3 {
  545. font-weight: bold;
  546. }
  547. .moz-reader-content h1 {
  548. font-size: 1.6em;
  549. line-height: 1.25em;
  550. }
  551. .moz-reader-content h2 {
  552. font-size: 1.2em;
  553. line-height: 1.51em;
  554. }
  555. .moz-reader-content h3 {
  556. font-size: 1em;
  557. line-height: 1.66em;
  558. }
  559. .moz-reader-content a:link {
  560. text-decoration: underline;
  561. font-weight: normal;
  562. }
  563. .moz-reader-content a:link,
  564. .moz-reader-content a:link:hover,
  565. .moz-reader-content a:link:active {
  566. color: #0095dd;
  567. }
  568. .moz-reader-content a:visited {
  569. color: #c2e;
  570. }
  571. .moz-reader-content * {
  572. max-width: 100%;
  573. height: auto;
  574. }
  575. .moz-reader-content p,
  576. .moz-reader-content p,
  577. .moz-reader-content code,
  578. .moz-reader-content pre,
  579. .moz-reader-content blockquote,
  580. .moz-reader-content ul,
  581. .moz-reader-content ol,
  582. .moz-reader-content li,
  583. .moz-reader-content figure,
  584. .moz-reader-content .wp-caption {
  585. margin: -10px -10px 20px -10px;
  586. padding: 10px;
  587. border-radius: 5px;
  588. }
  589. .moz-reader-content li {
  590. margin-bottom: 0;
  591. }
  592. .moz-reader-content li > ul,
  593. .moz-reader-content li > ol {
  594. margin-bottom: -10px;
  595. }
  596. .moz-reader-content p > img:only-child,
  597. .moz-reader-content p > a:only-child > img:only-child,
  598. .moz-reader-content .wp-caption img,
  599. .moz-reader-content figure img {
  600. display: block;
  601. }
  602. .moz-reader-content img[moz-reader-center] {
  603. margin-left: auto;
  604. margin-right: auto;
  605. }
  606. .moz-reader-content .caption,
  607. .moz-reader-content .wp-caption-text
  608. .moz-reader-content figcaption {
  609. font-size: 0.9em;
  610. line-height: 1.48em;
  611. font-style: italic;
  612. }
  613. .moz-reader-content code,
  614. .moz-reader-content pre {
  615. white-space: pre-wrap;
  616. }
  617. .moz-reader-content blockquote {
  618. padding: 0;
  619. padding-inline-start: 16px;
  620. }
  621. .moz-reader-content ul,
  622. .moz-reader-content ol {
  623. padding: 0;
  624. }
  625. .moz-reader-content ul {
  626. padding-inline-start: 30px;
  627. list-style: disc;
  628. }
  629. .moz-reader-content ol {
  630. padding-inline-start: 30px;
  631. list-style: decimal;
  632. }
  633. table,
  634. th,
  635. td {
  636. border: 1px solid currentColor;
  637. border-collapse: collapse;
  638. padding: 6px;
  639. vertical-align: top;
  640. }
  641. table {
  642. margin: 5px;
  643. }
  644. /* Visually hide (but don't display: none) screen reader elements */
  645. .moz-reader-content .visually-hidden,
  646. .moz-reader-content .visuallyhidden,
  647. .moz-reader-content .sr-only {
  648. display: inline-block;
  649. width: 1px;
  650. height: 1px;
  651. margin: -1px;
  652. overflow: hidden;
  653. padding: 0;
  654. border-width: 0;
  655. }
  656. /* Hide elements with common "hidden" class names */
  657. .moz-reader-content .hidden,
  658. .moz-reader-content .invisible {
  659. display: none;
  660. }
  661. /* Enforce wordpress and similar emoji/smileys aren't sized to be full-width,
  662. * see bug 1399616 for context. */
  663. .moz-reader-content img.wp-smiley,
  664. .moz-reader-content img.emoji {
  665. display: inline-block;
  666. border-width: 0;
  667. /* height: auto is implied from .moz-reader-content * rule. */
  668. width: 1em;
  669. margin: 0 .07em;
  670. padding: 0;
  671. }
  672. .reader-show-element {
  673. display: initial;
  674. }`;
  675. let NOTES_WEB_STYLESHEET, MASK_WEB_STYLESHEET, HIGHLIGHTS_WEB_STYLESHEET;
  676. let selectedNote, anchorElement, maskNoteElement, maskPageElement, highlightSelectionMode, removeHighlightMode, resizingNoteMode, movingNoteMode, highlightColor, collapseNoteTimeout, cuttingMode;
  677. let removedElements = [];
  678. window.onmessage = async event => {
  679. const message = JSON.parse(event.data);
  680. if (message.method == "init") {
  681. await init(message.content);
  682. }
  683. if (message.method == "addNote") {
  684. addNote(message);
  685. }
  686. if (message.method == "displayNotes") {
  687. document.querySelectorAll(NOTE_TAGNAME).forEach(noteElement => noteElement.shadowRoot.querySelector("." + NOTE_CLASS).classList.remove(NOTE_HIDDEN_CLASS));
  688. }
  689. if (message.method == "hideNotes") {
  690. document.querySelectorAll(NOTE_TAGNAME).forEach(noteElement => noteElement.shadowRoot.querySelector("." + NOTE_CLASS).classList.add(NOTE_HIDDEN_CLASS));
  691. }
  692. if (message.method == "enableHighlight") {
  693. highlightColor = message.color;
  694. highlightSelectionMode = true;
  695. }
  696. if (message.method == "disableHighlight") {
  697. highlightSelectionMode = false;
  698. }
  699. if (message.method == "displayHighlights") {
  700. document.querySelectorAll("." + HIGHLIGHT_CLASS).forEach(noteElement => noteElement.classList.remove(HIGHLIGHT_HIDDEN_CLASS));
  701. }
  702. if (message.method == "hideHighlights") {
  703. document.querySelectorAll("." + HIGHLIGHT_CLASS).forEach(noteElement => noteElement.classList.add(HIGHLIGHT_HIDDEN_CLASS));
  704. }
  705. if (message.method == "enableRemoveHighlights") {
  706. removeHighlightMode = true;
  707. }
  708. if (message.method == "disableRemoveHighlights") {
  709. removeHighlightMode = false;
  710. }
  711. if (message.method == "enableEditPage") {
  712. document.body.contentEditable = true;
  713. }
  714. if (message.method == "formatPage") {
  715. formatPage(true);
  716. }
  717. if (message.method == "formatPageNoTheme") {
  718. formatPage(false);
  719. }
  720. if (message.method == "disableEditPage") {
  721. document.body.contentEditable = false;
  722. }
  723. if (message.method == "enableCutPage") {
  724. cuttingMode = true;
  725. document.body.addEventListener("mouseover", highlightElementToCut);
  726. document.body.addEventListener("mouseout", highlightElementToCut);
  727. }
  728. if (message.method == "disableCutPage") {
  729. cuttingMode = false;
  730. document.body.removeEventListener("mouseover", highlightElementToCut);
  731. document.body.removeEventListener("mouseout", highlightElementToCut);
  732. }
  733. if (message.method == "undoCutPage") {
  734. if (removedElements.length) {
  735. removedElements.pop().classList.remove(REMOVED_CONTENT_CLASS);
  736. }
  737. }
  738. if (message.method == "undoAllCutPage") {
  739. while (removedElements.length) {
  740. removedElements.pop().classList.remove(REMOVED_CONTENT_CLASS);
  741. }
  742. }
  743. if (message.method == "getContent") {
  744. getContent(message.compressHTML);
  745. }
  746. };
  747. window.onresize = reflowNotes;
  748. async function init(content) {
  749. await initConstants();
  750. const contentDocument = (new DOMParser()).parseFromString(content, "text/html");
  751. if (contentDocument.doctype) {
  752. if (document.doctype) {
  753. document.replaceChild(contentDocument.doctype, document.doctype);
  754. } else {
  755. document.insertBefore(contentDocument.doctype, document.documentElement);
  756. }
  757. } else {
  758. document.doctype.remove();
  759. }
  760. contentDocument.querySelectorAll("noscript").forEach(element => {
  761. element.setAttribute(DISABLED_NOSCRIPT_ATTRIBUTE_NAME, element.innerHTML);
  762. element.textContent = "";
  763. });
  764. document.replaceChild(contentDocument.documentElement, document.documentElement);
  765. deserializeShadowRoots(document);
  766. const iconElement = document.querySelector("link[rel*=icon]");
  767. window.parent.postMessage(JSON.stringify({ "method": "setMetadata", title: document.title, icon: iconElement && iconElement.href }), "*");
  768. if (!isProbablyReaderable(document)) {
  769. window.parent.postMessage(JSON.stringify({ "method": "disableFormatPage" }), "*");
  770. }
  771. document.querySelectorAll(NOTE_TAGNAME).forEach(containerElement => attachNoteListeners(containerElement, true));
  772. document.documentElement.appendChild(getStyleElement(HIGHLIGHTS_WEB_STYLESHEET));
  773. maskPageElement = getMaskElement(PAGE_MASK_CLASS, PAGE_MASK_CONTAINER_CLASS);
  774. maskNoteElement = getMaskElement(NOTE_MASK_CLASS);
  775. document.documentElement.onmouseup = document.documentElement.ontouchend = onMouseUp;
  776. window.onclick = event => event.preventDefault();
  777. }
  778. async function initConstants() {
  779. [NOTES_WEB_STYLESHEET, MASK_WEB_STYLESHEET, HIGHLIGHTS_WEB_STYLESHEET] = await Promise.all([
  780. minifyText(await ((await fetch("../pages/editor-note-web.css")).text())),
  781. minifyText(await ((await fetch("../pages/editor-mask-web.css")).text())),
  782. minifyText(await ((await fetch("../pages/editor-frame-web.css")).text()))
  783. ]);
  784. }
  785. function addNote({ color }) {
  786. const containerElement = document.createElement(NOTE_TAGNAME);
  787. const noteElement = document.createElement("div");
  788. const headerElement = document.createElement("header");
  789. const mainElement = document.createElement("textarea");
  790. const resizeElement = document.createElement("div");
  791. const removeNoteElement = document.createElement("img");
  792. const anchorIconElement = document.createElement("img");
  793. const noteShadow = containerElement.attachShadow({ mode: "open" });
  794. headerElement.appendChild(anchorIconElement);
  795. headerElement.appendChild(removeNoteElement);
  796. noteElement.appendChild(headerElement);
  797. noteElement.appendChild(mainElement);
  798. noteElement.appendChild(resizeElement);
  799. noteShadow.appendChild(getStyleElement(NOTES_WEB_STYLESHEET));
  800. noteShadow.appendChild(noteElement);
  801. const notesElements = Array.from(document.querySelectorAll(NOTE_TAGNAME));
  802. const noteId = Math.max.call(Math, 0, ...notesElements.map(noteElement => Number(noteElement.dataset.noteId))) + 1;
  803. noteElement.classList.add(NOTE_CLASS);
  804. noteElement.classList.add(NOTE_ANCHORED_CLASS);
  805. noteElement.classList.add(color);
  806. const boundingRectDocument = document.documentElement.getBoundingClientRect();
  807. let positionX = NOTE_INITIAL_WIDTH + NOTE_INITIAL_POSITION_X - 1 - boundingRectDocument.x;
  808. let positionY = NOTE_INITIAL_HEIGHT + NOTE_INITIAL_POSITION_Y - 1 - boundingRectDocument.y;
  809. while (Array.from(document.elementsFromPoint(positionX - window.scrollX, positionY - window.scrollY)).find(element => element.tagName.toLowerCase() == NOTE_TAGNAME)) {
  810. positionX += NOTE_INITIAL_POSITION_X;
  811. positionY += NOTE_INITIAL_POSITION_Y;
  812. }
  813. noteElement.style.setProperty("left", (positionX - NOTE_INITIAL_WIDTH - 1) + "px");
  814. noteElement.style.setProperty("top", (positionY - NOTE_INITIAL_HEIGHT - 1) + "px");
  815. resizeElement.className = "note-resize";
  816. resizeElement.ondragstart = event => event.preventDefault();
  817. removeNoteElement.className = "note-remove";
  818. removeNoteElement.src = BUTTON_CLOSE_URL;
  819. removeNoteElement.ondragstart = event => event.preventDefault();
  820. anchorIconElement.className = "note-anchor";
  821. anchorIconElement.src = BUTTON_ANCHOR_URL;
  822. anchorIconElement.ondragstart = event => event.preventDefault();
  823. containerElement.dataset.noteId = noteId;
  824. addNoteRef(document.documentElement, noteId);
  825. attachNoteListeners(containerElement, true);
  826. document.documentElement.insertBefore(containerElement, maskPageElement.getRootNode().host);
  827. noteElement.classList.add(NOTE_SELECTED_CLASS);
  828. selectedNote = noteElement;
  829. }
  830. function attachNoteListeners(containerElement, editable = false) {
  831. const SELECT_PX_THRESHOLD = 4;
  832. const COLLAPSING_NOTE_DELAY = 750;
  833. const noteShadow = containerElement.shadowRoot;
  834. const noteElement = noteShadow.childNodes[1];
  835. const headerElement = noteShadow.querySelector("header");
  836. const mainElement = noteShadow.querySelector("textarea");
  837. const noteId = containerElement.dataset.noteId;
  838. const resizeElement = noteShadow.querySelector(".note-resize");
  839. const anchorIconElement = noteShadow.querySelector(".note-anchor");
  840. const removeNoteElement = noteShadow.querySelector(".note-remove");
  841. mainElement.readOnly = !editable;
  842. if (!editable) {
  843. anchorIconElement.style.setProperty("display", "none", "important");
  844. } else {
  845. anchorIconElement.style.removeProperty("display");
  846. }
  847. headerElement.ontouchstart = headerElement.onmousedown = event => {
  848. if (event.target == headerElement) {
  849. collapseNoteTimeout = setTimeout(() => noteElement.classList.toggle("note-collapsed"), COLLAPSING_NOTE_DELAY);
  850. event.preventDefault();
  851. const position = getPosition(event);
  852. const clientX = position.clientX;
  853. const clientY = position.clientY;
  854. const boundingRect = noteElement.getBoundingClientRect();
  855. const deltaX = clientX - boundingRect.left;
  856. const deltaY = clientY - boundingRect.top;
  857. maskPageElement.classList.add(PAGE_MASK_ACTIVE_CLASS);
  858. document.documentElement.style.setProperty("user-select", "none", "important");
  859. anchorElement = getAnchorElement(containerElement);
  860. displayMaskNote();
  861. selectNote(noteElement);
  862. moveNote(event, deltaX, deltaY);
  863. movingNoteMode = { event, deltaX, deltaY };
  864. document.documentElement.ontouchmove = document.documentElement.onmousemove = event => {
  865. clearTimeout(collapseNoteTimeout);
  866. if (!movingNoteMode) {
  867. movingNoteMode = { deltaX, deltaY };
  868. }
  869. movingNoteMode.event = event;
  870. moveNote(event, deltaX, deltaY);
  871. };
  872. }
  873. };
  874. resizeElement.ontouchstart = resizeElement.onmousedown = event => {
  875. event.preventDefault();
  876. resizingNoteMode = true;
  877. selectNote(noteElement);
  878. maskPageElement.classList.add(PAGE_MASK_ACTIVE_CLASS);
  879. document.documentElement.style.setProperty("user-select", "none", "important");
  880. document.documentElement.ontouchmove = document.documentElement.onmousemove = event => {
  881. event.preventDefault();
  882. const { clientX, clientY } = getPosition(event);
  883. const boundingRectNote = noteElement.getBoundingClientRect();
  884. noteElement.style.width = clientX - boundingRectNote.left + "px";
  885. noteElement.style.height = clientY - boundingRectNote.top + "px";
  886. };
  887. };
  888. anchorIconElement.ontouchend = anchorIconElement.onclick = event => {
  889. event.preventDefault();
  890. noteElement.classList.toggle(NOTE_ANCHORED_CLASS);
  891. if (!noteElement.classList.contains(NOTE_ANCHORED_CLASS)) {
  892. deleteNoteRef(containerElement, noteId);
  893. addNoteRef(document.documentElement, noteId);
  894. }
  895. };
  896. removeNoteElement.ontouchend = removeNoteElement.onclick = event => {
  897. event.preventDefault();
  898. deleteNoteRef(containerElement, noteId);
  899. containerElement.remove();
  900. };
  901. noteElement.onmousedown = () => {
  902. selectNote(noteElement);
  903. };
  904. function moveNote(event, deltaX, deltaY) {
  905. event.preventDefault();
  906. const { clientX, clientY } = getPosition(event);
  907. noteElement.classList.add(NOTE_MOVING_CLASS);
  908. if (editable) {
  909. if (noteElement.classList.contains(NOTE_ANCHORED_CLASS)) {
  910. deleteNoteRef(containerElement, noteId);
  911. anchorElement = getTarget(clientX, clientY) || document.documentElement;
  912. addNoteRef(anchorElement, noteId);
  913. } else {
  914. anchorElement = document.documentElement;
  915. }
  916. }
  917. document.documentElement.insertBefore(containerElement, maskPageElement.getRootNode().host);
  918. noteElement.style.setProperty("left", (clientX - deltaX) + "px");
  919. noteElement.style.setProperty("top", (clientY - deltaY) + "px");
  920. noteElement.style.setProperty("position", "fixed");
  921. displayMaskNote();
  922. }
  923. function displayMaskNote() {
  924. if (anchorElement == document.documentElement || anchorElement == document.documentElement) {
  925. maskNoteElement.classList.remove(NOTE_MASK_MOVING_CLASS);
  926. } else {
  927. const boundingRectAnchor = anchorElement.getBoundingClientRect();
  928. maskNoteElement.classList.add(NOTE_MASK_MOVING_CLASS);
  929. maskNoteElement.style.setProperty("top", boundingRectAnchor.y + "px");
  930. maskNoteElement.style.setProperty("left", boundingRectAnchor.x + "px");
  931. maskNoteElement.style.setProperty("width", boundingRectAnchor.width + "px");
  932. maskNoteElement.style.setProperty("height", boundingRectAnchor.height + "px");
  933. }
  934. }
  935. function selectNote(noteElement) {
  936. if (selectedNote) {
  937. selectedNote.classList.remove(NOTE_SELECTED_CLASS);
  938. }
  939. noteElement.classList.add(NOTE_SELECTED_CLASS);
  940. selectedNote = noteElement;
  941. }
  942. function getTarget(clientX, clientY) {
  943. const targets = Array.from(document.elementsFromPoint(clientX, clientY)).filter(element => element.tagName.toLowerCase() != NOTE_TAGNAME && !element.classList.contains(MASK_CLASS));
  944. if (!targets.includes(document.documentElement)) {
  945. targets.push(document.documentElement);
  946. }
  947. let newTarget, target = targets[0], boundingRect = target.getBoundingClientRect();
  948. newTarget = determineTargetElement("floor", target, clientX - boundingRect.left, getMatchedParents(target, "left"));
  949. if (newTarget == target) {
  950. newTarget = determineTargetElement("ceil", target, boundingRect.left + boundingRect.width - clientX, getMatchedParents(target, "right"));
  951. }
  952. if (newTarget == target) {
  953. newTarget = determineTargetElement("floor", target, clientY - boundingRect.top, getMatchedParents(target, "top"));
  954. }
  955. if (newTarget == target) {
  956. newTarget = determineTargetElement("ceil", target, boundingRect.top + boundingRect.height - clientY, getMatchedParents(target, "bottom"));
  957. }
  958. target = newTarget;
  959. while (boundingRect = target && target.getBoundingClientRect(), boundingRect && boundingRect.width <= SELECT_PX_THRESHOLD && boundingRect.height <= SELECT_PX_THRESHOLD) {
  960. target = target.parentElement;
  961. }
  962. return target;
  963. }
  964. function getMatchedParents(target, property) {
  965. let element = target, matchedParent, parents = [];
  966. do {
  967. const boundingRect = element.getBoundingClientRect();
  968. if (element.parentElement && !element.parentElement.tagName.toLowerCase() != NOTE_TAGNAME && !element.classList.contains(MASK_CLASS)) {
  969. const parentBoundingRect = element.parentElement.getBoundingClientRect();
  970. matchedParent = Math.abs(parentBoundingRect[property] - boundingRect[property]) <= SELECT_PX_THRESHOLD;
  971. if (matchedParent) {
  972. if (element.parentElement.clientWidth > SELECT_PX_THRESHOLD && element.parentElement.clientHeight > SELECT_PX_THRESHOLD &&
  973. ((element.parentElement.clientWidth - element.clientWidth > SELECT_PX_THRESHOLD) || (element.parentElement.clientHeight - element.clientHeight > SELECT_PX_THRESHOLD))) {
  974. parents.push(element.parentElement);
  975. }
  976. element = element.parentElement;
  977. }
  978. } else {
  979. matchedParent = false;
  980. }
  981. } while (matchedParent && element);
  982. return parents;
  983. }
  984. function determineTargetElement(roundingMethod, target, widthDistance, parents) {
  985. if (Math[roundingMethod](widthDistance / SELECT_PX_THRESHOLD) <= parents.length) {
  986. target = parents[parents.length - Math[roundingMethod](widthDistance / SELECT_PX_THRESHOLD) - 1];
  987. }
  988. return target;
  989. }
  990. }
  991. function onMouseUp(event) {
  992. if (highlightSelectionMode) {
  993. highlightSelection();
  994. }
  995. if (removeHighlightMode) {
  996. let element = event.target, done;
  997. while (element && !done) {
  998. if (element.classList.contains(HIGHLIGHT_CLASS)) {
  999. document.querySelectorAll("." + HIGHLIGHT_CLASS + "[data-singlefile-highlight-id=" + JSON.stringify(element.dataset.singlefileHighlightId) + "]").forEach(highlightedElement => {
  1000. resetHighlightedElement(highlightedElement);
  1001. });
  1002. done = true;
  1003. }
  1004. element = element.parentElement;
  1005. }
  1006. }
  1007. if (resizingNoteMode) {
  1008. resizingNoteMode = false;
  1009. document.documentElement.style.removeProperty("user-select");
  1010. maskPageElement.classList.remove(PAGE_MASK_ACTIVE_CLASS);
  1011. document.documentElement.ontouchmove = document.documentElement.onmousemove = null;
  1012. }
  1013. if (movingNoteMode) {
  1014. anchorNote(movingNoteMode.event || event, selectedNote, movingNoteMode.deltaX, movingNoteMode.deltaY);
  1015. movingNoteMode = null;
  1016. document.documentElement.ontouchmove = document.documentElement.onmousemove = null;
  1017. }
  1018. if (collapseNoteTimeout) {
  1019. clearTimeout(collapseNoteTimeout);
  1020. collapseNoteTimeout = null;
  1021. }
  1022. if (cuttingMode) {
  1023. let element = event.target;
  1024. if (document.documentElement != element && element.tagName.toLowerCase() != NOTE_TAGNAME) {
  1025. element.classList.add(REMOVED_CONTENT_CLASS);
  1026. removedElements.push(element);
  1027. }
  1028. }
  1029. }
  1030. function anchorNote(event, noteElement, deltaX, deltaY) {
  1031. event.preventDefault();
  1032. const { clientX, clientY } = getPosition(event);
  1033. document.documentElement.style.removeProperty("user-select");
  1034. noteElement.classList.remove(NOTE_MOVING_CLASS);
  1035. maskNoteElement.classList.remove(NOTE_MASK_MOVING_CLASS);
  1036. maskPageElement.classList.remove(PAGE_MASK_ACTIVE_CLASS);
  1037. const headerElement = noteElement.querySelector("header");
  1038. headerElement.ontouchmove = document.documentElement.onmousemove = null;
  1039. let currentElement = anchorElement;
  1040. let positionedElement;
  1041. while (currentElement.parentElement && !positionedElement) {
  1042. if (!FORBIDDEN_TAG_NAMES.includes(currentElement.tagName.toLowerCase())) {
  1043. const currentElementStyle = getComputedStyle(currentElement);
  1044. if (currentElementStyle.position != "static") {
  1045. positionedElement = currentElement;
  1046. }
  1047. }
  1048. currentElement = currentElement.parentElement;
  1049. }
  1050. if (!positionedElement) {
  1051. positionedElement = document.documentElement;
  1052. }
  1053. const containerElement = noteElement.getRootNode().host;
  1054. if (positionedElement == document.documentElement) {
  1055. const firstMaskElement = document.querySelector("." + MASK_CLASS);
  1056. document.documentElement.insertBefore(containerElement, firstMaskElement);
  1057. } else {
  1058. positionedElement.appendChild(containerElement);
  1059. }
  1060. const boundingRectPositionedElement = positionedElement.getBoundingClientRect();
  1061. const stylePositionedElement = window.getComputedStyle(positionedElement);
  1062. const borderX = parseInt(stylePositionedElement.getPropertyValue("border-left-width"));
  1063. const borderY = parseInt(stylePositionedElement.getPropertyValue("border-top-width"));
  1064. noteElement.style.setProperty("position", "absolute");
  1065. noteElement.style.setProperty("left", (clientX - boundingRectPositionedElement.x - deltaX - borderX) + "px");
  1066. noteElement.style.setProperty("top", (clientY - boundingRectPositionedElement.y - deltaY - borderY) + "px");
  1067. }
  1068. function getPosition(event) {
  1069. if (event.touches && event.touches.length) {
  1070. const touch = event.touches[0];
  1071. return touch;
  1072. } else {
  1073. return event;
  1074. }
  1075. }
  1076. function highlightSelection() {
  1077. let highlightId = 0;
  1078. document.querySelectorAll("." + HIGHLIGHT_CLASS).forEach(highlightedElement => highlightId = Math.max(highlightId, highlightedElement.dataset.singlefileHighlightId));
  1079. highlightId++;
  1080. const selection = window.getSelection();
  1081. const highlightedNodes = new Set();
  1082. for (let indexRange = 0; indexRange < selection.rangeCount; indexRange++) {
  1083. const range = selection.getRangeAt(indexRange);
  1084. if (!range.collapsed) {
  1085. if (range.commonAncestorContainer.nodeType == range.commonAncestorContainer.TEXT_NODE) {
  1086. let contentText = range.startContainer.splitText(range.startOffset);
  1087. contentText = contentText.splitText(range.endOffset);
  1088. addHighLightedNode(contentText.previousSibling);
  1089. } else {
  1090. const treeWalker = document.createTreeWalker(range.commonAncestorContainer, NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_TEXT);
  1091. let highlightNodes;
  1092. while (treeWalker.nextNode()) {
  1093. if (highlightNodes && !treeWalker.currentNode.contains(range.endContainer)) {
  1094. addHighLightedNode(treeWalker.currentNode);
  1095. }
  1096. if (treeWalker.currentNode == range.startContainer) {
  1097. if (range.startContainer.nodeType == range.startContainer.TEXT_NODE) {
  1098. const contentText = range.startContainer.splitText(range.startOffset);
  1099. treeWalker.nextNode();
  1100. addHighLightedNode(contentText);
  1101. } else {
  1102. addHighLightedNode(range.startContainer.childNodes[range.startOffset]);
  1103. }
  1104. highlightNodes = true;
  1105. }
  1106. if (treeWalker.currentNode == range.endContainer) {
  1107. if (range.endContainer.nodeType == range.endContainer.TEXT_NODE) {
  1108. const contentText = range.endContainer.splitText(range.endOffset);
  1109. treeWalker.nextNode();
  1110. addHighLightedNode(contentText.previousSibling);
  1111. } else {
  1112. addHighLightedNode(range.endContainer.childNodes[range.endOffset]);
  1113. }
  1114. highlightNodes = false;
  1115. }
  1116. }
  1117. range.collapse();
  1118. }
  1119. }
  1120. }
  1121. highlightedNodes.forEach(node => highlightNode(node));
  1122. function addHighLightedNode(node) {
  1123. if (node && node.textContent.trim()) {
  1124. if (node.nodeType == node.TEXT_NODE && node.parentElement.childNodes.length == 1 && node.parentElement.classList.contains(HIGHLIGHT_CLASS)) {
  1125. highlightedNodes.add(node.parentElement);
  1126. } else {
  1127. highlightedNodes.add(node);
  1128. }
  1129. }
  1130. }
  1131. function highlightNode(node) {
  1132. if (node.nodeType == node.ELEMENT_NODE) {
  1133. resetHighlightedElement(node);
  1134. node.classList.add(HIGHLIGHT_CLASS);
  1135. node.classList.add(highlightColor);
  1136. node.dataset.singlefileHighlightId = highlightId;
  1137. } else if (node.parentElement) {
  1138. highlightTextNode(node);
  1139. }
  1140. }
  1141. function highlightTextNode(node) {
  1142. const spanElement = document.createElement("span");
  1143. spanElement.classList.add(HIGHLIGHT_CLASS);
  1144. spanElement.classList.add(highlightColor);
  1145. spanElement.textContent = node.textContent;
  1146. spanElement.dataset.singlefileHighlightId = highlightId;
  1147. node.parentNode.replaceChild(spanElement, node);
  1148. return spanElement;
  1149. }
  1150. }
  1151. function highlightElementToCut(event) {
  1152. if (event.type != "mouseover" && event.type != "mouseout") return;
  1153. var target = event.target;
  1154. if ("classList" in target) {
  1155. target.classList.toggle("single-file-hover");
  1156. }
  1157. event.stopPropagation();
  1158. }
  1159. function formatPage(applySystemTheme) {
  1160. const shadowRoots = {};
  1161. const classesToPreserve = ["single-file-highlight", "single-file-highlight-yellow", "single-file-highlight-green", "single-file-highlight-pink", "single-file-highlight-blue"];
  1162. document.querySelectorAll(NOTE_TAGNAME).forEach(containerElement => {
  1163. shadowRoots[containerElement.dataset.noteId] = containerElement.shadowRoot;
  1164. const className = "singlefile-note-id-" + containerElement.dataset.noteId;
  1165. containerElement.classList.add(className);
  1166. classesToPreserve.push(className);
  1167. });
  1168. const article = new Readability(document, { classesToPreserve }).parse();
  1169. removedElements = [];
  1170. document.body.innerHTML = "";
  1171. const domParser = new DOMParser();
  1172. const doc = domParser.parseFromString(article.content, "text/html");
  1173. const contentEditable = document.body.contentEditable;
  1174. document.documentElement.replaceChild(doc.body, document.body);
  1175. document.querySelectorAll(NOTE_TAGNAME).forEach(containerElement => {
  1176. const noteId = (Array.from(containerElement.classList).find(className => /singlefile-note-id-\d+/.test(className))).split("singlefile-note-id-")[1];
  1177. containerElement.classList.remove("singlefile-note-id-" + noteId);
  1178. containerElement.dataset.noteId = noteId;
  1179. if (!containerElement.shadowRoot) {
  1180. containerElement.attachShadow({ mode: "open" });
  1181. containerElement.shadowRoot.appendChild(shadowRoots[noteId]);
  1182. }
  1183. });
  1184. document.querySelectorAll(NOTE_TAGNAME).forEach(containerElement => containerElement.shadowRoot = shadowRoots[containerElement.dataset.noteId]);
  1185. document.body.contentEditable = contentEditable;
  1186. document.head.querySelectorAll("style").forEach(styleElement => styleElement.remove());
  1187. const styleElement = document.createElement("style");
  1188. styleElement.textContent = STYLE_FORMATTED_PAGE;
  1189. document.head.appendChild(styleElement);
  1190. document.body.classList.add("moz-reader-content");
  1191. document.body.classList.add("content-width6");
  1192. document.body.classList.add("reader-show-element");
  1193. document.body.classList.add("sans-serif");
  1194. document.body.classList.add("container");
  1195. document.body.classList.add("line-height4");
  1196. const prefersColorSchemeDark = matchMedia("(prefers-color-scheme: dark)");
  1197. if (applySystemTheme && prefersColorSchemeDark && prefersColorSchemeDark.matches) {
  1198. document.body.classList.add("dark");
  1199. }
  1200. document.body.style.setProperty("display", "block");
  1201. document.body.style.setProperty("padding", "24px");
  1202. const titleElement = document.createElement("h1");
  1203. titleElement.classList.add("reader-title");
  1204. titleElement.textContent = article.title;
  1205. document.body.insertBefore(titleElement, document.body.firstChild);
  1206. document.documentElement.appendChild(getStyleElement(HIGHLIGHTS_WEB_STYLESHEET));
  1207. maskPageElement = getMaskElement(PAGE_MASK_CLASS, PAGE_MASK_CONTAINER_CLASS);
  1208. maskNoteElement = getMaskElement(NOTE_MASK_CLASS);
  1209. reflowNotes();
  1210. }
  1211. function getContent(compressHTML) {
  1212. serializeShadowRoots(document);
  1213. const doc = document.cloneNode(true);
  1214. deserializeShadowRoots(doc);
  1215. deserializeShadowRoots(document);
  1216. doc.querySelectorAll("[" + DISABLED_NOSCRIPT_ATTRIBUTE_NAME + "]").forEach(element => {
  1217. element.textContent = element.getAttribute(DISABLED_NOSCRIPT_ATTRIBUTE_NAME);
  1218. element.removeAttribute(DISABLED_NOSCRIPT_ATTRIBUTE_NAME);
  1219. });
  1220. doc.querySelectorAll("." + MASK_CLASS + ", ." + REMOVED_CONTENT_CLASS).forEach(maskElement => maskElement.remove());
  1221. doc.querySelectorAll("." + HIGHLIGHT_CLASS).forEach(noteElement => noteElement.classList.remove(HIGHLIGHT_HIDDEN_CLASS));
  1222. doc.querySelectorAll(`template[${SHADOW_MODE_ATTRIBUTE_NAME}]`).forEach(templateElement => {
  1223. const noteElement = templateElement.querySelector("." + NOTE_CLASS);
  1224. if (noteElement) {
  1225. noteElement.classList.remove(NOTE_HIDDEN_CLASS);
  1226. }
  1227. const mainElement = templateElement.querySelector("textarea");
  1228. if (mainElement) {
  1229. mainElement.textContent = mainElement.value;
  1230. }
  1231. });
  1232. delete doc.body.contentEditable;
  1233. const scriptElement = doc.createElement("script");
  1234. scriptElement.setAttribute(SCRIPT_TEMPLATE_SHADOW_ROOT, "");
  1235. scriptElement.textContent = getEmbedScript();
  1236. doc.body.appendChild(scriptElement);
  1237. window.parent.postMessage(JSON.stringify({ "method": "setContent", content: singlefile.lib.modules.serializer.process(doc, compressHTML) }), "*");
  1238. }
  1239. function reflowNotes() {
  1240. document.querySelectorAll(NOTE_TAGNAME).forEach(containerElement => {
  1241. const noteElement = containerElement.shadowRoot.querySelector("." + NOTE_CLASS);
  1242. const noteBoundingRect = noteElement.getBoundingClientRect();
  1243. const anchorElement = getAnchorElement(containerElement);
  1244. const anchorBoundingRect = anchorElement.getBoundingClientRect();
  1245. const maxX = anchorBoundingRect.x + Math.max(0, anchorBoundingRect.width - noteBoundingRect.width);
  1246. const minX = anchorBoundingRect.x;
  1247. const maxY = anchorBoundingRect.y + Math.max(0, anchorBoundingRect.height - NOTE_HEADER_HEIGHT);
  1248. const minY = anchorBoundingRect.y;
  1249. let left = parseInt(noteElement.style.getPropertyValue("left"));
  1250. let top = parseInt(noteElement.style.getPropertyValue("top"));
  1251. if (noteBoundingRect.x > maxX) {
  1252. left -= noteBoundingRect.x - maxX;
  1253. }
  1254. if (noteBoundingRect.x < minX) {
  1255. left += minX - noteBoundingRect.x;
  1256. }
  1257. if (noteBoundingRect.y > maxY) {
  1258. top -= noteBoundingRect.y - maxY;
  1259. }
  1260. if (noteBoundingRect.y < minY) {
  1261. top += minY - noteBoundingRect.y;
  1262. }
  1263. noteElement.style.setProperty("position", "absolute");
  1264. noteElement.style.setProperty("left", left + "px");
  1265. noteElement.style.setProperty("top", top + "px");
  1266. });
  1267. }
  1268. function resetHighlightedElement(element) {
  1269. element.classList.remove(HIGHLIGHT_CLASS);
  1270. element.classList.remove("single-file-highlight-yellow");
  1271. element.classList.remove("single-file-highlight-pink");
  1272. element.classList.remove("single-file-highlight-blue");
  1273. element.classList.remove("single-file-highlight-green");
  1274. delete element.dataset.singlefileHighlightId;
  1275. }
  1276. function serializeShadowRoots(node) {
  1277. node.querySelectorAll("*").forEach(element => {
  1278. if (element.shadowRoot) {
  1279. serializeShadowRoots(element.shadowRoot);
  1280. const templateElement = document.createElement("template");
  1281. templateElement.setAttribute(SHADOW_MODE_ATTRIBUTE_NAME, "open");
  1282. templateElement.appendChild(element.shadowRoot);
  1283. element.appendChild(templateElement);
  1284. }
  1285. });
  1286. }
  1287. function deserializeShadowRoots(node) {
  1288. node.querySelectorAll(`template[${SHADOW_MODE_ATTRIBUTE_NAME}]`).forEach(element => {
  1289. if (element.parentElement) {
  1290. let shadowRoot = element.parentElement.shadowRoot;
  1291. if (shadowRoot) {
  1292. Array.from(element.childNodes).forEach(node => shadowRoot.appendChild(node));
  1293. element.remove();
  1294. } else {
  1295. shadowRoot = element.parentElement.attachShadow({ mode: "open" });
  1296. const contentDocument = (new DOMParser()).parseFromString(element.innerHTML, "text/html");
  1297. Array.from(contentDocument.head.childNodes).forEach(node => shadowRoot.appendChild(node));
  1298. Array.from(contentDocument.body.childNodes).forEach(node => shadowRoot.appendChild(node));
  1299. }
  1300. deserializeShadowRoots(shadowRoot);
  1301. }
  1302. });
  1303. }
  1304. function getMaskElement(className, containerClassName) {
  1305. let maskElement = document.documentElement.querySelector("." + className);
  1306. if (!maskElement) {
  1307. maskElement = document.createElement("div");
  1308. const maskContainerElement = document.createElement("div");
  1309. if (containerClassName) {
  1310. maskContainerElement.classList.add(containerClassName);
  1311. }
  1312. maskContainerElement.classList.add(MASK_CLASS);
  1313. const firstNote = document.querySelector(NOTE_TAGNAME);
  1314. if (firstNote && firstNote.parentElement == document.documentElement) {
  1315. document.documentElement.insertBefore(maskContainerElement, firstNote);
  1316. } else {
  1317. document.documentElement.appendChild(maskContainerElement);
  1318. }
  1319. maskElement.classList.add(className);
  1320. const maskShadow = maskContainerElement.attachShadow({ mode: "open" });
  1321. maskShadow.appendChild(getStyleElement(MASK_WEB_STYLESHEET));
  1322. maskShadow.appendChild(maskElement);
  1323. return maskElement;
  1324. }
  1325. }
  1326. function getEmbedScript() {
  1327. return minifyText(`(() => {
  1328. document.currentScript.remove();
  1329. const processNode = node => {
  1330. node.querySelectorAll("template[${SHADOW_MODE_ATTRIBUTE_NAME}]").forEach(element=>{
  1331. if (!element.parentElement.shadowRoot) {
  1332. const shadowRoot = element.parentElement.attachShadow({mode:element.getAttribute("${SHADOW_MODE_ATTRIBUTE_NAME}"),delegatesFocus:Boolean(element.getAttribute("${SHADOW_DELEGATE_FOCUS_ATTRIBUTE_NAME}"))});
  1333. shadowRoot.innerHTML = element.innerHTML;
  1334. element.remove();
  1335. processNode(shadowRoot);
  1336. }
  1337. })
  1338. };
  1339. const FORBIDDEN_TAG_NAMES = ${JSON.stringify(FORBIDDEN_TAG_NAMES)};
  1340. const NOTE_TAGNAME = ${JSON.stringify(NOTE_TAGNAME)};
  1341. const NOTE_CLASS = ${JSON.stringify(NOTE_CLASS)};
  1342. const NOTE_ANCHORED_CLASS = ${JSON.stringify(NOTE_ANCHORED_CLASS)};
  1343. const NOTE_SELECTED_CLASS = ${JSON.stringify(NOTE_SELECTED_CLASS)};
  1344. const NOTE_MOVING_CLASS = ${JSON.stringify(NOTE_MOVING_CLASS)};
  1345. const NOTE_MASK_MOVING_CLASS = ${JSON.stringify(NOTE_MASK_MOVING_CLASS)};
  1346. const MASK_CLASS = ${JSON.stringify(MASK_CLASS)};
  1347. const HIGHLIGHT_CLASS = ${JSON.stringify(HIGHLIGHT_CLASS)};
  1348. const NOTES_WEB_STYLESHEET = ${JSON.stringify(NOTES_WEB_STYLESHEET)};
  1349. const MASK_WEB_STYLESHEET = ${JSON.stringify(MASK_WEB_STYLESHEET)};
  1350. const NOTE_HEADER_HEIGHT = ${JSON.stringify(NOTE_HEADER_HEIGHT)};
  1351. const PAGE_MASK_ACTIVE_CLASS = ${JSON.stringify(PAGE_MASK_ACTIVE_CLASS)};
  1352. const REMOVED_CONTENT_CLASS = ${JSON.stringify(REMOVED_CONTENT_CLASS)};
  1353. const reflowNotes = ${minifyText(reflowNotes.toString())};
  1354. const addNoteRef = ${minifyText(addNoteRef.toString())};
  1355. const deleteNoteRef = ${minifyText(deleteNoteRef.toString())};
  1356. const getNoteRefs = ${minifyText(getNoteRefs.toString())};
  1357. const setNoteRefs = ${minifyText(setNoteRefs.toString())};
  1358. const getAnchorElement = ${minifyText(getAnchorElement.toString())};
  1359. const getMaskElement = ${minifyText(getMaskElement.toString())};
  1360. const getStyleElement = ${minifyText(getStyleElement.toString())};
  1361. const attachNoteListeners = ${minifyText(attachNoteListeners.toString())};
  1362. const anchorNote = ${minifyText(anchorNote.toString())};
  1363. const getPosition = ${minifyText(getPosition.toString())};
  1364. const onMouseUp = ${minifyText(onMouseUp.toString())};
  1365. const maskNoteElement = getMaskElement(${JSON.stringify(NOTE_MASK_CLASS)});
  1366. const maskPageElement = getMaskElement(${JSON.stringify(PAGE_MASK_CLASS)}, ${JSON.stringify(PAGE_MASK_CONTAINER_CLASS)});
  1367. let selectedNote, highlightSelectionMode, removeHighlightMode, resizingNoteMode, movingNoteMode, collapseNoteTimeout, cuttingMode;
  1368. window.onresize = reflowNotes;
  1369. document.documentElement.onmouseup = document.documentElement.ontouchend = onMouseUp;
  1370. window.addEventListener("DOMContentLoaded", () => {
  1371. processNode(document);
  1372. reflowNotes();
  1373. document.querySelectorAll(${JSON.stringify(NOTE_TAGNAME)}).forEach(noteElement => attachNoteListeners(noteElement));
  1374. });
  1375. })()`);
  1376. }
  1377. function getStyleElement(stylesheet) {
  1378. const linkElement = document.createElement("style");
  1379. linkElement.textContent = stylesheet;
  1380. return linkElement;
  1381. }
  1382. function getAnchorElement(containerElement) {
  1383. 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 + ",") + "]");
  1384. }
  1385. function addNoteRef(anchorElement, noteId) {
  1386. const noteRefs = getNoteRefs(anchorElement);
  1387. noteRefs.push(noteId);
  1388. setNoteRefs(anchorElement, noteRefs);
  1389. }
  1390. function deleteNoteRef(containerElement, noteId) {
  1391. const anchorElement = getAnchorElement(containerElement);
  1392. const noteRefs = getNoteRefs(anchorElement).filter(noteRefs => noteRefs != noteId);
  1393. if (noteRefs.length) {
  1394. setNoteRefs(anchorElement, noteRefs);
  1395. } else {
  1396. delete anchorElement.dataset.singleFileNoteRefs;
  1397. }
  1398. }
  1399. function getNoteRefs(anchorElement) {
  1400. return JSON.parse("[" + (anchorElement.dataset.singleFileNoteRefs || "") + "]");
  1401. }
  1402. function setNoteRefs(anchorElement, noteRefs) {
  1403. anchorElement.dataset.singleFileNoteRefs = noteRefs.toString();
  1404. }
  1405. function minifyText(text) {
  1406. return text.replace(/[\n\t\s]+/g, " ");
  1407. }
  1408. })();