storage.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // chrome.storage.sync.clear()
  2. // chrome.permissions.getAll((p) => chrome.permissions.remove({origins: p.origins}))
  3. md.storage = ({compilers}) => {
  4. var match = '\\.(?:markdown|mdown|mkdn|md|mkd|mdwn|mdtxt|mdtext|text)(?:#.*|\\?.*)?$'
  5. var defaults = {
  6. theme: 'github',
  7. compiler: 'marked',
  8. content: {
  9. emoji: false,
  10. scroll: true,
  11. toc: false,
  12. mathjax: false,
  13. },
  14. raw: false,
  15. header: true,
  16. match,
  17. origins: {
  18. 'file://': {
  19. match,
  20. csp: false,
  21. encoding: '',
  22. }
  23. },
  24. }
  25. Object.keys(compilers).forEach((compiler) => {
  26. defaults[compiler] = compilers[compiler].defaults
  27. })
  28. var state = {}
  29. function set (options) {
  30. chrome.storage.sync.set(options)
  31. Object.assign(state, options)
  32. }
  33. chrome.storage.sync.get((res) => {
  34. var options = !Object.keys(res).length ? defaults : res
  35. // v2.2 -> v2.3
  36. if (!options.match || !options.origins) {
  37. options.match = match
  38. options.origins = {
  39. 'file://': match
  40. }
  41. }
  42. // v2.3 -> v2.4
  43. else if (!options.origins['file://']) {
  44. options.origins['file://'] = match
  45. }
  46. // v2.4 -> v2.5
  47. if (!options.compiler) {
  48. options.compiler = options.options
  49. }
  50. if (!options.content) {
  51. options.content = defaults.content
  52. }
  53. // v2.7 -> v2.8
  54. if (!options.marked) {
  55. options.compiler = 'marked'
  56. options.marked = compilers.marked.defaults
  57. }
  58. // v2.8 -> v2.9
  59. if (!options.remark) {
  60. options.remark = compilers.remark.defaults
  61. }
  62. // v2.9 -> v3.0
  63. if (options.content.emoji === undefined) {
  64. options.content.emoji = false
  65. }
  66. // v3.0 -> v3.1
  67. if (options.header === undefined) {
  68. options.header = true
  69. }
  70. // v3.1 -> v3.2
  71. if (options.remark && options.remark.yaml) {
  72. delete options.remark.yaml
  73. }
  74. if (options.content.mathjax === undefined) {
  75. options.content.mathjax = false
  76. }
  77. // v3.4 -> v3.5
  78. if (typeof options.origins['file://'] === 'string') {
  79. options.origins = Object.keys(options.origins)
  80. .reduce((all, key) => (all[key] = {
  81. match: options.origins[key],
  82. csp: options.csp,
  83. encoding: '',
  84. }, all), {})
  85. }
  86. if (typeof options.csp === 'boolean') {
  87. delete options.csp
  88. }
  89. // reload extension bug
  90. chrome.permissions.getAll((permissions) => {
  91. var origins = Object.keys(res.origins || {})
  92. chrome.permissions.remove({
  93. origins: permissions.origins
  94. .filter((origin) => origins.indexOf(origin.slice(0, -2)) === -1)
  95. })
  96. })
  97. Object.keys(compilers).forEach((compiler) => {
  98. if (!options[compiler]) {
  99. options[compiler] = compilers[compiler].defaults
  100. }
  101. })
  102. chrome.storage.sync.set(options)
  103. Object.assign(state, JSON.parse(JSON.stringify(options)))
  104. })
  105. return {defaults, state, set}
  106. }