custom.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. var Custom = () => {
  2. var defaults = {
  3. theme: '',
  4. color: 'auto',
  5. _colors: ['auto', 'light', 'dark'],
  6. error: '',
  7. }
  8. var state = Object.assign({}, defaults)
  9. chrome.runtime.sendMessage({message: 'custom.get'}, (res) => {
  10. Object.assign(state, res)
  11. m.redraw()
  12. })
  13. var events = {
  14. file: (e) => {
  15. document.querySelector('input[type=file]').click()
  16. },
  17. theme: (e) => {
  18. var file = e.target.files[0]
  19. if (file) {
  20. var minified
  21. var reader = new FileReader()
  22. reader.readAsText(file, 'UTF-8')
  23. reader.onload = (e) => {
  24. minified = csso.minify(e.target.result).css
  25. chrome.runtime.sendMessage({
  26. message: 'custom.set',
  27. custom: {
  28. theme: minified,
  29. color: state.color,
  30. },
  31. }, (res) => {
  32. if (res?.error) {
  33. state.error = res.error
  34. }
  35. else {
  36. state.theme = minified
  37. state.error = ''
  38. }
  39. m.redraw()
  40. })
  41. }
  42. }
  43. },
  44. remove: (e) => {
  45. state.theme = ''
  46. state.error = ''
  47. chrome.runtime.sendMessage({
  48. message: 'custom.set',
  49. custom: {
  50. theme: state.theme,
  51. color: state.color,
  52. },
  53. })
  54. m.redraw()
  55. },
  56. color: (e) => {
  57. state.color = state._colors[e.target.selectedIndex]
  58. chrome.runtime.sendMessage({
  59. message: 'custom.set',
  60. custom: {
  61. theme: state.theme,
  62. color: state.color,
  63. },
  64. })
  65. }
  66. }
  67. var oncreate = {
  68. ripple: (vnode) => {
  69. mdc.ripple.MDCRipple.attachTo(vnode.dom)
  70. }
  71. }
  72. var onupdate = {}
  73. var render = () => [
  74. m('.bs-callout m-custom',
  75. state.error &&
  76. m('.row',
  77. m('.col-12',
  78. m('span.m-label.m-error', state.error)
  79. )
  80. ),
  81. m('.row',
  82. m('.col-xxl-6.col-xl-6.col-lg-6.col-md-6.col-sm-12',
  83. m('span.m-label',
  84. 'Custom Theme'
  85. )
  86. ),
  87. m('.col-xxl-6.col-xl-6.col-lg-6.col-md-6.col-sm-12',
  88. m('input', {
  89. type: 'file',
  90. accept: '.css',
  91. onchange: events.theme,
  92. oncancel: events.theme,
  93. }),
  94. m('button.mdc-button mdc-button--raised m-button', {
  95. oncreate: oncreate.ripple,
  96. onclick: events.file
  97. },
  98. !state.theme ? 'Add' : 'Update'
  99. ),
  100. state.theme &&
  101. m('button.mdc-button mdc-button--raised m-button', {
  102. oncreate: oncreate.ripple,
  103. onclick: events.remove
  104. },
  105. 'Remove'
  106. ),
  107. ),
  108. ),
  109. state.theme &&
  110. m('.row',
  111. m('.col-xxl-6.col-xl-6.col-lg-6.col-md-6.col-sm-12',
  112. m('span.m-label',
  113. 'Color Scheme'
  114. )
  115. ),
  116. m('.col-xxl-6.col-xl-6.col-lg-6.col-md-6.col-sm-12',
  117. m('select.mdc-elevation--z2 m-select', {
  118. onchange: events.color
  119. },
  120. state._colors.map((color) =>
  121. m('option', {
  122. selected: state.color === color,
  123. }, color)
  124. )
  125. )
  126. ),
  127. ),
  128. )
  129. ]
  130. return {state, render}
  131. }