options.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. var state = {
  2. origins: []
  3. }
  4. var events = {
  5. add: () => {
  6. chrome.permissions.request({
  7. origins: [document.querySelector('input').value]
  8. }, (granted) => {
  9. if (granted) {
  10. all()
  11. document.querySelector('input').value = ''
  12. }
  13. })
  14. },
  15. remove: (origin) => () => {
  16. chrome.permissions.remove({
  17. origins: [origin]
  18. }, (removed) => {
  19. if (removed) {
  20. var index = state.origins.indexOf(origin)
  21. state.origins.splice(index, 1)
  22. m.redraw()
  23. }
  24. })
  25. }
  26. }
  27. function all () {
  28. chrome.permissions.getAll((res) => {
  29. state.origins = res.origins
  30. m.redraw()
  31. })
  32. }
  33. m.mount(document.querySelector('body'), {
  34. oninit: () => {
  35. all()
  36. },
  37. view: () =>
  38. m('div', [
  39. m('h1', 'Permissions'),
  40. m('p', 'Read and change your data on:'),
  41. m('table', state.origins.map((origin) =>
  42. m('tr', [
  43. m('td', origin),
  44. m('td', m('button', {onclick: events.remove(origin)}, 'Remove'))
  45. ])
  46. )),
  47. m('form', [
  48. m('label', 'Origin'),
  49. m('input[placeholder="https://raw.githubusercontent.com"]'),
  50. m('button[type=button]', {onclick: events.add}, 'Add')
  51. ])
  52. ])
  53. })