origins.js 12 KB

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