options.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. var defaults = {
  2. // storage
  3. origins: {},
  4. header: false,
  5. match: '',
  6. // UI
  7. scheme: 'https',
  8. host: '',
  9. timeout: null,
  10. file: true,
  11. // static
  12. schemes: ['https', 'http', '*'],
  13. encodings: {
  14. 'Unicode': ['UTF-8', 'UTF-16LE'],
  15. 'Arabic': ['ISO-8859-6', 'Windows-1256'],
  16. 'Baltic': ['ISO-8859-4', 'ISO-8859-13', 'Windows-1257'],
  17. 'Celtic': ['ISO-8859-14'],
  18. 'Central European': ['ISO-8859-2', 'Windows-1250'],
  19. 'Chinese Simplified': ['GB18030', 'GBK'],
  20. 'Chinese Traditional': ['BIG5'],
  21. 'Cyrillic': ['ISO-8859-5', 'IBM866', 'KOI8-R', 'KOI8-U', 'Windows-1251'],
  22. 'Greek': ['ISO-8859-7', 'Windows-1253'],
  23. 'Hebrew': ['Windows-1255', 'ISO-8859-8', 'ISO-8859-8-I'],
  24. 'Japanese': ['EUC-JP', 'ISO-2022-JP', 'Shift_JIS'],
  25. 'Korean': ['EUC-KR'],
  26. 'Nordic': ['ISO-8859-10'],
  27. 'Romanian': ['ISO-8859-16'],
  28. 'South European': ['ISO-8859-3'],
  29. 'Thai': ['Windows-874'],
  30. 'Turkish': ['Windows-1254'],
  31. 'Vietnamese': ['Windows-1258'],
  32. 'Western': ['ISO-8859-15', 'Windows-1252', 'Macintosh'],
  33. },
  34. // chrome
  35. permissions: {},
  36. }
  37. var state = Object.assign({}, defaults)
  38. chrome.extension.isAllowedFileSchemeAccess((isAllowedAccess) => {
  39. state.file = /Firefox/.test(navigator.userAgent)
  40. ? true // ff: `Allow access to file URLs` option isn't available
  41. : isAllowedAccess
  42. m.redraw()
  43. })
  44. chrome.runtime.sendMessage({message: 'options'}, (res) => {
  45. state = Object.assign({}, defaults, {file: state.file}, res)
  46. chrome.permissions.getAll(({origins}) => {
  47. state.permissions = origins.reduce((all, origin) =>
  48. (all[origin.replace(/(.*)\/\*$/, '$1')] = true, all), {})
  49. m.redraw()
  50. })
  51. })
  52. var events = {
  53. file: () => {
  54. chrome.tabs.create({url: `chrome://extensions/?id=${chrome.runtime.id}`})
  55. },
  56. header: (e) => {
  57. state.header = !state.header
  58. chrome.runtime.sendMessage({
  59. message: 'options.header',
  60. header: state.header,
  61. })
  62. },
  63. origin: {
  64. scheme: (e) => {
  65. state.scheme = state.schemes[e.target.selectedIndex]
  66. },
  67. host: (e) => {
  68. state.host = e.target.value.replace(/.*:\/\/([^/]+).*/i, '$1')
  69. },
  70. add: (all) => () => {
  71. if (!all && !state.host) {
  72. return
  73. }
  74. var origin = all ? '*://*' : `${state.scheme}://${state.host}`
  75. chrome.permissions.request({origins: [`${origin}/*`]}, (granted) => {
  76. if (granted) {
  77. chrome.runtime.sendMessage({message: 'origin.add', origin})
  78. state.origins[origin] = {
  79. match: state.match,
  80. csp: false,
  81. encoding: '',
  82. }
  83. state.host = ''
  84. state.permissions[origin] = true
  85. m.redraw()
  86. }
  87. })
  88. },
  89. remove: (origin) => () => {
  90. chrome.permissions.remove({origins: [`${origin}/*`]}, (removed) => {
  91. if (removed) {
  92. chrome.runtime.sendMessage({message: 'origin.remove', origin})
  93. delete state.origins[origin]
  94. delete state.permissions[origin]
  95. m.redraw()
  96. }
  97. })
  98. },
  99. refresh: (origin) => () => {
  100. chrome.permissions.request({origins: [`${origin}/*`]}, (granted) => {
  101. if (granted) {
  102. state.permissions[origin] = true
  103. m.redraw()
  104. }
  105. })
  106. },
  107. match: (origin) => (e) => {
  108. state.origins[origin].match = e.target.value
  109. clearTimeout(state.timeout)
  110. state.timeout = setTimeout(() => {
  111. var {match, csp, encoding} = state.origins[origin]
  112. chrome.runtime.sendMessage({
  113. message: 'origin.update',
  114. origin,
  115. options: {match, csp, encoding},
  116. })
  117. }, 750)
  118. },
  119. csp: (origin) => () => {
  120. state.origins[origin].csp = !state.origins[origin].csp
  121. var {match, csp, encoding} = state.origins[origin]
  122. chrome.runtime.sendMessage({
  123. message: 'origin.update',
  124. origin,
  125. options: {match, csp, encoding},
  126. })
  127. },
  128. encoding: (origin) => (e) => {
  129. state.origins[origin].encoding = e.target.value
  130. var {match, csp, encoding} = state.origins[origin]
  131. chrome.runtime.sendMessage({
  132. message: 'origin.update',
  133. origin,
  134. options: {match, csp, encoding},
  135. })
  136. },
  137. },
  138. }
  139. var oncreate = {
  140. ripple: (vnode) => {
  141. mdc.ripple.MDCRipple.attachTo(vnode.dom)
  142. },
  143. textfield: (vnode) => {
  144. mdc.textfield.MDCTextField.attachTo(vnode.dom)
  145. }
  146. }
  147. var onupdate = {
  148. header: (vnode) => {
  149. if (vnode.dom.classList.contains('is-checked') !== state.header) {
  150. vnode.dom.classList.toggle('is-checked')
  151. }
  152. },
  153. csp: (origin) => (vnode) => {
  154. if (vnode.dom.classList.contains('is-checked') !== state.origins[origin].csp) {
  155. vnode.dom.classList.toggle('is-checked')
  156. }
  157. }
  158. }
  159. m.mount(document.querySelector('main'), {
  160. view: () =>
  161. m('#options',
  162. // allowed origins
  163. m('.bs-callout m-origins',
  164. // add origin
  165. m('.m-add-origin',
  166. m('h4.mdc-typography--headline5', 'Allowed Origins'),
  167. m('select.mdc-elevation--z2 m-select', {
  168. onchange: events.origin.scheme
  169. },
  170. state.schemes.map((scheme) =>
  171. m('option', {
  172. value: scheme,
  173. selected: scheme === state.scheme
  174. },
  175. scheme + '://'
  176. )
  177. )),
  178. m('.mdc-text-field m-textfield', {
  179. oncreate: oncreate.textfield,
  180. },
  181. m('input.mdc-text-field__input', {
  182. type: 'text',
  183. value: state.host,
  184. onchange: events.origin.host,
  185. placeholder: 'raw.githubusercontent.com'
  186. }),
  187. m('.mdc-line-ripple')
  188. ),
  189. m('button.mdc-button mdc-button--raised m-button', {
  190. oncreate: oncreate.ripple,
  191. onclick: events.origin.add()
  192. },
  193. 'Add'
  194. ),
  195. m('button.mdc-button mdc-button--raised m-button', {
  196. oncreate: oncreate.ripple,
  197. onclick: events.origin.add(true)
  198. },
  199. 'Allow All'
  200. )
  201. ),
  202. // global options
  203. m('.m-global',
  204. (
  205. (
  206. // header detection - ff: disabled
  207. !/Firefox/.test(navigator.userAgent) &&
  208. Object.keys(state.origins).length > 1
  209. )
  210. || null
  211. ) &&
  212. m('label.mdc-switch m-switch', {
  213. onupdate: onupdate.header,
  214. title: 'Toggle header detection'
  215. },
  216. m('input.mdc-switch__native-control', {
  217. type: 'checkbox',
  218. checked: state.header,
  219. onchange: events.header
  220. }),
  221. m('.mdc-switch__background', m('.mdc-switch__knob')),
  222. m('span.mdc-switch-label',
  223. 'Detect ',
  224. m('code', 'text/markdown'),
  225. ' and ',
  226. m('code', 'text/x-markdown'),
  227. ' content type'
  228. )
  229. ),
  230. // file access is disabled
  231. (!state.file || null) &&
  232. m('button.mdc-button mdc-button--raised m-button', {
  233. oncreate: oncreate.ripple,
  234. onclick: events.file
  235. },
  236. 'Allow Access to file:// URLs'
  237. )
  238. ),
  239. // allowed origins
  240. (state.file || Object.keys(state.origins).length > 1 || null) &&
  241. m('ul.m-list',
  242. Object.keys(state.origins).sort().map((origin) =>
  243. (
  244. (
  245. state.file && origin === 'file://' &&
  246. // ff: access to file:// URLs is not allowed
  247. !/Firefox/.test(navigator.userAgent)
  248. )
  249. || origin !== 'file://' || null
  250. ) &&
  251. m('li.mdc-elevation--z2', {
  252. class: state.origins[origin].expanded ? 'm-expanded' : null,
  253. },
  254. m('.m-summary', {
  255. onclick: (e) => state.origins[origin].expanded = !state.origins[origin].expanded
  256. },
  257. m('.m-origin', origin),
  258. m('.m-options',
  259. !state.permissions[origin] ? m('span', m('strong', 'refresh')) : null,
  260. state.origins[origin].match !== state.match ? m('span', 'match') : null,
  261. state.origins[origin].csp ? m('span', 'csp') : null,
  262. state.origins[origin].encoding ? m('span', 'encoding') : null,
  263. ),
  264. m('i.material-icons', {
  265. class: state.origins[origin].expanded ? 'icon-arrow-up' : 'icon-arrow-down'
  266. })
  267. ),
  268. m('.m-content',
  269. // match
  270. m('.m-option m-match',
  271. m('.m-name', m('span', 'match')),
  272. m('.m-control',
  273. m('.mdc-text-field m-textfield', {
  274. oncreate: oncreate.textfield
  275. },
  276. m('input.mdc-text-field__input', {
  277. type: 'text',
  278. onkeyup: events.origin.match(origin),
  279. value: state.origins[origin].match,
  280. }),
  281. m('.mdc-line-ripple')
  282. )
  283. )
  284. ),
  285. // csp
  286. (origin !== 'file://' || null) &&
  287. m('.m-option m-csp',
  288. m('.m-name', m('span', 'csp')),
  289. m('.m-control',
  290. m('label.mdc-switch m-switch', {
  291. onupdate: onupdate.csp(origin),
  292. },
  293. m('input.mdc-switch__native-control', {
  294. type: 'checkbox',
  295. checked: state.origins[origin].csp,
  296. onchange: events.origin.csp(origin)
  297. }),
  298. m('.mdc-switch__background', m('.mdc-switch__knob')),
  299. m('span.mdc-switch-label',
  300. 'Disable ',
  301. m('code', 'Content Security Policy'),
  302. )
  303. )
  304. )
  305. ),
  306. // encoding
  307. (origin !== 'file://' || null) &&
  308. m('.m-option m-encoding',
  309. m('.m-name', m('span', 'encoding')),
  310. m('.m-control',
  311. m('select.mdc-elevation--z2 m-select', {
  312. onchange: events.origin.encoding(origin),
  313. },
  314. m('option', {
  315. value: '',
  316. selected: state.origins[origin].encoding === ''
  317. },
  318. 'auto'
  319. ),
  320. Object.keys(state.encodings).map((label) =>
  321. m('optgroup', {label}, state.encodings[label].map((encoding) =>
  322. m('option', {
  323. value: encoding,
  324. selected: state.origins[origin].encoding === encoding
  325. },
  326. encoding
  327. )
  328. ))
  329. )
  330. )
  331. )
  332. ),
  333. // refresh/remove
  334. (origin !== 'file://' || null) &&
  335. m('.m-footer',
  336. m('span',
  337. (!state.permissions[origin] || null) &&
  338. m('button.mdc-button mdc-button--raised m-button', {
  339. oncreate: oncreate.ripple,
  340. onclick: events.origin.refresh(origin)
  341. },
  342. 'Refresh'
  343. ),
  344. m('button.mdc-button mdc-button--raised m-button', {
  345. oncreate: oncreate.ripple,
  346. onclick: events.origin.remove(origin)
  347. },
  348. 'Remove'
  349. )
  350. )
  351. )
  352. )
  353. )
  354. )
  355. )
  356. ),
  357. )
  358. })
  359. // ff: set appropriate footer icon
  360. document.querySelector(
  361. '.icon-' + (/Firefox/.test(navigator.userAgent) ? 'firefox' : 'chrome')
  362. ).classList.remove('icon-hidden')