-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent_script.js
More file actions
259 lines (232 loc) · 7.69 KB
/
Copy pathcontent_script.js
File metadata and controls
259 lines (232 loc) · 7.69 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
console.log("FB Post Sender content script loaded");
let isExtensionActive = false;
let isPostingTab = false;
let stateCheckInterval;
// Add this function to verify posting tab status
function verifyPostingTabStatus() {
chrome.runtime.sendMessage({action: "verifyPostingTab"}, function(response) {
if (response && response.isPostingTab !== isPostingTab) {
isPostingTab = response.isPostingTab;
isExtensionActive = response.isPostingTab;
updateModalStatus();
updateButtonStyles();
}
});
}
// Initialize modal function
function initializeModal() {
if (!document.getElementById("extension-modal")) {
const modal = document.createElement("div");
modal.id = "extension-modal";
modal.className = isPostingTab ? "posting-mode" : "sharing-mode";
modal.innerHTML = `
<h3>Auto-Posting ${
isPostingTab
? "Active (Receiving Posts)"
: "Active (Sharing Mode)"
}</h3>
<div id="posts-list"></div>
<div class="status-message">
${
isPostingTab
? "Waiting to receive posts..."
: 'Click "Post on Page" buttons to share'
}
</div>
<div class="connection-status">Connected</div>
`;
document.body.appendChild(modal);
}
}
// Update modal status
function updateModalStatus() {
const modal = document.getElementById("extension-modal");
if (modal) {
modal.className = isPostingTab ? "posting-mode" : "sharing-mode";
const header = modal.querySelector("h3");
const statusMessage = modal.querySelector(".status-message");
const connectionStatus = modal.querySelector(".connection-status");
if (header) {
header.textContent = `Auto-Posting ${
isPostingTab ? "Active (Receiving Posts)" : "Active (Sharing Mode)"
}`;
}
if (statusMessage) {
statusMessage.textContent = isPostingTab
? "Waiting to receive posts..."
: 'Click "Post on Page" buttons to share';
}
if (connectionStatus) {
connectionStatus.textContent = isPostingTab
? "Connected"
: "Sharing Mode Active";
}
}
}
// Listen for messages from background script
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "toggleStatus") {
isExtensionActive = request.isActive;
isPostingTab = request.isPostingTab;
if (isExtensionActive) {
initializeModal();
// Start periodic state verification if this is the posting tab
if (isPostingTab) {
if (stateCheckInterval) {
clearInterval(stateCheckInterval);
}
stateCheckInterval = setInterval(verifyPostingTabStatus, 5000);
}
} else {
const modal = document.getElementById('extension-modal');
if (modal) modal.remove();
if (stateCheckInterval) {
clearInterval(stateCheckInterval);
}
}
updateButtonStyles();
updateModalStatus();
} else if (request.action === "newPost" && isPostingTab) {
const postsList = document.getElementById("posts-list");
if (postsList) {
const postElement = document.createElement("div");
postElement.textContent = `Posted: ${request.post.caption.substring(
0,
30
)}...`;
postsList.appendChild(postElement);
}
}
});
// Add cleanup when the page is unloaded
window.addEventListener('unload', function() {
if (stateCheckInterval) {
clearInterval(stateCheckInterval);
}
});
// Check initial status when content script loads
chrome.runtime.sendMessage({ action: "checkStatus" }, function (response) {
isExtensionActive = response.isActive;
isPostingTab = response.isPostingTab;
if (isExtensionActive) {
initializeModal();
}
updateButtonStyles();
});
function updateButtonStyles() {
const buttons = document.querySelectorAll(".repost-button");
buttons.forEach((button) => {
if (isExtensionActive && !isPostingTab) {
// Only show active buttons in sharing tabs
button.classList.add("active");
button.style.backgroundColor = "#4CAF50";
button.style.display = "inline-block";
} else {
button.classList.remove("active");
button.style.backgroundColor = "#ccc";
button.style.display = isPostingTab ? "none" : "inline-block"; // Hide buttons in posting tab
}
});
}
function getTextFromParents(element, maxDepth = 10) {
let currentElement = element;
let depth = 0;
let textArray = [];
while (currentElement && depth < maxDepth) {
for (let node of currentElement.childNodes) {
let trimmedText = node.textContent.trim();
if (trimmedText) {
textArray.push(trimmedText);
}
}
currentElement = currentElement.parentElement;
depth++;
}
// Remove duplicates
textArray = [...new Set(textArray)];
// Filter out unwanted phrases and words
textArray = textArray.filter(
(text) =>
!text.toLowerCase().includes("post on page") &&
!text.toLowerCase().includes("facebook") &&
!text.toLowerCase().includes("tahir") &&
!text.toLowerCase().includes("reaction") &&
!text.toLowerCase().includes("shared") &&
!text.toLowerCase().includes("uploads") &&
!text.toLowerCase().includes("groups")
);
// Join the array elements into a single string
return textArray.join(" ");
}
function captureAndSendPost(img) {
try {
const imageUrl = img.src;
let caption = getTextFromParents(img);
console.log("Captured post:", { imageUrl, caption });
chrome.runtime.sendMessage(
{
action: "sendToExtensionB",
data: { imageUrl, caption },
},
(response) => {
if (response && response.success) {
alert("Post captured and will be shared!");
} else {
alert(
response.message || "Failed to capture post. Please try again."
);
}
}
);
} catch (error) {
console.error("Error capturing post:", error);
alert(
"An error occurred while capturing the post. Please try again." + error
);
}
}
function injectPostButtons() {
const images = document.querySelectorAll('img[src^="https://scontent"]');
console.log(`Found ${images.length} relevant images`);
images.forEach((img, index) => {
const parent = img.parentElement;
if (parent && !parent.querySelector(".repost-button")) {
const button = document.createElement("button");
button.textContent = "Post on Page";
button.className = "repost-button";
button.addEventListener("click", (e) => {
e.preventDefault();
e.stopPropagation();
// if (!isExtensionActive && !isPostingTab) {
// alert("Please activate the extension by clicking its icon first.");
// return;
// }
captureAndSendPost(img);
});
// Insert the button after the image
if (img.nextSibling) {
parent.insertBefore(button, img.nextSibling);
} else {
parent.appendChild(button);
}
}
});
// Update styles for all buttons
updateButtonStyles();
}
// Run the injection when the page loads and periodically
injectPostButtons();
setInterval(injectPostButtons, 3000);
// Optional: Add mutation observer to handle dynamically loaded content
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.addedNodes.length) {
injectPostButtons();
}
});
});
// Start observing the document with the configured parameters
observer.observe(document.body, {
childList: true,
subtree: true,
});