content.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. function injectCSS () {
  2. var link = document.createElement('link')
  3. link.rel = 'stylesheet'
  4. link.type = 'text/css'
  5. link.href = '#'
  6. link.id = 'theme'
  7. document.head.appendChild(link)
  8. }
  9. $(function () {
  10. injectCSS()
  11. $('body').addClass('markdown-body')//github
  12. $('pre').attr('id', 'markdown').hide()
  13. chrome.extension.sendMessage({
  14. message: 'markdown',
  15. markdown: $('#markdown').text()
  16. }, function (res) {
  17. $('body').append('<div id="html">').find('#html').append(res.marked)
  18. Prism.highlightAll()
  19. })
  20. chrome.extension.sendMessage({
  21. message: 'settings',
  22. }, function (data) {
  23. $('#theme').attr('href', chrome.extension.getURL('/themes/'+data.theme+'.css'))
  24. $('#theme').attr('disabled', data.raw)
  25. $('#markdown')[data.raw?'show':'hide']()
  26. $('#html')[data.raw?'hide':'show']()
  27. })
  28. $(window).on('load', function (e) {
  29. setTimeout(function () {
  30. var timeout = null
  31. $(window).on('scroll', function (e) {
  32. clearTimeout(timeout)
  33. timeout = setTimeout(function () {
  34. localStorage.setItem('scrolltop', $(window).scrollTop())
  35. }, 100)
  36. })
  37. $(window).scrollTop(localStorage.getItem('scrolltop'))
  38. }, 100)
  39. })
  40. })
  41. chrome.extension.onMessage.addListener(function (req, sender, sendResponse) {
  42. switch (req.message) {
  43. case 'reload':
  44. window.location.reload(true)
  45. break
  46. case 'theme':
  47. var raw = $('#theme').attr('disabled') == 'disabled'
  48. $('#theme').remove()
  49. injectCSS()
  50. $('#theme').attr('href', chrome.extension.getURL('/themes/'+req.theme+'.css'))
  51. $('#theme').attr('disabled', raw)
  52. break
  53. case 'raw':
  54. $('#theme').attr('disabled', !($('#theme').attr('disabled') == 'disabled'))
  55. $('#markdown, #html').toggle()
  56. break
  57. }
  58. })