-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpreload.js
More file actions
172 lines (156 loc) · 6.22 KB
/
preload.js
File metadata and controls
172 lines (156 loc) · 6.22 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
const { ipcRenderer } = require('electron');
// --- 1. Notification Interception (Main + Service Worker) ---
function filterNotification(title, options) {
const lowerTitle = title ? title.toLowerCase() : '';
const lowerBody = (options && options.body) ? options.body.toLowerCase() : '';
const isMessage = lowerTitle.includes('message') || lowerTitle.includes('messenger') ||
lowerTitle.includes('訊息') || lowerTitle.includes('聊天室') ||
lowerBody.includes('sent') || lowerBody.includes('傳送') ||
lowerBody.includes('message') || lowerBody.includes('訊息');
const isFB = lowerTitle.includes('facebook') || lowerBody.includes('facebook');
if (isFB && !isMessage) return true;
if (!isMessage) return true;
return false;
}
const OriginalNotification = window.Notification;
const CustomNotification = function (title, options) {
if (filterNotification(title, options)) {
return { onclick: null, onshow: null, onerror: null, onclose: null, close: () => {} };
}
return new OriginalNotification(title, options);
};
CustomNotification.requestPermission = OriginalNotification.requestPermission;
CustomNotification.permission = OriginalNotification.permission;
window.Notification = CustomNotification;
if ('ServiceWorkerRegistration' in window) {
const originalShowNotification = ServiceWorkerRegistration.prototype.showNotification;
ServiceWorkerRegistration.prototype.showNotification = function(title, options) {
if (filterNotification(title, options)) return Promise.resolve();
return originalShowNotification.call(this, title, options);
};
}
// --- 2. DOM-Only Badge Detection ---
function getMessengerUnreadCount() {
const messengerSelectors = [
'a[href^="/messages/"]',
'div[role="button"][aria-label*="Messenger"]',
'div[role="button"][aria-label*="訊息"]',
'div[aria-label="Messenger"]',
'div[aria-label="訊息"]'
];
for (const selector of messengerSelectors) {
const element = document.querySelector(selector);
if (element) {
const badge = element.querySelector('span[role="gridcell"], span[aria-hidden="false"], div[style*="background-color"] span, span:not(:empty)');
if (badge) {
const count = parseInt(badge.textContent.trim(), 10);
if (!isNaN(count) && count > 0) return count;
}
return 0;
}
}
const title = document.title;
const match = title.match(/^\((\d+)\)/);
if (match) {
const isGeneric = title.includes('Messenger | Facebook') || title.includes('Messages | Facebook') || title.includes('Facebook');
if (!isGeneric) return parseInt(match[1], 10);
}
return 0;
}
let lastCount = -1;
function updateBadge() {
const count = getMessengerUnreadCount();
if (count !== lastCount) {
console.log(`[Badge] Unread count: ${count}`);
lastCount = count;
if (count > 0) {
const dataUrl = drawBadge(count);
ipcRenderer.send('update-badge', { dataUrl, text: count.toString() });
} else {
ipcRenderer.send('update-badge', { dataUrl: null, text: '' });
}
}
}
// --- 3. UI Cleaning & Context Menu ---
function injectStyles() {
const style = document.createElement('style');
style.textContent = `
div[aria-label="Notifications"], div[aria-label="通知"],
a[href*="/notifications/"] { display: none !important; }
div[aria-label="Notifications"] span, div[aria-label="通知"] span { display: none !important; }
`;
document.head.appendChild(style);
}
function drawBadge(count) {
const radius = 32;
const size = radius * 2;
const canvas = document.createElement('canvas');
canvas.width = size;
canvas.height = size;
const ctx = canvas.getContext('2d');
ctx.fillStyle = '#FF3B30';
ctx.beginPath();
ctx.arc(radius, radius, radius, 0, Math.PI * 2);
ctx.fill();
ctx.fillStyle = 'white';
ctx.font = 'bold 40px Arial';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
let text = count > 99 ? '99+' : count.toString();
if (count > 99) ctx.font = 'bold 30px Arial';
else if (count > 9) ctx.font = 'bold 36px Arial';
ctx.fillText(text, radius, radius + 4);
return canvas.toDataURL();
}
// Context Menu Features
let lastRightClickElement = null;
// Use capture: true to ensure we get the element even if FB stops propagation
window.addEventListener('contextmenu', (e) => {
lastRightClickElement = e.target;
}, true);
function getMessageContainer(el) {
if (!el) return null;
// Try to find the closest element that represents the message text
// Messenger uses dir="auto" for text containers.
// We want the outermost one if they are nested.
let current = el.closest('[dir="auto"]');
if (!current) return el;
let highest = current;
while (current.parentElement) {
current = current.parentElement.closest('[dir="auto"]');
if (current) {
highest = current;
} else {
break;
}
}
return highest;
}
ipcRenderer.on('select-all-message', () => {
const container = getMessageContainer(lastRightClickElement);
if (container) {
const range = document.createRange();
range.selectNodeContents(container);
const selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
}
});
ipcRenderer.on('copy-entire-message', () => {
const container = getMessageContainer(lastRightClickElement);
if (container) {
const text = container.innerText || container.textContent || '';
if (text) ipcRenderer.send('copy-to-clipboard', text);
}
});
window.addEventListener('DOMContentLoaded', () => {
injectStyles();
updateBadge();
const observer = new MutationObserver(updateBadge);
observer.observe(document.body, { childList: true, subtree: true });
const titleElement = document.querySelector('title');
if (titleElement) {
new MutationObserver(updateBadge).observe(titleElement, { childList: true, subtree: true, characterData: true });
}
setInterval(updateBadge, 2000);
});