content.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. var $ = document.querySelector.bind(document)
  2. var state = {
  3. theme: '',
  4. html: '',
  5. markdown: '',
  6. raw: false,
  7. getURL: () => chrome.extension.getURL('/themes/' + state.theme + '.css')
  8. }
  9. chrome.extension.sendMessage({message: 'settings'}, (data) => {
  10. state.theme = data.theme
  11. state.raw = data.raw
  12. m.redraw()
  13. })
  14. chrome.extension.onMessage.addListener((req, sender, sendResponse) => {
  15. if (req.message === 'reload') {
  16. window.location.reload(true)
  17. }
  18. else if (req.message === 'theme') {
  19. state.theme = req.theme
  20. m.redraw()
  21. }
  22. else if (req.message === 'raw') {
  23. state.raw = req.raw
  24. m.redraw()
  25. }
  26. })
  27. function mount () {
  28. $('pre').style.display = 'none'
  29. m.mount($('body'), {
  30. controller: function () {
  31. ;((done) => {
  32. if (document.charset === 'UTF-8') {
  33. done()
  34. return
  35. }
  36. m.request({url: window.location.href,
  37. deserialize: (body) => {
  38. done(body)
  39. return body
  40. }
  41. })
  42. })((data) => {
  43. state.markdown = data || $('pre').innerText
  44. chrome.extension.sendMessage({
  45. message: 'markdown',
  46. markdown: state.markdown
  47. }, (res) => {
  48. state.html = res.marked
  49. m.redraw()
  50. })
  51. })
  52. return {
  53. markdown: (element, initialized, context) => {
  54. if (!initialized) {
  55. document.body.scrollTop = parseInt(localStorage.getItem('scrolltop'))
  56. }
  57. },
  58. html: (element, initialized, context) => {
  59. if (!initialized) {
  60. document.body.scrollTop = parseInt(localStorage.getItem('scrolltop'))
  61. setTimeout(() => Prism.highlightAll(), 20)
  62. }
  63. }
  64. }
  65. },
  66. view: (ctrl) => {
  67. var dom = []
  68. if (state.raw) {
  69. updateStyles()
  70. dom.push(m('pre#markdown', {config: ctrl.markdown}, state.markdown))
  71. }
  72. if (state.theme && !state.raw) {
  73. updateStyles()
  74. dom.push(m('link#theme [rel="stylesheet"] [type="text/css"]',
  75. {href: state.getURL()}))
  76. }
  77. if (state.html && !state.raw) {
  78. dom.push(m('#html', {config: ctrl.html}, m.trust(state.html)))
  79. }
  80. return (dom.length ? dom : m('div'))
  81. }
  82. })
  83. }
  84. function scroll () {
  85. setTimeout(() => {
  86. var timeout = null
  87. window.addEventListener('scroll', () => {
  88. clearTimeout(timeout)
  89. timeout = setTimeout(() => {
  90. localStorage.setItem('scrolltop', document.body.scrollTop)
  91. }, 100)
  92. })
  93. document.body.scrollTop = parseInt(localStorage.getItem('scrolltop'))
  94. }, 100)
  95. }
  96. if (document.readyState === 'complete') {
  97. mount()
  98. scroll()
  99. }
  100. else {
  101. window.addEventListener('DOMContentLoaded', mount)
  102. window.addEventListener('load', scroll)
  103. }
  104. function updateStyles () {
  105. if (state.raw) {
  106. $('html').classList.remove('markdown-theme-html')
  107. $('body').classList.remove('markdown-theme')
  108. $('html').classList.remove('markdown-body-html')
  109. $('body').classList.remove('markdown-body')
  110. }
  111. else if (/github(-dark)?/.test(state.theme)) {
  112. $('html').classList.remove('markdown-theme-html')
  113. $('body').classList.remove('markdown-theme')
  114. $('html').classList.add('markdown-body-html')
  115. $('body').classList.add('markdown-body')
  116. }
  117. else {
  118. $('html').classList.remove('markdown-body-html')
  119. $('body').classList.remove('markdown-body')
  120. $('html').classList.add('markdown-theme-html')
  121. $('body').classList.add('markdown-theme')
  122. }
  123. }