aboutsummaryrefslogtreecommitdiffstats
path: root/recipes/whatsapp/webview.js
blob: 89bc673477dea77e30f8608d0272e0a136873b0f (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
81
82
83
84
85
86
87
88
89
90
const _path = _interopRequireDefault(require('path'));

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

module.exports = Ferdium => {
  let dbCache

  const getMessages = () => {
    if(!dbCache) {
      const dbsPromise = indexedDB.databases()
      dbsPromise.then((databases) => {
        for(let index in databases) {
          //Wait for model-storage db to be available before calling indexedDB.open(). This is to make sure whatsapp created the model-storage DB
          if(databases[index].name === "model-storage") {
            const request = window.indexedDB.open("model-storage");
            request.onsuccess = () => {
              dbCache = request.result;
              //This will be called when db.delete is triggered, we need to close and set dbCache to null to trigger lookup again
              dbCache.onversionchange = () => {
                dbCache.close()
                dbCache = null
              };
            }
            request.addEventListener('error', () => {
              console.error("Opening model-storage database failed:", event);
            })
          }
        }
      })
    } else {
      let unreadCount = 0;
      let unreadMutedCount = 0;

      const txn = dbCache.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);
      })
    }
  }

  // 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'));
};