aboutsummaryrefslogtreecommitdiffstats
path: root/recipes/google-calendar/webview-unsafe.js
blob: 0f4833e04a15a21da38f382c82e9ac71896539c5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
let modal;
let updates = 0;

const waitFor = (condition, callback) => {
  if (condition()) {
    callback();
  } else {
    window.setTimeout(waitFor.bind(null, condition, callback), 100);
  }
};

const showModal = text => {
  modal.querySelector('p').textContent = text;
  updates += 1;
  window.ferdium.setBadge(updates);
  modal.classList.add('open');
};

const hideModal = () => {
  modal.querySelector('p').textContent = '';
  updates -= 1;
  window.ferdium.setBadge(updates);
  modal.classList.remove('open');
};

const createModal = () => {
  const modalDialog = document.createElement('div');
  modalDialog.setAttribute('id', 'franz-modal');
  modalDialog.textContent =
    '<div class="modal-content"><span class="close">&times;</span><p></p></div>';
  modalDialog.querySelector('.close').addEventListener('click', hideModal);

  return modalDialog;
};

window.alert = showModal;

modal = createModal();
waitFor(
  () => document.body,
  () => document.body.append(modal),
);
document.addEventListener(
  'keydown',
  event => event.key === 'Escape' && hideModal(),
);