diff options
Diffstat (limited to 'recipes/whatsapp/webview.js')
-rw-r--r-- | recipes/whatsapp/webview.js | 66 |
1 files changed, 32 insertions, 34 deletions
diff --git a/recipes/whatsapp/webview.js b/recipes/whatsapp/webview.js index e2496be..26f2a3a 100644 --- a/recipes/whatsapp/webview.js +++ b/recipes/whatsapp/webview.js | |||
@@ -1,42 +1,21 @@ | |||
1 | const _path = _interopRequireDefault(require('path')); | ||
2 | |||
3 | function _interopRequireDefault(obj) { | 1 | function _interopRequireDefault(obj) { |
4 | return obj && obj.__esModule ? obj : {default: obj}; | 2 | return obj && obj.__esModule ? obj : { default: obj }; |
5 | } | 3 | } |
6 | 4 | ||
5 | const _path = _interopRequireDefault(require('path')); | ||
6 | |||
7 | module.exports = Ferdium => { | 7 | module.exports = Ferdium => { |
8 | let dbCache | 8 | let dbCache; |
9 | 9 | ||
10 | const getMessages = () => { | 10 | const getMessages = () => { |
11 | if(!dbCache) { | 11 | if (dbCache) { |
12 | const dbsPromise = indexedDB.databases() | ||
13 | dbsPromise.then((databases) => { | ||
14 | for(let index in databases) { | ||
15 | //Wait for model-storage db to be available before calling indexedDB.open(). This is to make sure whatsapp created the model-storage DB | ||
16 | if(databases[index].name === "model-storage") { | ||
17 | const request = window.indexedDB.open("model-storage"); | ||
18 | request.onsuccess = () => { | ||
19 | dbCache = request.result; | ||
20 | //This will be called when db.delete is triggered, we need to close and set dbCache to null to trigger lookup again | ||
21 | dbCache.onversionchange = () => { | ||
22 | dbCache.close() | ||
23 | dbCache = null | ||
24 | }; | ||
25 | } | ||
26 | request.addEventListener('error', () => { | ||
27 | console.error("Opening model-storage database failed:", event); | ||
28 | }) | ||
29 | } | ||
30 | } | ||
31 | }) | ||
32 | } else { | ||
33 | let unreadCount = 0; | 12 | let unreadCount = 0; |
34 | let unreadMutedCount = 0; | 13 | let unreadMutedCount = 0; |
35 | 14 | ||
36 | const txn = dbCache.transaction('chat', 'readonly'); | 15 | const txn = dbCache.transaction('chat', 'readonly'); |
37 | const store = txn.objectStore('chat'); | 16 | const store = txn.objectStore('chat'); |
38 | const query = store.getAll(); | 17 | const query = store.getAll(); |
39 | query.onsuccess = (event) => { | 18 | query.onsuccess = event => { |
40 | for (const chat of event.target.result) { | 19 | for (const chat of event.target.result) { |
41 | if (chat.unreadCount > 0) { | 20 | if (chat.unreadCount > 0) { |
42 | if (chat.muteExpiration > 0 || chat.isAutoMuted) { | 21 | if (chat.muteExpiration > 0 || chat.isAutoMuted) { |
@@ -50,11 +29,32 @@ module.exports = Ferdium => { | |||
50 | Ferdium.setBadge(unreadCount, unreadMutedCount); | 29 | Ferdium.setBadge(unreadCount, unreadMutedCount); |
51 | }; | 30 | }; |
52 | 31 | ||
53 | query.addEventListener('error', (event) => { | 32 | query.addEventListener('error', event => { |
54 | console.error("Loading data from database failed:", event); | 33 | console.error('Loading data from database failed:', event); |
55 | }) | 34 | }); |
35 | } else { | ||
36 | const dbsPromise = indexedDB.databases(); | ||
37 | dbsPromise.then(databases => { | ||
38 | for (let index in databases) { | ||
39 | //Wait for model-storage db to be available before calling indexedDB.open(). This is to make sure whatsapp created the model-storage DB | ||
40 | if (databases[index].name === 'model-storage') { | ||
41 | const request = window.indexedDB.open('model-storage'); | ||
42 | request.onsuccess = () => { | ||
43 | dbCache = request.result; | ||
44 | // This will be called when db.delete is triggered, we need to close and set dbCache to null to trigger lookup again | ||
45 | dbCache.onversionchange = () => { | ||
46 | dbCache.close(); | ||
47 | dbCache = null; | ||
48 | }; | ||
49 | }; | ||
50 | request.addEventListener('error', () => { | ||
51 | console.error('Opening model-storage database failed:', event); | ||
52 | }); | ||
53 | } | ||
54 | } | ||
55 | }); | ||
56 | } | 56 | } |
57 | } | 57 | }; |
58 | 58 | ||
59 | // inject webview hacking script | 59 | // inject webview hacking script |
60 | Ferdium.injectJSUnsafe(_path.default.join(__dirname, 'webview-unsafe.js')); | 60 | Ferdium.injectJSUnsafe(_path.default.join(__dirname, 'webview-unsafe.js')); |
@@ -74,14 +74,12 @@ module.exports = Ferdium => { | |||
74 | Ferdium.releaseServiceWorkers(); | 74 | Ferdium.releaseServiceWorkers(); |
75 | }); | 75 | }); |
76 | 76 | ||
77 | Ferdium.handleDarkMode((isEnabled) => { | 77 | Ferdium.handleDarkMode(isEnabled => { |
78 | |||
79 | if (isEnabled) { | 78 | if (isEnabled) { |
80 | document.body.classList.add('dark'); | 79 | document.body.classList.add('dark'); |
81 | } else { | 80 | } else { |
82 | document.body.classList.remove('dark'); | 81 | document.body.classList.remove('dark'); |
83 | } | 82 | } |
84 | |||
85 | }); | 83 | }); |
86 | 84 | ||
87 | Ferdium.loop(loopFunc); | 85 | Ferdium.loop(loopFunc); |