// ============================================================================
// File: facebook.js
// ============================================================================
(global => {
'use strict';
let localGroups = [];
let selectedGroupIds = new Set();
let savedTags = {};
let uploadedImages = [];
const FACEBOOK_INTEGRITY_SALT = "FritreeFacebookGroupRegistrySymmetricValidationMatrix_2026";
/**
* Injects HTML structures into placeholder
*/
function renderStructuralHTMLTemplates() {
const paneFbPub = document.getElementById('pane-fb-pub');
if (paneFbPub) {
paneFbPub.innerHTML = `
Facebook Group Registry
Groups Selected: 0 of 0
Establish your active Facebook session so we can load your communities list here.
Facebook Campaign Composer
Currently dispatching 0 archived post(s) under the active algorithm: Balanced Random Protection.
Unique Variations: 1
Uniqueness score: 0%
`;
}
}
async function calculateDatabaseSignature(groups, selectedIds) {
const structuralConcat = groups.map(g => `${g.id}:${g.isAdmin}`).sort().join('||') +
"||" + Array.from(selectedIds).sort().join(',');
if (typeof FritreeCrypto !== 'undefined' && typeof FritreeCrypto.signReceipt === 'function') {
return await FritreeCrypto.signReceipt(
"FB_GROUPS",
structuralConcat.length,
"verify_integrity",
structuralConcat,
FACEBOOK_INTEGRITY_SALT
);
}
let hash = 0;
const inputStr = structuralConcat + FACEBOOK_INTEGRITY_SALT;
for (let i = 0; i < inputStr.length; i++) {
const char = inputStr.charCodeAt(i);
hash = ((hash << 5) - hash) + char;
hash |= 0;
}
return "lite_sec_sig_" + Math.abs(hash).toString(16);
}
async function verifyRegistryIntegrity() {
try {
if (localGroups.length === 0) return true;
const savedSignature = await FritreeStorage.get('local_fb_groups_integrity_sig', '');
if (!savedSignature) return true;
const computedSignature = await calculateDatabaseSignature(localGroups, selectedGroupIds);
if (savedSignature !== computedSignature) {
await FritreeStorage.set('local_fb_groups_integrity_sig', computedSignature);
}
return true;
} catch (e) {
console.error("[Fritree Crypto] Failed to audit groups registry signature:", e);
return true;
}
}
async function saveGroupsAndSelectionsSecurely() {
const computedSignature = await calculateDatabaseSignature(localGroups, selectedGroupIds);
await FritreeStorage.set('local_groups', localGroups);
await FritreeStorage.set('local_selected_groups', Array.from(selectedGroupIds));
await FritreeStorage.set('local_fb_groups_integrity_sig', computedSignature);
}
async function initFacebookModule() {
try {
renderStructuralHTMLTemplates();
const rawGroups = await FritreeStorage.get('local_groups', []);
localGroups = Array.isArray(rawGroups) ? rawGroups : [];
const cachedSelected = await FritreeStorage.get('local_selected_groups', []);
selectedGroupIds = new Set(Array.isArray(cachedSelected) ? cachedSelected.filter(id => typeof id === 'string') : []);
const rawTags = await FritreeStorage.get('local_saved_tags', {});
savedTags = sanitizeSavedTags(rawTags);
await verifyRegistryIntegrity();
const unifiedInput = document.getElementById('unified-group-input');
const selectKeywordBtn = document.getElementById('btn-select-similar');
const saveSegmentBtn = document.getElementById('btn-save-tag');
const selectAllBtn = document.getElementById('btn-select-all');
const deselectAllBtn = document.getElementById('btn-deselect-all');
const selectAdminBtn = document.getElementById('btn-select-admin');
if (unifiedInput) {
unifiedInput.addEventListener('input', executeUnifiedGroupsSearch);
}
if (selectKeywordBtn) {
selectKeywordBtn.addEventListener('click', executeKeywordTargetingFromUnifiedInput);
}
if (saveSegmentBtn) {
saveSegmentBtn.addEventListener('click', executeSegmentSavingFromUnifiedInput);
}
if (selectAllBtn) selectAllBtn.addEventListener('click', () => selectAllGroupsInRegistry(true));
if (deselectAllBtn) deselectAllBtn.addEventListener('click', () => selectAllGroupsInRegistry(false));
if (selectAdminBtn) selectAdminBtn.addEventListener('click', selectManagedGroupsOnly);
const dropzone = document.getElementById('image-dropzone');
const fileInput = document.getElementById('image-file-input');
if (dropzone && fileInput) {
dropzone.addEventListener('click', () => fileInput.click());
fileInput.addEventListener('change', handleMediaFileSelection);
dropzone.addEventListener('dragover', (e) => {
e.preventDefault();
dropzone.style.borderColor = 'var(--primary)';
dropzone.style.background = 'rgba(24, 119, 242, 0.05)';
});
dropzone.addEventListener('dragleave', () => {
dropzone.style.borderColor = 'var(--border)';
dropzone.style.background = 'transparent';
});
dropzone.addEventListener('drop', (e) => {
e.preventDefault();
dropzone.style.borderColor = 'var(--border)';
dropzone.style.background = 'transparent';
if (e.dataTransfer.files.length > 0) {
processIncomingMediaFiles(e.dataTransfer.files);
}
});
}
renderGroupsList(localGroups);
renderSavedSegmentsList();
updateSelectedCount();
if (typeof window.addLog === 'function') {
window.addLog("Facebook groups registry and targeting segments loaded successfully.", "info");
}
} catch (e) {
console.error("[Fritree UI] Failed to initialize Facebook target manager module:", e);
}
}
function renderGroupsList(groups) {
const listContainer = document.getElementById('groups-list');
if (!listContainer) return;
listContainer.innerHTML = '';
if (groups.length === 0) {
const emptyState = document.createElement('div');
emptyState.style.cssText = 'padding: 30px; text-align: center; color: var(--text-muted); font-size: 13px; font-weight: 500;';
emptyState.innerHTML = '