content.js 3.3 KB

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