aboutsummaryrefslogtreecommitdiffstats
path: root/recipes/whatsapp/webview.js
blob: 8a5f83057f621fd96d6649690a5c969158fc0f9d (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const _path = _interopRequireDefault(require('path'));

function _interopRequireDefault(obj) {
  return obj && obj.__esModule ? obj : {default: obj};
}

let getMessages = () => {
  /* stub until db is connected*/
}

module.exports = Ferdium => {
  const request = window.indexedDB.open("model-storage");
  request.onsuccess = () => {
    const db = request.result;

    getMessages = () => {
      let unreadCount = 0;
      let unreadMutedCount = 0;

      const txn = db.transaction('chat', 'readonly');
      const store = txn.objectStore('chat');

      const query = store.getAll();

      query.onsuccess = (event) => {
        for (const chat of event.target.result) {
          if (chat.unreadCount > 0) {
            if (chat.muteExpiration === 0) {
              unreadCount += chat.unreadCount;
            } else {
              unreadMutedCount += chat.unreadCount;
            }
          }
        }

        Ferdium.setBadge(unreadCount, unreadMutedCount);
      };

      query.addEventListener('error', (event) => {
        console.error("Loading data from database failed:", event);
      })
    }
  };

  request.addEventListener('error', (event) => {
    console.error("Opening model-storage database failed:", event);
  })

  // inject webview hacking script
  Ferdium.injectJSUnsafe(_path.default.join(__dirname, 'webview-unsafe.js'));

  const getActiveDialogTitle = () => {
    const element = document.querySelector('header .emoji-texttt');

    Ferdium.setDialogTitle(element ? element.textContent : '');
  };

  const loopFunc = () => {
    getMessages();
    getActiveDialogTitle();
  };

  window.addEventListener('beforeunload', async () => {
    Ferdium.releaseServiceWorkers();
  });

  Ferdium.handleDarkMode((isEnabled) => {

    if (isEnabled) {
      document.body.classList.add('dark');
    } else {
      document.body.classList.remove('dark');
    }

  });

  Ferdium.loop(loopFunc);

  Ferdium.injectCSS(_path.default.join(__dirname, 'service.css'));
};