Exempt repeat AI comments on same post

This commit is contained in:
2026-04-07 16:54:24 +02:00
parent 52a21ca089
commit b6f8572aae
3 changed files with 155 additions and 32 deletions

View File

@@ -1,7 +1,7 @@
// Facebook Post Tracker Extension
// Uses API_BASE_URL from config.js
const EXTENSION_VERSION = '1.2.2';
const EXTENSION_VERSION = '1.2.3';
const PROCESSED_ATTR = 'data-fb-tracker-processed';
const PENDING_ATTR = 'data-fb-tracker-pending';
const DIALOG_ROOT_SELECTOR = '[role="dialog"], [data-pagelet*="Modal"], [data-pagelet="StoriesRecentStoriesFeedSection"]';
@@ -6509,7 +6509,8 @@ async function generateAIComment(postText, profileNumber, options = {}) {
maxAttempts = 3,
flowId = null,
source = 'extension-ai-button',
returnMeta = false
returnMeta = false,
postKey = null
} = options;
const normalizedFlowId = typeof flowId === 'string' && flowId.trim()
? flowId.trim()
@@ -6524,6 +6525,9 @@ async function generateAIComment(postText, profileNumber, options = {}) {
if (typeof preferredCredentialId === 'number' && !Number.isNaN(preferredCredentialId)) {
basePayload.preferredCredentialId = preferredCredentialId;
}
if (typeof postKey === 'string' && postKey.trim()) {
basePayload.postKey = postKey.trim();
}
const requestAttempts = [];
let lastError = null;
@@ -6605,7 +6609,8 @@ async function generateAIComment(postText, profileNumber, options = {}) {
flowId: effectiveFlowId,
requestAttempts,
backendTimings: data.timings && data.timings.backend ? data.timings.backend : null,
autoCommentRateLimitStatus: data.autoCommentRateLimitStatus || null
autoCommentRateLimitStatus: data.autoCommentRateLimitStatus || null,
autoCommentRateLimitGlobalStatus: data.autoCommentRateLimitGlobalStatus || null
};
return returnMeta ? result : sanitizedComment;
}
@@ -6664,6 +6669,27 @@ async function generateAIComment(postText, profileNumber, options = {}) {
throw finalError;
}
async function fetchAIAutoCommentRateLimitStatus(profileNumber, options = {}) {
const normalizedProfile = parseInt(profileNumber, 10);
if (!normalizedProfile) {
return null;
}
const params = new URLSearchParams();
params.set('profileNumber', String(normalizedProfile));
if (typeof options.postKey === 'string' && options.postKey.trim()) {
params.set('postKey', options.postKey.trim());
}
const response = await backendFetch(`${API_URL}/ai/auto-comment-rate-limit-status?${params.toString()}`);
const data = await response.json().catch(() => ({}));
if (!response.ok) {
throw new Error(data.error || 'Failed to fetch AI rate limit status');
}
return data && data.status ? data.status : null;
}
async function handleSelectionAIRequest(selectionText, sendResponse) {
try {
const normalizedSelection = normalizeSelectionText(selectionText);
@@ -6869,6 +6895,20 @@ async function addAICommentButton(container, postElement) {
: 'Generiere automatisch einen passenden Kommentar';
};
const getCurrentPostKey = () => {
const raw = encodedPostUrl || (container && container.getAttribute('data-post-url'));
if (!raw) {
return null;
}
try {
const decoded = decodeURIComponent(raw);
return normalizeFacebookPostUrl(decoded) || decoded;
} catch (error) {
console.warn('[FB Tracker] Konnte Post-Key nicht lesen:', error);
return null;
}
};
const buildBlockedButtonTitle = (status) => {
if (!status || !status.blocked) {
return getDefaultButtonTitle();
@@ -7024,8 +7064,10 @@ async function addAICommentButton(container, postElement) {
applyAvailabilityState(null);
return null;
}
const settings = await fetchAISettings(forceRefresh);
const status = getAIAutoCommentRateLimitStatusFromSettings(settings, profileNumber);
const status = await fetchAIAutoCommentRateLimitStatus(profileNumber, {
forceRefresh,
postKey: getCurrentPostKey()
});
applyAvailabilityState(status);
return status;
} catch (error) {
@@ -7040,7 +7082,7 @@ async function addAICommentButton(container, postElement) {
return await rateLimitRefreshPromise;
};
const handleSharedAISettingsUpdate = async (settings) => {
const handleSharedAISettingsUpdate = async () => {
if (!wrapper.isConnected) {
clearBlockedCountdown();
aiAvailabilitySubscribers.delete(handleSharedAISettingsUpdate);
@@ -7048,13 +7090,7 @@ async function addAICommentButton(container, postElement) {
}
try {
const profileNumber = await fetchBackendProfileNumber();
if (!profileNumber) {
applyAvailabilityState(null);
return;
}
const status = getAIAutoCommentRateLimitStatusFromSettings(settings, profileNumber);
applyAvailabilityState(status);
await refreshAvailabilityState(true);
} catch (error) {
console.warn('[FB Tracker] Failed to apply shared AI settings update:', error);
}
@@ -7841,7 +7877,8 @@ async function addAICommentButton(container, postElement) {
preferredCredentialId,
flowId: flowTrace.flowId,
source: flowTrace.source,
returnMeta: true
returnMeta: true,
postKey: getCurrentPostKey()
});
endPhase('aiRequestMs', {
traceId: aiResult.traceId || null,
@@ -7849,9 +7886,12 @@ async function addAICommentButton(container, postElement) {
});
mergeTraceInfo(aiResult);
if (aiResult.autoCommentRateLimitStatus) {
const nextSettings = upsertAIAutoCommentRateLimitStatus(aiSettingsCache.data, aiResult.autoCommentRateLimitStatus);
applyAISettingsSnapshot(nextSettings, { timestamp: Date.now(), broadcast: true });
applyAvailabilityState(aiResult.autoCommentRateLimitStatus);
const globalRateLimitStatus = aiResult.autoCommentRateLimitGlobalStatus || null;
if (globalRateLimitStatus) {
const nextSettings = upsertAIAutoCommentRateLimitStatus(aiSettingsCache.data, globalRateLimitStatus);
applyAISettingsSnapshot(nextSettings, { timestamp: Date.now(), broadcast: true });
}
} else {
void refreshAvailabilityState(true, profileNumber);
}
@@ -7969,7 +8009,7 @@ async function addAICommentButton(container, postElement) {
if (error && error.status === 429) {
const syncedStatus = await refreshAvailabilityState(true);
if (syncedStatus) {
if (syncedStatus && !syncedStatus.same_post_exempt) {
const nextSettings = upsertAIAutoCommentRateLimitStatus(aiSettingsCache.data, syncedStatus);
applyAISettingsSnapshot(nextSettings, { timestamp: Date.now(), broadcast: true });
}