<commit_before>
window.addEventListener('DOMContentLoaded', () => {
  var $ = document.querySelector.bind(document)
  $('pre').style.display = 'none'

  m.mount($('body'), {
    controller: function () {
      var state = {
        theme: '',
        html: '',
        markdown: '',
        raw: false,
        getURL: () => chrome.extension.getURL('/themes/' + state.theme + '.css')
      }

      ;((done) => {
        if (document.charset === 'UTF-8') {
          done()
        }
        m.request({url: window.location.href,
          deserialize: (body) => {
            done(body)
            return body
          }
        })
      })((data) => {
        state.markdown = data || $('pre').innerText

        setTimeout(() => {
          chrome.extension.sendMessage({message: 'settings'}, (data) => {
            state.theme = data.theme
            state.raw = data.raw
            m.redraw()

            setTimeout(() => {
              chrome.extension.sendMessage({
                message: 'markdown',
                markdown: state.markdown
              }, (res) => {
                state.html = res.marked
                m.redraw()
              })
            }, 0)
          })
        }, 0)
      })

      chrome.extension.onMessage.addListener((req, sender, sendResponse) => {
        if (req.message === 'reload') {
          window.location.reload(true)
        }
        else if (req.message === 'theme') {
          state.theme = req.theme
          m.redraw()
        }
        else if (req.message === 'raw') {
          state.raw = req.raw
          m.redraw()
        }
      })

      return {
        state: state,
        config: (element, initialized, context) => {
          if (!initialized) {
            Prism.highlightAll()
          }
        },
        updateStyles: (theme) => {
          if (state.raw) {
            $('html').classList.remove('markdown-theme-html')
            $('body').classList.remove('markdown-theme')
            $('html').classList.remove('markdown-body-html')
            $('body').classList.remove('markdown-body')
          }
          else if (/github(-dark)?/.test(state.theme)) {
            $('html').classList.remove('markdown-theme-html')
            $('body').classList.remove('markdown-theme')
            $('html').classList.add('markdown-body-html')
            $('body').classList.add('markdown-body')
          }
          else {
            $('html').classList.remove('markdown-body-html')
            $('body').classList.remove('markdown-body')
            $('html').classList.add('markdown-theme-html')
            $('body').classList.add('markdown-theme')
          }
        }
      }
    },
    view: (ctrl) => {
      var state = ctrl.state

      var dom = []

      if (state.raw) {
        ctrl.updateStyles()
        dom.push(m('pre#markdown', state.markdown))
      }
      if (state.theme && !state.raw) {
        ctrl.updateStyles()
        dom.push(m('link#theme [rel="stylesheet"] [type="text/css"]',
          {href: state.getURL()}))
      }
      if (state.html && !state.raw) {
        dom.push(m('#html', {config: ctrl.config}, m.trust(state.html)))
      }

      return (dom.length ? dom : m('div'))
    }
  })
})

window.addEventListener('load', () => setTimeout(() => {
  var timeout = null
  window.addEventListener('scroll', () => {
    clearTimeout(timeout)
    timeout = setTimeout(() => {
      localStorage.setItem('scrolltop', document.body.scrollTop)
    }, 100)
  })
  document.body.scrollTop = parseInt(localStorage.getItem('scrolltop'))
}, 100))<commit_msg>Improve rendering time + refactoring<commit_after>
var $ = document.querySelector.bind(document)

var state = {
  theme: '',
  html: '',
  markdown: '',
  raw: false,
  getURL: () => chrome.extension.getURL('/themes/' + state.theme + '.css')
}

chrome.extension.sendMessage({message: 'settings'}, (data) => {
  state.theme = data.theme
  state.raw = data.raw
})

chrome.extension.onMessage.addListener((req, sender, sendResponse) => {
  if (req.message === 'reload') {
    window.location.reload(true)
  }
  else if (req.message === 'theme') {
    state.theme = req.theme
    m.redraw()
  }
  else if (req.message === 'raw') {
    state.raw = req.raw
    m.redraw()
  }
})

window.addEventListener('DOMContentLoaded', () => {
  $('pre').style.display = 'none'

  m.mount($('body'), {
    controller: function () {
      ;((done) => {
        if (document.charset === 'UTF-8') {
          done()
        }
        m.request({url: window.location.href,
          deserialize: (body) => {
            done(body)
            return body
          }
        })
      })((data) => {
        state.markdown = data || $('pre').innerText

        chrome.extension.sendMessage({
          message: 'markdown',
          markdown: state.markdown
        }, (res) => {
          state.html = res.marked
          m.redraw()
        })
      })

      return {
        markdown: (element, initialized, context) => {
          if (!initialized) {
            document.body.scrollTop = parseInt(localStorage.getItem('scrolltop'))
          }
        },
        html: (element, initialized, context) => {
          if (!initialized) {
            document.body.scrollTop = parseInt(localStorage.getItem('scrolltop'))
            Prism.highlightAll()
          }
        }
      }
    },
    view: (ctrl) => {
      var dom = []

      if (state.raw) {
        updateStyles()
        dom.push(m('pre#markdown', {config: ctrl.markdown}, state.markdown))
      }
      if (state.theme && !state.raw) {
        updateStyles()
        dom.push(m('link#theme [rel="stylesheet"] [type="text/css"]',
          {href: state.getURL()}))
      }
      if (state.html && !state.raw) {
        dom.push(m('#html', {config: ctrl.html}, m.trust(state.html)))
      }

      return (dom.length ? dom : m('div'))
    }
  })
})

window.addEventListener('load', () => setTimeout(() => {
  var timeout = null
  window.addEventListener('scroll', () => {
    clearTimeout(timeout)
    timeout = setTimeout(() => {
      localStorage.setItem('scrolltop', document.body.scrollTop)
    }, 100)
  })
  document.body.scrollTop = parseInt(localStorage.getItem('scrolltop'))
}, 100))

function updateStyles () {
  if (state.raw) {
    $('html').classList.remove('markdown-theme-html')
    $('body').classList.remove('markdown-theme')
    $('html').classList.remove('markdown-body-html')
    $('body').classList.remove('markdown-body')
  }
  else if (/github(-dark)?/.test(state.theme)) {
    $('html').classList.remove('markdown-theme-html')
    $('body').classList.remove('markdown-theme')
    $('html').classList.add('markdown-body-html')
    $('body').classList.add('markdown-body')
  }
  else {
    $('html').classList.remove('markdown-body-html')
    $('body').classList.remove('markdown-body')
    $('html').classList.add('markdown-theme-html')
    $('body').classList.add('markdown-theme')
  }
}
{'fraction_non_alphanumeric': 0.1845444059976932, 'fraction_numerical': 0.002306805074971165, 'mean_word_length': 2.0519137703475585, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2084046', 'n_tokens_mistral': 2118, 'n_tokens_neox': 2073, 'n_words': 314}