|
1 | | -import { ipcRenderer } from "electron"; |
2 | | -import path from "path"; |
| 1 | +import { contextBridge, ipcRenderer, webFrame } from "electron"; |
| 2 | + |
3 | 3 | import { |
4 | 4 | INITIAL_ICON_IMAGE, |
5 | 5 | IS_MAC, |
6 | | - RESOURCES_PATH, |
7 | | -} from "./helpers/constants"; |
| 6 | +} from "./preload/constants_preload"; |
8 | 7 | import { |
9 | 8 | createRecentThreadObserver, |
10 | 9 | createUnreadObserver, |
11 | 10 | focusFunctions, |
12 | 11 | recentThreadObserver, |
13 | | -} from "./helpers/observers"; |
14 | | -import { getProfileImg } from "./helpers/profileImage"; |
15 | | - |
16 | | -window.addEventListener("load", () => { |
17 | | - if (IS_MAC) { |
18 | | - const titlebarStyle = `#amd-titlebar { |
19 | | - -webkit-app-region: drag; |
20 | | - position: fixed; |
21 | | - width: 100%; |
22 | | - height: 64px; |
23 | | - top: 0; |
24 | | - left: 0; |
25 | | - background: none; |
26 | | - pointer-events: none; |
27 | | - }`; |
| 12 | +} from "./preload/observers"; |
28 | 13 |
|
29 | | - document.body.appendChild( |
30 | | - Object.assign(document.createElement("style"), { |
31 | | - textContent: titlebarStyle, |
32 | | - }) |
33 | | - ); |
34 | | - |
35 | | - const titlebar = document.createElement("div"); |
36 | | - titlebar.id = "amd-titlebar"; |
37 | | - document.querySelector("mw-app")?.parentNode?.prepend(titlebar); |
| 14 | +declare global { |
| 15 | + interface Window { |
| 16 | + interop: any; |
38 | 17 | } |
39 | | - |
40 | | - const conversationListObserver = new MutationObserver(() => { |
41 | | - if (document.querySelector("mws-conversations-list") != null) { |
42 | | - createUnreadObserver(); |
43 | | - createRecentThreadObserver(); |
44 | | - |
45 | | - // keep trying to get an image that isnt blank until they load |
46 | | - const interval = setInterval(() => { |
47 | | - const conversation = document.body.querySelector( |
48 | | - "mws-conversation-list-item" |
49 | | - ); |
50 | | - if (conversation) { |
51 | | - const canvas = conversation.querySelector( |
52 | | - "a div.avatar-container canvas" |
53 | | - ) as HTMLCanvasElement | null; |
54 | | - |
55 | | - if (canvas != null && canvas.toDataURL() != INITIAL_ICON_IMAGE) { |
56 | | - recentThreadObserver(); |
57 | | - // refresh for profile image loads after letter loads. |
58 | | - setTimeout(recentThreadObserver, 3000); |
59 | | - clearInterval(interval); |
60 | | - } |
61 | | - } |
62 | | - }, 250); |
63 | | - conversationListObserver.disconnect(); |
| 18 | +} |
| 19 | + |
| 20 | +const preload_init = () => { |
| 21 | + |
| 22 | + if (IS_MAC) { |
| 23 | + const titlebarStyle = `#amd-titlebar { |
| 24 | + -webkit-app-region: drag; |
| 25 | + position: fixed; |
| 26 | + width: 100%; |
| 27 | + height: 64px; |
| 28 | + top: 0; |
| 29 | + left: 0; |
| 30 | + background: none; |
| 31 | + pointer-events: none; |
| 32 | + }`; |
| 33 | + |
| 34 | + document.body.appendChild( |
| 35 | + Object.assign(document.createElement("style"), { |
| 36 | + textContent: titlebarStyle, |
| 37 | + }) |
| 38 | + ); |
| 39 | + |
| 40 | + const titlebar = document.createElement("div"); |
| 41 | + titlebar.id = "amd-titlebar"; |
| 42 | + document.querySelector("mw-app")?.parentNode?.prepend(titlebar); |
64 | 43 | } |
| 44 | + |
| 45 | + const conversationListObserver = new MutationObserver(() => { |
| 46 | + if (document.querySelector("mws-conversations-list") != null) { |
| 47 | + createUnreadObserver(); |
| 48 | + createRecentThreadObserver(); |
| 49 | + |
| 50 | + // keep trying to get an image that isnt blank until they load |
| 51 | + const interval = setInterval(() => { |
| 52 | + const conversation = document.body.querySelector( |
| 53 | + "mws-conversation-list-item" |
| 54 | + ); |
| 55 | + if (conversation) { |
| 56 | + const canvas = conversation.querySelector( |
| 57 | + "a div.avatar-container canvas" |
| 58 | + ) as HTMLCanvasElement | null; |
| 59 | + |
| 60 | + if (canvas != null && canvas.toDataURL() != INITIAL_ICON_IMAGE) { |
| 61 | + recentThreadObserver(); |
| 62 | + // refresh for profile image loads after letter loads. |
| 63 | + setTimeout(recentThreadObserver, 3000); |
| 64 | + clearInterval(interval); |
| 65 | + } |
| 66 | + } |
| 67 | + }, 250); |
| 68 | + conversationListObserver.disconnect(); |
| 69 | + } |
| 70 | + |
| 71 | + const title = document.head.querySelector("title"); |
| 72 | + if (title != null) { |
| 73 | + title.innerText = "Android Messages"; |
| 74 | + } |
| 75 | + }); |
| 76 | + |
| 77 | + conversationListObserver.observe(document.body, { |
| 78 | + attributes: false, |
| 79 | + subtree: true, |
| 80 | + childList: true, |
| 81 | + }); |
| 82 | +} |
65 | 83 |
|
66 | | - const title = document.head.querySelector("title"); |
67 | | - if (title != null) { |
68 | | - title.innerText = "Android Messages"; |
69 | | - } |
70 | | - }); |
71 | | - |
72 | | - conversationListObserver.observe(document.body, { |
73 | | - attributes: false, |
74 | | - subtree: true, |
75 | | - childList: true, |
76 | | - }); |
| 84 | +ipcRenderer.on("focus-conversation", (event, i) => { |
| 85 | + focusFunctions[i](); |
77 | 86 | }); |
78 | 87 |
|
79 | | -const OldNotification = window.Notification; |
80 | | - |
81 | | -// eslint-disable-next-line @typescript-eslint/ban-ts-comment |
82 | | -// @ts-ignore |
83 | | -window.Notification = function (title: string, options: NotificationOptions) { |
84 | | - const icon = getProfileImg(title); |
85 | | - |
86 | | - const hideContent = ipcRenderer.sendSync("should-hide-notification-content"); |
87 | | - |
88 | | - const notificationOpts: NotificationOptions = hideContent |
89 | | - ? { |
90 | | - body: "Click to open", |
91 | | - icon: path.resolve(RESOURCES_PATH, "icons", "64x64.png"), |
92 | | - } |
93 | | - : { |
94 | | - icon: icon?.toDataURL(), |
95 | | - body: options.body || "", |
96 | | - }; |
97 | | - |
98 | | - const newTitle = hideContent ? "New Message" : title; |
99 | | - const notification = new OldNotification(newTitle, notificationOpts); |
100 | | - notification.addEventListener("click", () => { |
| 88 | +contextBridge.exposeInMainWorld("interop", { |
| 89 | + show_main_window: () => { |
101 | 90 | ipcRenderer.send("show-main-window"); |
102 | | - document.dispatchEvent(new Event("focus")); |
| 91 | + }, |
| 92 | + flash_main: () => { |
| 93 | + ipcRenderer.send("flash-main-window-if-not-focused"); |
| 94 | + }, |
| 95 | + should_hide: () => { |
| 96 | + return ipcRenderer.sendSync("should-hide-notification-content"); |
| 97 | + }, |
| 98 | + get_icon: async () => { |
| 99 | + const data = await ipcRenderer.invoke("get-icon"); |
| 100 | + return `data:image/png;base64,${data}`; |
| 101 | + }, |
| 102 | + preload_init, |
| 103 | +}); |
| 104 | +webFrame.executeJavaScript(` |
| 105 | + window.addEventListener("load", async () => { |
| 106 | + window.interop.preload_init(); |
| 107 | + window.icon_data_uri = await window.interop.get_icon(); |
103 | 108 | }); |
104 | | - ipcRenderer.send("flash-main-window-if-not-focused"); |
105 | | - return notification; |
| 109 | +`); |
| 110 | +webFrame.executeJavaScript(`window.OldNotification = window.Notification; |
| 111 | +window.Notification = function (title, options) { |
| 112 | + try { |
| 113 | + const hideContent = window.interop.should_hide(); |
| 114 | +
|
| 115 | + const notificationOpts = hideContent |
| 116 | + ? { |
| 117 | + body: "Click to open", |
| 118 | + icon: window.icon_data_uri |
| 119 | + } |
| 120 | + : { |
| 121 | + body: options?.body || "", |
| 122 | + icon: options?.icon |
| 123 | + }; |
| 124 | +
|
| 125 | + const newTitle = hideContent ? "New Message" : title; |
| 126 | + const notification = new window.OldNotification(newTitle, notificationOpts); |
| 127 | + notification.addEventListener("click", () => { |
| 128 | + window.interop.show_main_window(); |
| 129 | + document.dispatchEvent(new Event("focus")); |
| 130 | + }); |
| 131 | + window.interop.flash_main(); |
| 132 | + return notification; |
| 133 | + } catch (e) { |
| 134 | + console.error(e); |
| 135 | + console.trace(); |
| 136 | + } |
106 | 137 | }; |
107 | 138 |
|
108 | | -// eslint-disable-next-line @typescript-eslint/ban-ts-comment |
109 | | -//@ts-ignore |
110 | 139 | window.Notification.permission = "granted"; |
111 | 140 | window.Notification.requestPermission = async () => "granted"; |
112 | | - |
113 | | -window.module.exports = null; |
114 | | - |
115 | | -ipcRenderer.on("focus-conversation", (event, i) => { |
116 | | - focusFunctions[i](); |
117 | | -}); |
| 141 | +`); |
| 142 | +contextBridge.exposeInMainWorld("module", {exports: null}); |
0 commit comments