1
0

options.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. var defaults = {
  2. // storage
  3. origins: {},
  4. header: false,
  5. csp: false,
  6. // static
  7. protocols: ['https', 'http', '*'],
  8. // UI
  9. protocol: 'https',
  10. origin: '',
  11. timeout: null,
  12. file: true,
  13. }
  14. var state = Object.assign({}, defaults)
  15. var events = {
  16. file: () => {
  17. chrome.tabs.create({url: 'chrome://extensions/?id=' + chrome.runtime.id})
  18. },
  19. header: (e) => {
  20. state.header = !state.header
  21. chrome.runtime.sendMessage({
  22. message: 'options.header',
  23. header: state.header,
  24. })
  25. },
  26. csp: (e) => {
  27. ;((done) => {
  28. // ff: webRequest is required permission
  29. if (/Firefox/.test(navigator.userAgent)) {
  30. done()
  31. }
  32. else {
  33. var action = state.csp ? 'remove' : 'request'
  34. chrome.permissions[action]({
  35. permissions: ['webRequest', 'webRequestBlocking']
  36. }, done)
  37. }
  38. })(() => {
  39. state.csp = !state.csp
  40. chrome.runtime.sendMessage({
  41. message: 'options.csp',
  42. csp: state.csp,
  43. })
  44. })
  45. },
  46. origin: {
  47. protocol: (e) => {
  48. state.protocol = state.protocols[e.target.selectedIndex]
  49. },
  50. name: (e) => {
  51. state.origin = e.target.value
  52. },
  53. add: () => {
  54. var domain = state.origin.replace(/.*:\/\/([^/]+).*/i, '$1')
  55. if (!domain) {
  56. return
  57. }
  58. var origin = state.protocol + '://' + domain
  59. chrome.permissions.request({origins: [origin + '/*']}, (granted) => {
  60. if (granted) {
  61. chrome.runtime.sendMessage({message: 'origin.add', origin}, init)
  62. }
  63. })
  64. },
  65. all: () => {
  66. var origin = '*://*'
  67. chrome.permissions.request({origins: [origin + '/*']}, (granted) => {
  68. if (granted) {
  69. chrome.runtime.sendMessage({message: 'origin.add', origin}, init)
  70. }
  71. })
  72. },
  73. remove: (origin) => () => {
  74. chrome.permissions.remove({origins: [origin + '/*']}, (removed) => {
  75. if (removed) {
  76. chrome.runtime.sendMessage({message: 'origin.remove', origin}, init)
  77. }
  78. })
  79. },
  80. update: (origin) => (e) => {
  81. state.origins[origin] = e.target.value
  82. clearTimeout(state.timeout)
  83. state.timeout = setTimeout(() => {
  84. chrome.runtime.sendMessage({
  85. message: 'origin.update', origin, match: e.target.value
  86. })
  87. }, 750)
  88. },
  89. refresh: (origin) => () => {
  90. chrome.permissions.request({origins: [origin + '/*']})
  91. },
  92. },
  93. }
  94. chrome.extension.isAllowedFileSchemeAccess((isAllowedAccess) => {
  95. state.file = /Firefox/.test(navigator.userAgent)
  96. ? true // ff: `Allow access to file URLs` option isn't available
  97. : isAllowedAccess
  98. m.redraw()
  99. })
  100. var init = () => {
  101. chrome.runtime.sendMessage({message: 'options'}, (res) => {
  102. state = Object.assign({}, defaults, {file: state.file}, res)
  103. m.redraw()
  104. })
  105. }
  106. init()
  107. var oncreate = {
  108. ripple: (vnode) => {
  109. mdc.ripple.MDCRipple.attachTo(vnode.dom)
  110. }
  111. }
  112. var onupdate = {
  113. header: (vnode) => {
  114. if (vnode.dom.classList.contains('is-checked') !== state.header) {
  115. vnode.dom.classList.toggle('is-checked')
  116. }
  117. },
  118. csp: (vnode) => {
  119. if (vnode.dom.classList.contains('is-checked') !== state.csp) {
  120. vnode.dom.classList.toggle('is-checked')
  121. }
  122. }
  123. }
  124. m.mount(document.querySelector('main'), {
  125. view: () =>
  126. m('#options',
  127. // file access is disabled
  128. (!state.file || null) &&
  129. m('.bs-callout m-file',
  130. m('h4.mdc-typography--headline', 'Access to file:// URLs is Disabled'),
  131. m('img.mdc-elevation--z2', {src: '/images/file-urls.png'}),
  132. m('button.mdc-button mdc-button--raised m-button', {
  133. oncreate: oncreate.ripple,
  134. onclick: events.file
  135. },
  136. 'Enable Access to file:// URLs'
  137. )
  138. ),
  139. // allowed origins
  140. m('.bs-callout m-origins',
  141. m('h4.mdc-typography--headline', 'Allowed Origins'),
  142. // add origin
  143. m('select.mdc-elevation--z2 m-select', {
  144. onchange: events.origin.protocol
  145. },
  146. state.protocols.map((protocol) =>
  147. m('option', {
  148. value: protocol,
  149. selected: protocol === state.protocol
  150. },
  151. protocol + '://'
  152. )
  153. )),
  154. m('.mdc-textfield m-textfield',
  155. m('input.mdc-textfield__input', {
  156. type: 'text',
  157. value: state.origin,
  158. onchange: events.origin.name,
  159. placeholder: 'raw.githubusercontent.com'
  160. })
  161. ),
  162. m('button.mdc-button mdc-button--raised m-button', {
  163. oncreate: oncreate.ripple,
  164. onclick: events.origin.add
  165. },
  166. 'Add'
  167. ),
  168. m('button.mdc-button mdc-button--raised m-button', {
  169. oncreate: oncreate.ripple,
  170. onclick: events.origin.all
  171. },
  172. 'Allow All'
  173. ),
  174. // header detection - ff: disabled
  175. (!/Firefox/.test(navigator.userAgent) || null) &&
  176. m('label.mdc-switch m-switch', {
  177. onupdate: onupdate.header,
  178. title: 'Toggle header detection'
  179. },
  180. m('input.mdc-switch__native-control', {
  181. type: 'checkbox',
  182. checked: state.header,
  183. onchange: events.header
  184. }),
  185. m('.mdc-switch__background', m('.mdc-switch__knob')),
  186. m('span.mdc-switch-label',
  187. 'Detect ',
  188. m('code', 'text/markdown'),
  189. ' and ',
  190. m('code', 'text/x-markdown'),
  191. ' content type'
  192. )
  193. ),
  194. // csp
  195. m('label.mdc-switch m-switch', {
  196. onupdate: onupdate.csp,
  197. title: 'Disable Content Security Policy (CSP)'
  198. },
  199. m('input.mdc-switch__native-control', {
  200. type: 'checkbox',
  201. checked: state.csp,
  202. onchange: events.csp
  203. }),
  204. m('.mdc-switch__background', m('.mdc-switch__knob')),
  205. m('span.mdc-switch-label',
  206. 'Disable ',
  207. m('code', 'Content Security Policy'),
  208. )
  209. ),
  210. m('ul.mdc-elevation--z2 m-list',
  211. Object.keys(state.origins).sort().map((origin) =>
  212. (
  213. (
  214. state.file && origin === 'file://' &&
  215. // ff: access to file:// URLs is not allowed
  216. !/Firefox/.test(navigator.userAgent)
  217. )
  218. || origin !== 'file://' || null
  219. ) &&
  220. m('li',
  221. m('span', origin.replace(/^(\*|file|http(s)?).*/, '$1')),
  222. m('span', origin.replace(/^(\*|file|http(s)?):\/\//, '')),
  223. m('.mdc-textfield m-textfield', {
  224. oncreate: oncreate.textfield
  225. },
  226. m('input.mdc-textfield__input', {
  227. type: 'text',
  228. onkeyup: events.origin.update(origin),
  229. value: state.origins[origin],
  230. })
  231. ),
  232. (origin !== 'file://' || null) &&
  233. m('span',
  234. m('button.mdc-button', {
  235. oncreate: oncreate.ripple,
  236. onclick: events.origin.refresh(origin),
  237. title: 'Refresh'
  238. },
  239. m('i.material-icons icon-refresh')
  240. ),
  241. m('button.mdc-button', {
  242. oncreate: oncreate.ripple,
  243. onclick: events.origin.remove(origin),
  244. title: 'Remove'
  245. },
  246. m('i.material-icons icon-remove')
  247. )
  248. )
  249. )
  250. )
  251. )
  252. ),
  253. )
  254. })