content-ui-editor-web.js 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589
  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. contentDocument.querySelectorAll("iframe").forEach(element => {
  765. const pointerEvents = "pointer-events";
  766. element.style.setProperty("-sf-" + pointerEvents, element.style.getPropertyValue(pointerEvents), element.style.getPropertyPriority(pointerEvents));
  767. element.style.setProperty(pointerEvents, "none", "important");
  768. });
  769. document.replaceChild(contentDocument.documentElement, document.documentElement);
  770. deserializeShadowRoots(document);
  771. const iconElement = document.querySelector("link[rel*=icon]");
  772. window.parent.postMessage(JSON.stringify({ "method": "setMetadata", title: document.title, icon: iconElement && iconElement.href }), "*");
  773. if (!isProbablyReaderable(document)) {
  774. window.parent.postMessage(JSON.stringify({ "method": "disableFormatPage" }), "*");
  775. }
  776. document.querySelectorAll(NOTE_TAGNAME).forEach(containerElement => attachNoteListeners(containerElement, true));
  777. document.documentElement.appendChild(getStyleElement(HIGHLIGHTS_WEB_STYLESHEET));
  778. maskPageElement = getMaskElement(PAGE_MASK_CLASS, PAGE_MASK_CONTAINER_CLASS);
  779. maskNoteElement = getMaskElement(NOTE_MASK_CLASS);
  780. document.documentElement.onmouseup = document.documentElement.ontouchend = onMouseUp;
  781. window.onclick = event => event.preventDefault();
  782. }
  783. async function initConstants() {
  784. [NOTES_WEB_STYLESHEET, MASK_WEB_STYLESHEET, HIGHLIGHTS_WEB_STYLESHEET] = await Promise.all([
  785. minifyText(await ((await fetch("../pages/editor-note-web.css")).text())),
  786. minifyText(await ((await fetch("../pages/editor-mask-web.css")).text())),
  787. minifyText(await ((await fetch("../pages/editor-frame-web.css")).text()))
  788. ]);
  789. }
  790. function addNote({ color }) {
  791. const containerElement = document.createElement(NOTE_TAGNAME);
  792. const noteElement = document.createElement("div");
  793. const headerElement = document.createElement("header");
  794. const mainElement = document.createElement("textarea");
  795. const resizeElement = document.createElement("div");
  796. const removeNoteElement = document.createElement("img");
  797. const anchorIconElement = document.createElement("img");
  798. const noteShadow = containerElement.attachShadow({ mode: "open" });
  799. headerElement.appendChild(anchorIconElement);
  800. headerElement.appendChild(removeNoteElement);
  801. noteElement.appendChild(headerElement);
  802. noteElement.appendChild(mainElement);
  803. noteElement.appendChild(resizeElement);
  804. noteShadow.appendChild(getStyleElement(NOTES_WEB_STYLESHEET));
  805. noteShadow.appendChild(noteElement);
  806. const notesElements = Array.from(document.querySelectorAll(NOTE_TAGNAME));
  807. const noteId = Math.max.call(Math, 0, ...notesElements.map(noteElement => Number(noteElement.dataset.noteId))) + 1;
  808. noteElement.classList.add(NOTE_CLASS);
  809. noteElement.classList.add(NOTE_ANCHORED_CLASS);
  810. noteElement.classList.add(color);
  811. mainElement.dir = "auto";
  812. const boundingRectDocument = document.documentElement.getBoundingClientRect();
  813. let positionX = NOTE_INITIAL_WIDTH + NOTE_INITIAL_POSITION_X - 1 - boundingRectDocument.x;
  814. let positionY = NOTE_INITIAL_HEIGHT + NOTE_INITIAL_POSITION_Y - 1 - boundingRectDocument.y;
  815. while (Array.from(document.elementsFromPoint(positionX - window.scrollX, positionY - window.scrollY)).find(element => element.tagName.toLowerCase() == NOTE_TAGNAME)) {
  816. positionX += NOTE_INITIAL_POSITION_X;
  817. positionY += NOTE_INITIAL_POSITION_Y;
  818. }
  819. noteElement.style.setProperty("left", (positionX - NOTE_INITIAL_WIDTH - 1) + "px");
  820. noteElement.style.setProperty("top", (positionY - NOTE_INITIAL_HEIGHT - 1) + "px");
  821. resizeElement.className = "note-resize";
  822. resizeElement.ondragstart = event => event.preventDefault();
  823. removeNoteElement.className = "note-remove";
  824. removeNoteElement.src = BUTTON_CLOSE_URL;
  825. removeNoteElement.ondragstart = event => event.preventDefault();
  826. anchorIconElement.className = "note-anchor";
  827. anchorIconElement.src = BUTTON_ANCHOR_URL;
  828. anchorIconElement.ondragstart = event => event.preventDefault();
  829. containerElement.dataset.noteId = noteId;
  830. addNoteRef(document.documentElement, noteId);
  831. attachNoteListeners(containerElement, true);
  832. document.documentElement.insertBefore(containerElement, maskPageElement.getRootNode().host);
  833. noteElement.classList.add(NOTE_SELECTED_CLASS);
  834. selectedNote = noteElement;
  835. }
  836. function attachNoteListeners(containerElement, editable = false) {
  837. const SELECT_PX_THRESHOLD = 4;
  838. const COLLAPSING_NOTE_DELAY = 750;
  839. const noteShadow = containerElement.shadowRoot;
  840. const noteElement = noteShadow.childNodes[1];
  841. const headerElement = noteShadow.querySelector("header");
  842. const mainElement = noteShadow.querySelector("textarea");
  843. const noteId = containerElement.dataset.noteId;
  844. const resizeElement = noteShadow.querySelector(".note-resize");
  845. const anchorIconElement = noteShadow.querySelector(".note-anchor");
  846. const removeNoteElement = noteShadow.querySelector(".note-remove");
  847. mainElement.readOnly = !editable;
  848. if (!editable) {
  849. anchorIconElement.style.setProperty("display", "none", "important");
  850. } else {
  851. anchorIconElement.style.removeProperty("display");
  852. }
  853. headerElement.ontouchstart = headerElement.onmousedown = event => {
  854. if (event.target == headerElement) {
  855. collapseNoteTimeout = setTimeout(() => noteElement.classList.toggle("note-collapsed"), COLLAPSING_NOTE_DELAY);
  856. event.preventDefault();
  857. const position = getPosition(event);
  858. const clientX = position.clientX;
  859. const clientY = position.clientY;
  860. const boundingRect = noteElement.getBoundingClientRect();
  861. const deltaX = clientX - boundingRect.left;
  862. const deltaY = clientY - boundingRect.top;
  863. maskPageElement.classList.add(PAGE_MASK_ACTIVE_CLASS);
  864. document.documentElement.style.setProperty("user-select", "none", "important");
  865. anchorElement = getAnchorElement(containerElement);
  866. displayMaskNote();
  867. selectNote(noteElement);
  868. moveNote(event, deltaX, deltaY);
  869. movingNoteMode = { event, deltaX, deltaY };
  870. document.documentElement.ontouchmove = document.documentElement.onmousemove = event => {
  871. clearTimeout(collapseNoteTimeout);
  872. if (!movingNoteMode) {
  873. movingNoteMode = { deltaX, deltaY };
  874. }
  875. movingNoteMode.event = event;
  876. moveNote(event, deltaX, deltaY);
  877. };
  878. }
  879. };
  880. resizeElement.ontouchstart = resizeElement.onmousedown = event => {
  881. event.preventDefault();
  882. resizingNoteMode = true;
  883. selectNote(noteElement);
  884. maskPageElement.classList.add(PAGE_MASK_ACTIVE_CLASS);
  885. document.documentElement.style.setProperty("user-select", "none", "important");
  886. document.documentElement.ontouchmove = document.documentElement.onmousemove = event => {
  887. event.preventDefault();
  888. const { clientX, clientY } = getPosition(event);
  889. const boundingRectNote = noteElement.getBoundingClientRect();
  890. noteElement.style.width = clientX - boundingRectNote.left + "px";
  891. noteElement.style.height = clientY - boundingRectNote.top + "px";
  892. };
  893. };
  894. anchorIconElement.ontouchend = anchorIconElement.onclick = event => {
  895. event.preventDefault();
  896. noteElement.classList.toggle(NOTE_ANCHORED_CLASS);
  897. if (!noteElement.classList.contains(NOTE_ANCHORED_CLASS)) {
  898. deleteNoteRef(containerElement, noteId);
  899. addNoteRef(document.documentElement, noteId);
  900. }
  901. };
  902. removeNoteElement.ontouchend = removeNoteElement.onclick = event => {
  903. event.preventDefault();
  904. deleteNoteRef(containerElement, noteId);
  905. containerElement.remove();
  906. };
  907. noteElement.onmousedown = () => {
  908. selectNote(noteElement);
  909. };
  910. function moveNote(event, deltaX, deltaY) {
  911. event.preventDefault();
  912. const { clientX, clientY } = getPosition(event);
  913. noteElement.classList.add(NOTE_MOVING_CLASS);
  914. if (editable) {
  915. if (noteElement.classList.contains(NOTE_ANCHORED_CLASS)) {
  916. deleteNoteRef(containerElement, noteId);
  917. anchorElement = getTarget(clientX, clientY) || document.documentElement;
  918. addNoteRef(anchorElement, noteId);
  919. } else {
  920. anchorElement = document.documentElement;
  921. }
  922. }
  923. document.documentElement.insertBefore(containerElement, maskPageElement.getRootNode().host);
  924. noteElement.style.setProperty("left", (clientX - deltaX) + "px");
  925. noteElement.style.setProperty("top", (clientY - deltaY) + "px");
  926. noteElement.style.setProperty("position", "fixed");
  927. displayMaskNote();
  928. }
  929. function displayMaskNote() {
  930. if (anchorElement == document.documentElement || anchorElement == document.documentElement) {
  931. maskNoteElement.classList.remove(NOTE_MASK_MOVING_CLASS);
  932. } else {
  933. const boundingRectAnchor = anchorElement.getBoundingClientRect();
  934. maskNoteElement.classList.add(NOTE_MASK_MOVING_CLASS);
  935. maskNoteElement.style.setProperty("top", boundingRectAnchor.y + "px");
  936. maskNoteElement.style.setProperty("left", boundingRectAnchor.x + "px");
  937. maskNoteElement.style.setProperty("width", boundingRectAnchor.width + "px");
  938. maskNoteElement.style.setProperty("height", boundingRectAnchor.height + "px");
  939. }
  940. }
  941. function selectNote(noteElement) {
  942. if (selectedNote) {
  943. selectedNote.classList.remove(NOTE_SELECTED_CLASS);
  944. }
  945. noteElement.classList.add(NOTE_SELECTED_CLASS);
  946. selectedNote = noteElement;
  947. }
  948. function getTarget(clientX, clientY) {
  949. const targets = Array.from(document.elementsFromPoint(clientX, clientY)).filter(element => element.tagName.toLowerCase() != NOTE_TAGNAME && !element.classList.contains(MASK_CLASS));
  950. if (!targets.includes(document.documentElement)) {
  951. targets.push(document.documentElement);
  952. }
  953. let newTarget, target = targets[0], boundingRect = target.getBoundingClientRect();
  954. newTarget = determineTargetElement("floor", target, clientX - boundingRect.left, getMatchedParents(target, "left"));
  955. if (newTarget == target) {
  956. newTarget = determineTargetElement("ceil", target, boundingRect.left + boundingRect.width - clientX, getMatchedParents(target, "right"));
  957. }
  958. if (newTarget == target) {
  959. newTarget = determineTargetElement("floor", target, clientY - boundingRect.top, getMatchedParents(target, "top"));
  960. }
  961. if (newTarget == target) {
  962. newTarget = determineTargetElement("ceil", target, boundingRect.top + boundingRect.height - clientY, getMatchedParents(target, "bottom"));
  963. }
  964. target = newTarget;
  965. while (boundingRect = target && target.getBoundingClientRect(), boundingRect && boundingRect.width <= SELECT_PX_THRESHOLD && boundingRect.height <= SELECT_PX_THRESHOLD) {
  966. target = target.parentElement;
  967. }
  968. return target;
  969. }
  970. function getMatchedParents(target, property) {
  971. let element = target, matchedParent, parents = [];
  972. do {
  973. const boundingRect = element.getBoundingClientRect();
  974. if (element.parentElement && !element.parentElement.tagName.toLowerCase() != NOTE_TAGNAME && !element.classList.contains(MASK_CLASS)) {
  975. const parentBoundingRect = element.parentElement.getBoundingClientRect();
  976. matchedParent = Math.abs(parentBoundingRect[property] - boundingRect[property]) <= SELECT_PX_THRESHOLD;
  977. if (matchedParent) {
  978. if (element.parentElement.clientWidth > SELECT_PX_THRESHOLD && element.parentElement.clientHeight > SELECT_PX_THRESHOLD &&
  979. ((element.parentElement.clientWidth - element.clientWidth > SELECT_PX_THRESHOLD) || (element.parentElement.clientHeight - element.clientHeight > SELECT_PX_THRESHOLD))) {
  980. parents.push(element.parentElement);
  981. }
  982. element = element.parentElement;
  983. }
  984. } else {
  985. matchedParent = false;
  986. }
  987. } while (matchedParent && element);
  988. return parents;
  989. }
  990. function determineTargetElement(roundingMethod, target, widthDistance, parents) {
  991. if (Math[roundingMethod](widthDistance / SELECT_PX_THRESHOLD) <= parents.length) {
  992. target = parents[parents.length - Math[roundingMethod](widthDistance / SELECT_PX_THRESHOLD) - 1];
  993. }
  994. return target;
  995. }
  996. }
  997. function onMouseUp(event) {
  998. if (highlightSelectionMode) {
  999. highlightSelection();
  1000. }
  1001. if (removeHighlightMode) {
  1002. let element = event.target, done;
  1003. while (element && !done) {
  1004. if (element.classList.contains(HIGHLIGHT_CLASS)) {
  1005. document.querySelectorAll("." + HIGHLIGHT_CLASS + "[data-singlefile-highlight-id=" + JSON.stringify(element.dataset.singlefileHighlightId) + "]").forEach(highlightedElement => {
  1006. resetHighlightedElement(highlightedElement);
  1007. });
  1008. done = true;
  1009. }
  1010. element = element.parentElement;
  1011. }
  1012. }
  1013. if (resizingNoteMode) {
  1014. resizingNoteMode = false;
  1015. document.documentElement.style.removeProperty("user-select");
  1016. maskPageElement.classList.remove(PAGE_MASK_ACTIVE_CLASS);
  1017. document.documentElement.ontouchmove = document.documentElement.onmousemove = null;
  1018. }
  1019. if (movingNoteMode) {
  1020. anchorNote(movingNoteMode.event || event, selectedNote, movingNoteMode.deltaX, movingNoteMode.deltaY);
  1021. movingNoteMode = null;
  1022. document.documentElement.ontouchmove = document.documentElement.onmousemove = null;
  1023. }
  1024. if (collapseNoteTimeout) {
  1025. clearTimeout(collapseNoteTimeout);
  1026. collapseNoteTimeout = null;
  1027. }
  1028. if (cuttingMode) {
  1029. let element = event.target;
  1030. if (document.documentElement != element && element.tagName.toLowerCase() != NOTE_TAGNAME) {
  1031. element.classList.add(REMOVED_CONTENT_CLASS);
  1032. removedElements.push(element);
  1033. }
  1034. }
  1035. }
  1036. function anchorNote(event, noteElement, deltaX, deltaY) {
  1037. event.preventDefault();
  1038. const { clientX, clientY } = getPosition(event);
  1039. document.documentElement.style.removeProperty("user-select");
  1040. noteElement.classList.remove(NOTE_MOVING_CLASS);
  1041. maskNoteElement.classList.remove(NOTE_MASK_MOVING_CLASS);
  1042. maskPageElement.classList.remove(PAGE_MASK_ACTIVE_CLASS);
  1043. const headerElement = noteElement.querySelector("header");
  1044. headerElement.ontouchmove = document.documentElement.onmousemove = null;
  1045. let currentElement = anchorElement;
  1046. let positionedElement;
  1047. while (currentElement.parentElement && !positionedElement) {
  1048. if (!FORBIDDEN_TAG_NAMES.includes(currentElement.tagName.toLowerCase())) {
  1049. const currentElementStyle = getComputedStyle(currentElement);
  1050. if (currentElementStyle.position != "static") {
  1051. positionedElement = currentElement;
  1052. }
  1053. }
  1054. currentElement = currentElement.parentElement;
  1055. }
  1056. if (!positionedElement) {
  1057. positionedElement = document.documentElement;
  1058. }
  1059. const containerElement = noteElement.getRootNode().host;
  1060. if (positionedElement == document.documentElement) {
  1061. const firstMaskElement = document.querySelector("." + MASK_CLASS);
  1062. document.documentElement.insertBefore(containerElement, firstMaskElement);
  1063. } else {
  1064. positionedElement.appendChild(containerElement);
  1065. }
  1066. const boundingRectPositionedElement = positionedElement.getBoundingClientRect();
  1067. const stylePositionedElement = window.getComputedStyle(positionedElement);
  1068. const borderX = parseInt(stylePositionedElement.getPropertyValue("border-left-width"));
  1069. const borderY = parseInt(stylePositionedElement.getPropertyValue("border-top-width"));
  1070. noteElement.style.setProperty("position", "absolute");
  1071. noteElement.style.setProperty("left", (clientX - boundingRectPositionedElement.x - deltaX - borderX) + "px");
  1072. noteElement.style.setProperty("top", (clientY - boundingRectPositionedElement.y - deltaY - borderY) + "px");
  1073. }
  1074. function getPosition(event) {
  1075. if (event.touches && event.touches.length) {
  1076. const touch = event.touches[0];
  1077. return touch;
  1078. } else {
  1079. return event;
  1080. }
  1081. }
  1082. function highlightSelection() {
  1083. let highlightId = 0;
  1084. document.querySelectorAll("." + HIGHLIGHT_CLASS).forEach(highlightedElement => highlightId = Math.max(highlightId, highlightedElement.dataset.singlefileHighlightId));
  1085. highlightId++;
  1086. const selection = window.getSelection();
  1087. const highlightedNodes = new Set();
  1088. for (let indexRange = 0; indexRange < selection.rangeCount; indexRange++) {
  1089. const range = selection.getRangeAt(indexRange);
  1090. if (!range.collapsed) {
  1091. if (range.commonAncestorContainer.nodeType == range.commonAncestorContainer.TEXT_NODE) {
  1092. let contentText = range.startContainer.splitText(range.startOffset);
  1093. contentText = contentText.splitText(range.endOffset);
  1094. addHighLightedNode(contentText.previousSibling);
  1095. } else {
  1096. const treeWalker = document.createTreeWalker(range.commonAncestorContainer, NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_TEXT);
  1097. let highlightNodes;
  1098. while (treeWalker.nextNode()) {
  1099. if (highlightNodes && !treeWalker.currentNode.contains(range.endContainer)) {
  1100. addHighLightedNode(treeWalker.currentNode);
  1101. }
  1102. if (treeWalker.currentNode == range.startContainer) {
  1103. if (range.startContainer.nodeType == range.startContainer.TEXT_NODE) {
  1104. const contentText = range.startContainer.splitText(range.startOffset);
  1105. treeWalker.nextNode();
  1106. addHighLightedNode(contentText);
  1107. } else {
  1108. addHighLightedNode(range.startContainer.childNodes[range.startOffset]);
  1109. }
  1110. highlightNodes = true;
  1111. }
  1112. if (treeWalker.currentNode == range.endContainer) {
  1113. if (range.endContainer.nodeType == range.endContainer.TEXT_NODE) {
  1114. const contentText = range.endContainer.splitText(range.endOffset);
  1115. treeWalker.nextNode();
  1116. addHighLightedNode(contentText.previousSibling);
  1117. } else {
  1118. addHighLightedNode(range.endContainer.childNodes[range.endOffset]);
  1119. }
  1120. highlightNodes = false;
  1121. }
  1122. }
  1123. range.collapse();
  1124. }
  1125. }
  1126. }
  1127. highlightedNodes.forEach(node => highlightNode(node));
  1128. function addHighLightedNode(node) {
  1129. if (node && node.textContent.trim()) {
  1130. if (node.nodeType == node.TEXT_NODE && node.parentElement.childNodes.length == 1 && node.parentElement.classList.contains(HIGHLIGHT_CLASS)) {
  1131. highlightedNodes.add(node.parentElement);
  1132. } else {
  1133. highlightedNodes.add(node);
  1134. }
  1135. }
  1136. }
  1137. function highlightNode(node) {
  1138. if (node.nodeType == node.ELEMENT_NODE) {
  1139. resetHighlightedElement(node);
  1140. node.classList.add(HIGHLIGHT_CLASS);
  1141. node.classList.add(highlightColor);
  1142. node.dataset.singlefileHighlightId = highlightId;
  1143. } else if (node.parentElement) {
  1144. highlightTextNode(node);
  1145. }
  1146. }
  1147. function highlightTextNode(node) {
  1148. const spanElement = document.createElement("span");
  1149. spanElement.classList.add(HIGHLIGHT_CLASS);
  1150. spanElement.classList.add(highlightColor);
  1151. spanElement.textContent = node.textContent;
  1152. spanElement.dataset.singlefileHighlightId = highlightId;
  1153. node.parentNode.replaceChild(spanElement, node);
  1154. return spanElement;
  1155. }
  1156. }
  1157. function highlightElementToCut(event) {
  1158. if (event.type != "mouseover" && event.type != "mouseout") return;
  1159. var target = event.target;
  1160. if ("classList" in target) {
  1161. target.classList.toggle("single-file-hover");
  1162. }
  1163. event.stopPropagation();
  1164. }
  1165. function formatPage(applySystemTheme) {
  1166. const shadowRoots = {};
  1167. const classesToPreserve = ["single-file-highlight", "single-file-highlight-yellow", "single-file-highlight-green", "single-file-highlight-pink", "single-file-highlight-blue"];
  1168. document.querySelectorAll(NOTE_TAGNAME).forEach(containerElement => {
  1169. shadowRoots[containerElement.dataset.noteId] = containerElement.shadowRoot;
  1170. const className = "singlefile-note-id-" + containerElement.dataset.noteId;
  1171. containerElement.classList.add(className);
  1172. classesToPreserve.push(className);
  1173. });
  1174. const article = new Readability(document, { classesToPreserve }).parse();
  1175. removedElements = [];
  1176. document.body.innerHTML = "";
  1177. const domParser = new DOMParser();
  1178. const doc = domParser.parseFromString(article.content, "text/html");
  1179. const contentEditable = document.body.contentEditable;
  1180. document.documentElement.replaceChild(doc.body, document.body);
  1181. document.querySelectorAll(NOTE_TAGNAME).forEach(containerElement => {
  1182. const noteId = (Array.from(containerElement.classList).find(className => /singlefile-note-id-\d+/.test(className))).split("singlefile-note-id-")[1];
  1183. containerElement.classList.remove("singlefile-note-id-" + noteId);
  1184. containerElement.dataset.noteId = noteId;
  1185. if (!containerElement.shadowRoot) {
  1186. containerElement.attachShadow({ mode: "open" });
  1187. containerElement.shadowRoot.appendChild(shadowRoots[noteId]);
  1188. }
  1189. });
  1190. document.querySelectorAll(NOTE_TAGNAME).forEach(containerElement => containerElement.shadowRoot = shadowRoots[containerElement.dataset.noteId]);
  1191. document.body.contentEditable = contentEditable;
  1192. document.head.querySelectorAll("style").forEach(styleElement => styleElement.remove());
  1193. const styleElement = document.createElement("style");
  1194. styleElement.textContent = STYLE_FORMATTED_PAGE;
  1195. document.head.appendChild(styleElement);
  1196. document.body.classList.add("moz-reader-content");
  1197. document.body.classList.add("content-width6");
  1198. document.body.classList.add("reader-show-element");
  1199. document.body.classList.add("sans-serif");
  1200. document.body.classList.add("container");
  1201. document.body.classList.add("line-height4");
  1202. const prefersColorSchemeDark = matchMedia("(prefers-color-scheme: dark)");
  1203. if (applySystemTheme && prefersColorSchemeDark && prefersColorSchemeDark.matches) {
  1204. document.body.classList.add("dark");
  1205. }
  1206. document.body.style.setProperty("display", "block");
  1207. document.body.style.setProperty("padding", "24px");
  1208. const titleElement = document.createElement("h1");
  1209. titleElement.classList.add("reader-title");
  1210. titleElement.textContent = article.title;
  1211. document.body.insertBefore(titleElement, document.body.firstChild);
  1212. document.documentElement.appendChild(getStyleElement(HIGHLIGHTS_WEB_STYLESHEET));
  1213. maskPageElement = getMaskElement(PAGE_MASK_CLASS, PAGE_MASK_CONTAINER_CLASS);
  1214. maskNoteElement = getMaskElement(NOTE_MASK_CLASS);
  1215. reflowNotes();
  1216. }
  1217. function getContent(compressHTML) {
  1218. serializeShadowRoots(document);
  1219. const doc = document.cloneNode(true);
  1220. deserializeShadowRoots(doc);
  1221. deserializeShadowRoots(document);
  1222. doc.querySelectorAll("[" + DISABLED_NOSCRIPT_ATTRIBUTE_NAME + "]").forEach(element => {
  1223. element.textContent = element.getAttribute(DISABLED_NOSCRIPT_ATTRIBUTE_NAME);
  1224. element.removeAttribute(DISABLED_NOSCRIPT_ATTRIBUTE_NAME);
  1225. });
  1226. doc.querySelectorAll("." + MASK_CLASS + ", ." + REMOVED_CONTENT_CLASS).forEach(maskElement => maskElement.remove());
  1227. doc.querySelectorAll("." + HIGHLIGHT_CLASS).forEach(noteElement => noteElement.classList.remove(HIGHLIGHT_HIDDEN_CLASS));
  1228. doc.querySelectorAll(`template[${SHADOW_MODE_ATTRIBUTE_NAME}]`).forEach(templateElement => {
  1229. const noteElement = templateElement.querySelector("." + NOTE_CLASS);
  1230. if (noteElement) {
  1231. noteElement.classList.remove(NOTE_HIDDEN_CLASS);
  1232. }
  1233. const mainElement = templateElement.querySelector("textarea");
  1234. if (mainElement) {
  1235. mainElement.textContent = mainElement.value;
  1236. }
  1237. });
  1238. doc.querySelectorAll("iframe").forEach(element => {
  1239. const pointerEvents = "pointer-events";
  1240. element.style.setProperty(pointerEvents, element.style.getPropertyValue("-sf-" + pointerEvents), element.style.getPropertyPriority("-sf-" + pointerEvents));
  1241. element.style.removeProperty("-sf-" + pointerEvents);
  1242. });
  1243. delete doc.body.contentEditable;
  1244. const scriptElement = doc.createElement("script");
  1245. scriptElement.setAttribute(SCRIPT_TEMPLATE_SHADOW_ROOT, "");
  1246. scriptElement.textContent = getEmbedScript();
  1247. doc.body.appendChild(scriptElement);
  1248. window.parent.postMessage(JSON.stringify({ "method": "setContent", content: singlefile.lib.modules.serializer.process(doc, compressHTML) }), "*");
  1249. }
  1250. function reflowNotes() {
  1251. document.querySelectorAll(NOTE_TAGNAME).forEach(containerElement => {
  1252. const noteElement = containerElement.shadowRoot.querySelector("." + NOTE_CLASS);
  1253. const noteBoundingRect = noteElement.getBoundingClientRect();
  1254. const anchorElement = getAnchorElement(containerElement);
  1255. const anchorBoundingRect = anchorElement.getBoundingClientRect();
  1256. const maxX = anchorBoundingRect.x + Math.max(0, anchorBoundingRect.width - noteBoundingRect.width);
  1257. const minX = anchorBoundingRect.x;
  1258. const maxY = anchorBoundingRect.y + Math.max(0, anchorBoundingRect.height - NOTE_HEADER_HEIGHT);
  1259. const minY = anchorBoundingRect.y;
  1260. let left = parseInt(noteElement.style.getPropertyValue("left"));
  1261. let top = parseInt(noteElement.style.getPropertyValue("top"));
  1262. if (noteBoundingRect.x > maxX) {
  1263. left -= noteBoundingRect.x - maxX;
  1264. }
  1265. if (noteBoundingRect.x < minX) {
  1266. left += minX - noteBoundingRect.x;
  1267. }
  1268. if (noteBoundingRect.y > maxY) {
  1269. top -= noteBoundingRect.y - maxY;
  1270. }
  1271. if (noteBoundingRect.y < minY) {
  1272. top += minY - noteBoundingRect.y;
  1273. }
  1274. noteElement.style.setProperty("position", "absolute");
  1275. noteElement.style.setProperty("left", left + "px");
  1276. noteElement.style.setProperty("top", top + "px");
  1277. });
  1278. }
  1279. function resetHighlightedElement(element) {
  1280. element.classList.remove(HIGHLIGHT_CLASS);
  1281. element.classList.remove("single-file-highlight-yellow");
  1282. element.classList.remove("single-file-highlight-pink");
  1283. element.classList.remove("single-file-highlight-blue");
  1284. element.classList.remove("single-file-highlight-green");
  1285. delete element.dataset.singlefileHighlightId;
  1286. }
  1287. function serializeShadowRoots(node) {
  1288. node.querySelectorAll("*").forEach(element => {
  1289. if (element.shadowRoot) {
  1290. serializeShadowRoots(element.shadowRoot);
  1291. const templateElement = document.createElement("template");
  1292. templateElement.setAttribute(SHADOW_MODE_ATTRIBUTE_NAME, "open");
  1293. templateElement.appendChild(element.shadowRoot);
  1294. element.appendChild(templateElement);
  1295. }
  1296. });
  1297. }
  1298. function deserializeShadowRoots(node) {
  1299. node.querySelectorAll(`template[${SHADOW_MODE_ATTRIBUTE_NAME}]`).forEach(element => {
  1300. if (element.parentElement) {
  1301. let shadowRoot = element.parentElement.shadowRoot;
  1302. if (shadowRoot) {
  1303. Array.from(element.childNodes).forEach(node => shadowRoot.appendChild(node));
  1304. element.remove();
  1305. } else {
  1306. shadowRoot = element.parentElement.attachShadow({ mode: "open" });
  1307. const contentDocument = (new DOMParser()).parseFromString(element.innerHTML, "text/html");
  1308. Array.from(contentDocument.head.childNodes).forEach(node => shadowRoot.appendChild(node));
  1309. Array.from(contentDocument.body.childNodes).forEach(node => shadowRoot.appendChild(node));
  1310. }
  1311. deserializeShadowRoots(shadowRoot);
  1312. }
  1313. });
  1314. }
  1315. function getMaskElement(className, containerClassName) {
  1316. let maskElement = document.documentElement.querySelector("." + className);
  1317. if (!maskElement) {
  1318. maskElement = document.createElement("div");
  1319. const maskContainerElement = document.createElement("div");
  1320. if (containerClassName) {
  1321. maskContainerElement.classList.add(containerClassName);
  1322. }
  1323. maskContainerElement.classList.add(MASK_CLASS);
  1324. const firstNote = document.querySelector(NOTE_TAGNAME);
  1325. if (firstNote && firstNote.parentElement == document.documentElement) {
  1326. document.documentElement.insertBefore(maskContainerElement, firstNote);
  1327. } else {
  1328. document.documentElement.appendChild(maskContainerElement);
  1329. }
  1330. maskElement.classList.add(className);
  1331. const maskShadow = maskContainerElement.attachShadow({ mode: "open" });
  1332. maskShadow.appendChild(getStyleElement(MASK_WEB_STYLESHEET));
  1333. maskShadow.appendChild(maskElement);
  1334. return maskElement;
  1335. }
  1336. }
  1337. function getEmbedScript() {
  1338. return minifyText(`(() => {
  1339. document.currentScript.remove();
  1340. const processNode = node => {
  1341. node.querySelectorAll("template[${SHADOW_MODE_ATTRIBUTE_NAME}]").forEach(element=>{
  1342. if (!element.parentElement.shadowRoot) {
  1343. const shadowRoot = element.parentElement.attachShadow({mode:element.getAttribute("${SHADOW_MODE_ATTRIBUTE_NAME}"),delegatesFocus:Boolean(element.getAttribute("${SHADOW_DELEGATE_FOCUS_ATTRIBUTE_NAME}"))});
  1344. shadowRoot.innerHTML = element.innerHTML;
  1345. element.remove();
  1346. processNode(shadowRoot);
  1347. }
  1348. })
  1349. };
  1350. const FORBIDDEN_TAG_NAMES = ${JSON.stringify(FORBIDDEN_TAG_NAMES)};
  1351. const NOTE_TAGNAME = ${JSON.stringify(NOTE_TAGNAME)};
  1352. const NOTE_CLASS = ${JSON.stringify(NOTE_CLASS)};
  1353. const NOTE_ANCHORED_CLASS = ${JSON.stringify(NOTE_ANCHORED_CLASS)};
  1354. const NOTE_SELECTED_CLASS = ${JSON.stringify(NOTE_SELECTED_CLASS)};
  1355. const NOTE_MOVING_CLASS = ${JSON.stringify(NOTE_MOVING_CLASS)};
  1356. const NOTE_MASK_MOVING_CLASS = ${JSON.stringify(NOTE_MASK_MOVING_CLASS)};
  1357. const MASK_CLASS = ${JSON.stringify(MASK_CLASS)};
  1358. const HIGHLIGHT_CLASS = ${JSON.stringify(HIGHLIGHT_CLASS)};
  1359. const NOTES_WEB_STYLESHEET = ${JSON.stringify(NOTES_WEB_STYLESHEET)};
  1360. const MASK_WEB_STYLESHEET = ${JSON.stringify(MASK_WEB_STYLESHEET)};
  1361. const NOTE_HEADER_HEIGHT = ${JSON.stringify(NOTE_HEADER_HEIGHT)};
  1362. const PAGE_MASK_ACTIVE_CLASS = ${JSON.stringify(PAGE_MASK_ACTIVE_CLASS)};
  1363. const REMOVED_CONTENT_CLASS = ${JSON.stringify(REMOVED_CONTENT_CLASS)};
  1364. const reflowNotes = ${minifyText(reflowNotes.toString())};
  1365. const addNoteRef = ${minifyText(addNoteRef.toString())};
  1366. const deleteNoteRef = ${minifyText(deleteNoteRef.toString())};
  1367. const getNoteRefs = ${minifyText(getNoteRefs.toString())};
  1368. const setNoteRefs = ${minifyText(setNoteRefs.toString())};
  1369. const getAnchorElement = ${minifyText(getAnchorElement.toString())};
  1370. const getMaskElement = ${minifyText(getMaskElement.toString())};
  1371. const getStyleElement = ${minifyText(getStyleElement.toString())};
  1372. const attachNoteListeners = ${minifyText(attachNoteListeners.toString())};
  1373. const anchorNote = ${minifyText(anchorNote.toString())};
  1374. const getPosition = ${minifyText(getPosition.toString())};
  1375. const onMouseUp = ${minifyText(onMouseUp.toString())};
  1376. const maskNoteElement = getMaskElement(${JSON.stringify(NOTE_MASK_CLASS)});
  1377. const maskPageElement = getMaskElement(${JSON.stringify(PAGE_MASK_CLASS)}, ${JSON.stringify(PAGE_MASK_CONTAINER_CLASS)});
  1378. let selectedNote, highlightSelectionMode, removeHighlightMode, resizingNoteMode, movingNoteMode, collapseNoteTimeout, cuttingMode;
  1379. window.onresize = reflowNotes;
  1380. document.documentElement.onmouseup = document.documentElement.ontouchend = onMouseUp;
  1381. window.addEventListener("DOMContentLoaded", () => {
  1382. processNode(document);
  1383. reflowNotes();
  1384. document.querySelectorAll(${JSON.stringify(NOTE_TAGNAME)}).forEach(noteElement => attachNoteListeners(noteElement));
  1385. });
  1386. })()`);
  1387. }
  1388. function getStyleElement(stylesheet) {
  1389. const linkElement = document.createElement("style");
  1390. linkElement.textContent = stylesheet;
  1391. return linkElement;
  1392. }
  1393. function getAnchorElement(containerElement) {
  1394. 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 + ",") + "]");
  1395. }
  1396. function addNoteRef(anchorElement, noteId) {
  1397. const noteRefs = getNoteRefs(anchorElement);
  1398. noteRefs.push(noteId);
  1399. setNoteRefs(anchorElement, noteRefs);
  1400. }
  1401. function deleteNoteRef(containerElement, noteId) {
  1402. const anchorElement = getAnchorElement(containerElement);
  1403. const noteRefs = getNoteRefs(anchorElement).filter(noteRefs => noteRefs != noteId);
  1404. if (noteRefs.length) {
  1405. setNoteRefs(anchorElement, noteRefs);
  1406. } else {
  1407. delete anchorElement.dataset.singleFileNoteRefs;
  1408. }
  1409. }
  1410. function getNoteRefs(anchorElement) {
  1411. return JSON.parse("[" + (anchorElement.dataset.singleFileNoteRefs || "") + "]");
  1412. }
  1413. function setNoteRefs(anchorElement, noteRefs) {
  1414. anchorElement.dataset.singleFileNoteRefs = noteRefs.toString();
  1415. }
  1416. function minifyText(text) {
  1417. return text.replace(/[\n\t\s]+/g, " ");
  1418. }
  1419. })();