-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
136 lines (116 loc) · 4.86 KB
/
Copy pathcontent.js
File metadata and controls
136 lines (116 loc) · 4.86 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
console.log('[Disappearing Photos] Content script loaded');
// Inject the Telegram integration script into page context (always runs)
(function() {
const injectScript = document.createElement('script');
injectScript.src = chrome.runtime.getURL('inject.js');
injectScript.onload = function() {
console.log('[Disappearing Photos] ✅ Telegram integration script loaded');
this.remove();
};
injectScript.onerror = function() {
console.error('[Disappearing Photos] ❌ Failed to load Telegram integration');
};
(document.head || document.documentElement).prepend(injectScript);
// Also inject the send helper script
const helperScript = document.createElement('script');
helperScript.src = chrome.runtime.getURL('send-helper.js');
helperScript.onload = function() {
console.log('[Disappearing Photos] ✅ Send helper script loaded');
this.remove();
};
helperScript.onerror = function() {
console.error('[Disappearing Photos] ❌ Failed to load send helper');
};
(document.head || document.documentElement).prepend(helperScript);
})();
// Listen for messages from popup
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
// Check initialization status
if (request.action === 'checkInitStatus') {
const isInitialized = localStorage.getItem('telegram_worker_patched') === 'true';
sendResponse({ initialized: isInitialized });
return;
}
// Deinitialize extension (remove patch flag)
if (request.action === 'deinitializeExtension') {
console.log('[Disappearing Photos] Deinitializing extension...');
// Inject the deinit helper script
const script = document.createElement('script');
script.src = chrome.runtime.getURL('deinit-helper.js');
script.onload = function() {
console.log('[Disappearing Photos] ✅ Deinit helper script loaded');
this.remove();
};
script.onerror = function() {
console.error('[Disappearing Photos] ❌ Failed to load deinit helper');
sendResponse({ success: false, error: 'Failed to load deinit helper script' });
};
// Listen for completion message
const messageListener = (event) => {
if (event.data.type === 'DEINIT_COMPLETE') {
window.removeEventListener('message', messageListener);
sendResponse({ success: true });
}
};
window.addEventListener('message', messageListener);
// Inject the script
(document.head || document.documentElement).appendChild(script);
return true; // Keep the message channel open for async response
}
// Initialize extension (run cache patcher)
if (request.action === 'initializeExtension') {
console.log('[Disappearing Photos] Initializing extension...');
// Inject the cache patcher script
const script = document.createElement('script');
script.src = chrome.runtime.getURL('cache-patcher.js');
script.onload = function() {
console.log('[Disappearing Photos] ✅ Cache patcher script loaded');
this.remove();
};
script.onerror = function() {
console.error('[Disappearing Photos] ❌ Failed to load cache patcher');
sendResponse({ success: false, error: 'Failed to load cache patcher script' });
};
// Listen for result from cache patcher
const messageListener = (event) => {
if (event.data.type === 'CACHE_PATCH_RESULT') {
window.removeEventListener('message', messageListener);
if (event.data.success) {
sendResponse({ success: true, alreadyPatched: event.data.alreadyPatched });
} else {
sendResponse({ success: false, error: event.data.error || 'Cache patching failed' });
}
}
};
window.addEventListener('message', messageListener);
// Inject the script
(document.head || document.documentElement).appendChild(script);
return true; // Keep the message channel open for async response
}
// Send disappearing photo
if (request.action === 'sendDisappearingPhoto') {
// Listen for result from page context first
const messageListener = (event) => {
if (event.data.type === 'DISAPPEARING_PHOTO_RESULT') {
window.removeEventListener('message', messageListener);
sendResponse(event.data);
}
};
window.addEventListener('message', messageListener);
// Send message to page context via postMessage
// The send-helper.js script will receive this and call sendDisappearingPhoto
const message = {
type: 'SEND_DISAPPEARING_PHOTO_REQUEST',
ttl: request.ttl
};
// Check if it's a URL or blob data
if (request.imageUrl) {
message.imageUrl = request.imageUrl;
} else if (request.blobData) {
message.blobData = request.blobData;
message.mimeType = request.mimeType;
}
window.postMessage(message, '*');
return true; // Keep the message channel open for async response
}
});