content.js 3.2 KB

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