minor changes

This commit is contained in:
2025-11-24 16:32:13 +01:00
parent 3c23aae864
commit 6d0cada610
14 changed files with 1059 additions and 491 deletions

View File

@@ -1,7 +1,7 @@
// Facebook Post Tracker Extension
// Uses API_BASE_URL from config.js
const EXTENSION_VERSION = '1.1.0';
const EXTENSION_VERSION = '1.2.0';
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"]';
@@ -155,6 +155,55 @@ const aiCredentialCache = {
timestamp: 0,
pending: null
};
const MODERATION_SETTINGS_CACHE_TTL = 5 * 60 * 1000;
const moderationSettingsCache = {
data: null,
timestamp: 0,
pending: null
};
const SPORTS_SCORING_DEFAULTS = {
threshold: 5,
weights: {
scoreline: 3,
scoreEmoji: 2,
sportEmoji: 2,
sportVerb: 1.5,
sportNoun: 2,
hashtag: 1.5,
teamToken: 2,
competition: 2,
celebration: 1,
location: 1
}
};
const DEFAULT_SPORT_TERMS = {
nouns: [
'auswärtssieg', 'heimsieg', 'derbysieg', 'revanche', 'spiel', 'spieltag', 'match', 'derby', 'finale',
'cup', 'pokal', 'liga', 'bundesliga', 'oberliga', 'kreisliga', 'bezirksliga', 'meisterschaft',
'turnier', 'halbzeit', 'tabellenplatz', 'tabelle', 'tor', 'tore', 'treffer', 'stadion', 'arena', 'halle',
'trainerteam', 'mannschaft', 'fans', 'fanblock', 'jugend', 'u17', 'u19', 'u15'
],
verbs: [
'gewinnen', 'siegen', 'geholt', 'erkämpfen', 'erkämpft', 'erkämpfen', 'drehen', 'punkten',
'trifft', 'treffen', 'schießt', 'schiesst', 'schießen', 'schiessen', 'verteidigen', 'stürmen', 'kämpfen'
],
competitions: [
'bundesliga', 'liga', 'serie a', 'premier league', 'champions league', 'europa league', 'dfb-pokal',
'cup', 'pokal', 'qualifikation', 'qualirunde', 'halbfinale', 'viertelfinale', 'achtelfinale', 'relegation'
],
celebrations: [
'sieg', 'siege', 'auswärtssieg', 'heimsieg', 'auswärtsspiel', 'punkte', 'punkte geholt', 'man of the match', 'motm',
'tabellenführung', 'tabellenplatz', 'tabellendritter', 'tabellenzweiter'
],
locations: [
'auswärts', 'heimspiel', 'derby', 'arena', 'stadion', 'halle', 'bolle', 'bölle', 'mosel'
],
negatives: [
'rezept', 'kochen', 'politik', 'wahl', 'bundestag', 'landtag', 'software', 'release', 'update', 'konzert',
'album', 'tour', 'podcast', 'karriere', 'job', 'stellenangebot', 'bewerbung'
]
};
console.log(`[FB Tracker v${EXTENSION_VERSION}] Extension loaded, API URL:`, API_URL);
@@ -235,6 +284,100 @@ async function fetchActiveAICredentials(forceRefresh = false) {
}
}
function normalizeModerationSettings(payload) {
if (!payload || typeof payload !== 'object') {
return {
sports_scoring_enabled: true,
sports_score_threshold: SPORTS_SCORING_DEFAULTS.threshold,
sports_score_weights: SPORTS_SCORING_DEFAULTS.weights,
sports_terms: DEFAULT_SPORT_TERMS,
sports_auto_hide_enabled: false
};
}
const threshold = (() => {
const parsed = parseFloat(payload.sports_score_threshold);
if (Number.isNaN(parsed) || parsed < 0) {
return SPORTS_SCORING_DEFAULTS.threshold;
}
return Math.min(50, Math.max(0, parsed));
})();
const weightsSource = payload.sports_score_weights && typeof payload.sports_score_weights === 'object'
? payload.sports_score_weights
: {};
const normalizedWeights = { ...SPORTS_SCORING_DEFAULTS.weights };
for (const key of Object.keys(SPORTS_SCORING_DEFAULTS.weights)) {
const raw = weightsSource[key];
const parsed = typeof raw === 'number' ? raw : parseFloat(raw);
if (Number.isFinite(parsed)) {
normalizedWeights[key] = Math.max(0, Math.min(10, parsed));
}
}
const normalizeTerms = (terms) => {
const base = { ...DEFAULT_SPORT_TERMS };
const src = terms && typeof terms === 'object' ? terms : {};
const normalizeList = (list, fallback) => {
if (!Array.isArray(list)) return fallback;
const cleaned = list
.map((entry) => typeof entry === 'string' ? entry.trim().toLowerCase() : '')
.filter((entry) => entry);
const unique = Array.from(new Set(cleaned)).slice(0, 200);
return unique.length ? unique : fallback;
};
return {
nouns: normalizeList(src.nouns, base.nouns),
verbs: normalizeList(src.verbs, base.verbs),
competitions: normalizeList(src.competitions, base.competitions),
celebrations: normalizeList(src.celebrations, base.celebrations),
locations: normalizeList(src.locations, base.locations),
negatives: normalizeList(src.negatives, base.negatives)
};
};
return {
sports_scoring_enabled: payload.sports_scoring_enabled !== false,
sports_score_threshold: threshold,
sports_score_weights: normalizedWeights,
sports_terms: normalizeTerms(payload.sports_terms),
sports_auto_hide_enabled: !!payload.sports_auto_hide_enabled
};
}
async function fetchModerationSettings(forceRefresh = false) {
const now = Date.now();
if (!forceRefresh && moderationSettingsCache.data && (now - moderationSettingsCache.timestamp < MODERATION_SETTINGS_CACHE_TTL)) {
return moderationSettingsCache.data;
}
if (moderationSettingsCache.pending) {
try {
return await moderationSettingsCache.pending;
} catch (error) {
// fallthrough to retry
}
}
moderationSettingsCache.pending = (async () => {
const response = await backendFetch(`${API_URL}/moderation-settings`);
if (!response.ok) {
const errorData = await response.json().catch(() => ({}));
throw new Error(errorData.error || 'Moderations-Einstellungen konnten nicht geladen werden');
}
const data = await response.json();
const normalized = normalizeModerationSettings(data);
moderationSettingsCache.data = normalized;
moderationSettingsCache.timestamp = Date.now();
return normalized;
})();
try {
return await moderationSettingsCache.pending;
} finally {
moderationSettingsCache.pending = null;
}
}
function formatAICredentialLabel(credential) {
if (!credential || typeof credential !== 'object') {
return 'Unbekannte AI';
@@ -658,13 +801,14 @@ async function recordSearchResultPost(primaryUrl, allUrlCandidates = [], options
return null;
}
const { skipIncrement = false, forceHide = false } = options || {};
const { skipIncrement = false, forceHide = false, sportsAutoHide = false } = options || {};
const payload = {
url: primaryUrl,
candidates: Array.isArray(allUrlCandidates) ? allUrlCandidates : [],
skip_increment: !!skipIncrement,
force_hide: !!forceHide
force_hide: !!forceHide,
sports_auto_hide: !!sportsAutoHide
};
const response = await backendFetch(`${API_URL}/search-posts`, {
@@ -1840,15 +1984,22 @@ function extractDeadlineFromPostText(postElement) {
// Check if date is valid (e.g., not 31.02.)
if (date.getMonth() === month - 1 && date.getDate() === day) {
const timeInfo = extractTimeAfterIndex(fullText, pattern.lastIndex);
const hasTime = Boolean(timeInfo);
if (timeInfo) {
date.setHours(timeInfo.hour, timeInfo.minute, 0, 0);
} else if (hasInclusiveKeywordNear(fullText, matchIndex)) {
date.setHours(23, 59, 0, 0);
}
const hasInclusiveTime = hasInclusiveKeywordNear(fullText, matchIndex);
const recordHasTime = hasTime || hasInclusiveTime;
if (hasInclusiveTime && !hasTime) {
date.setHours(23, 59, 0, 0);
}
// Only add if date is in the future
if (date > today) {
foundDates.push(date);
foundDates.push({ date, hasTime: recordHasTime });
}
}
}
@@ -1856,13 +2007,19 @@ function extractDeadlineFromPostText(postElement) {
}
// Pattern for "12. Oktober" or "12 Oktober"
const monthPattern = /\b(\d{1,2})\.?\s*(januar|jan|februar|feb|märz|mär|maerz|april|apr|mai|juni|jun|juli|jul|august|aug|september|sep|sept|oktober|okt|november|nov|dezember|dez)\b/gi;
const monthPattern = /\b(\d{1,2})\.?\s*(januar|jan|februar|feb|märz|mär|maerz|april|apr|mai|juni|jun|juli|jul|august|aug|september|sep|sept|oktober|okt|november|nov|dezember|dez)\s*(\d{2,4})?\b/gi;
let monthMatch;
while ((monthMatch = monthPattern.exec(fullText)) !== null) {
const day = parseInt(monthMatch[1], 10);
const monthStr = monthMatch[2].toLowerCase();
const month = monthNames[monthStr];
const year = today.getFullYear();
let year = today.getFullYear();
if (monthMatch[3]) {
year = parseInt(monthMatch[3], 10);
if (year < 100) {
year += 2000;
}
}
const matchIndex = monthMatch.index;
if (month && day >= 1 && day <= 31) {
@@ -1871,30 +2028,246 @@ function extractDeadlineFromPostText(postElement) {
// Check if date is valid
if (date.getMonth() === month - 1 && date.getDate() === day) {
const timeInfo = extractTimeAfterIndex(fullText, monthPattern.lastIndex);
const hasTime = Boolean(timeInfo);
if (timeInfo) {
date.setHours(timeInfo.hour, timeInfo.minute, 0, 0);
} else if (hasInclusiveKeywordNear(fullText, matchIndex)) {
date.setHours(23, 59, 0, 0);
}
const hasInclusiveTime = hasInclusiveKeywordNear(fullText, matchIndex);
const recordHasTime = hasTime || hasInclusiveTime;
if (hasInclusiveTime && !hasTime) {
date.setHours(23, 59, 0, 0);
}
// If date has passed this year, assume next year
if (date <= today) {
date.setFullYear(year + 1);
}
foundDates.push(date);
foundDates.push({ date, hasTime: recordHasTime });
}
}
}
// Return the earliest future date
if (foundDates.length > 0) {
foundDates.sort((a, b) => a - b);
return toDateTimeLocalString(foundDates[0]);
foundDates.sort((a, b) => {
const diff = a.date - b.date;
if (diff !== 0) {
return diff;
}
if (a.hasTime && !b.hasTime) return -1;
if (!a.hasTime && b.hasTime) return 1;
return 0;
});
return toDateTimeLocalString(foundDates[0].date);
}
return null;
}
function escapeRegex(value) {
return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
function collectKeywordMatches(keywords, text, limit = 20) {
if (!Array.isArray(keywords) || !keywords.length || !text) {
return [];
}
const found = [];
for (const keyword of keywords) {
if (!keyword) continue;
const pattern = new RegExp(`\\b${escapeRegex(keyword)}\\b`, 'gi');
const matches = text.match(pattern);
if (matches && matches.length) {
found.push(keyword);
if (found.length >= limit) {
break;
}
}
}
return Array.from(new Set(found));
}
function collectRegexMatches(regex, text, limit = 20) {
if (!regex || !(regex instanceof RegExp) || !text) {
return [];
}
const matches = Array.from(text.matchAll(regex)).map((m) => m[0]);
if (!matches.length) {
return [];
}
return Array.from(new Set(matches)).slice(0, limit);
}
function filterScorelines(candidates = []) {
const filtered = [];
for (const raw of candidates) {
const parts = raw.split(':').map((part) => part.trim());
if (parts.length !== 2) {
continue;
}
const [a, b] = parts.map((p) => parseInt(p, 10));
if (Number.isNaN(a) || Number.isNaN(b)) {
continue;
}
if (a < 0 || b < 0) {
continue;
}
if (a > 15 || b > 15) {
continue;
}
filtered.push(`${a}:${b}`);
}
return filtered;
}
function evaluateSportsScore(text, moderationSettings = null) {
if (!text || typeof text !== 'string') {
return null;
}
const normalizedText = text.toLowerCase();
const weights = {
...SPORTS_SCORING_DEFAULTS.weights,
...(moderationSettings && moderationSettings.sports_score_weights ? moderationSettings.sports_score_weights : {})
};
const threshold = moderationSettings && typeof moderationSettings.sports_score_threshold === 'number'
? moderationSettings.sports_score_threshold
: SPORTS_SCORING_DEFAULTS.threshold;
const terms = (() => {
const base = DEFAULT_SPORT_TERMS;
const incoming = moderationSettings && moderationSettings.sports_terms ? moderationSettings.sports_terms : null;
const normalizeList = (list, fallback) => {
if (!Array.isArray(list)) return fallback;
const cleaned = list
.map((entry) => typeof entry === 'string' ? entry.trim().toLowerCase() : '')
.filter((entry) => entry);
const unique = Array.from(new Set(cleaned)).slice(0, 200);
return unique.length ? unique : fallback;
};
const src = incoming && typeof incoming === 'object' ? incoming : {};
return {
nouns: normalizeList(src.nouns, base.nouns),
verbs: normalizeList(src.verbs, base.verbs),
competitions: normalizeList(src.competitions, base.competitions),
celebrations: normalizeList(src.celebrations, base.celebrations),
locations: normalizeList(src.locations, base.locations),
negatives: normalizeList(src.negatives, base.negatives)
};
})();
const matchesCount = (regex) => {
if (!regex || !(regex instanceof RegExp)) {
return 0;
}
const matches = normalizedText.match(regex);
return matches ? matches.length : 0;
};
const applyWeight = (count, weight, label, matches = []) => {
if (!count || !weight) {
return 0;
}
const effective = Math.min(count, 5);
const gain = effective * weight;
if (matches && matches.length) {
hitDetails.push(`${label}: ${matches.slice(0, 10).join(', ')}`);
} else {
hitDetails.push(`${label} x${effective} (+${gain.toFixed(1)})`);
}
score += gain;
return gain;
};
const hitDetails = [];
let score = 0;
const scorelineMatchesRaw = collectRegexMatches(/\b\d{1,2}\s*:\s*\d{1,2}\b/g, normalizedText);
const scorelineMatches = filterScorelines(scorelineMatchesRaw);
applyWeight(scorelineMatches.length, weights.scoreline, 'Ergebnis', scorelineMatches);
const scoreEmojiMatches = collectRegexMatches(/[0-9]️⃣/g, normalizedText)
.concat(collectRegexMatches(/\+\s*\d\b/g, normalizedText));
applyWeight(scoreEmojiMatches.length, weights.scoreEmoji, 'Punkte', scoreEmojiMatches);
const sportEmojiMatches = collectRegexMatches(/[⚽🏐🏀🏈🎾🏉🥅🏒🏑🏓🏸🤾🏏🎽🎳🥊🥋⛳]/g, normalizedText);
applyWeight(sportEmojiMatches.length, weights.sportEmoji, 'Sport-Emoji', sportEmojiMatches);
const verbMatches = collectKeywordMatches(terms.verbs, normalizedText);
applyWeight(verbMatches.length, weights.sportVerb, 'Sport-Verben', verbMatches);
const nounMatches = collectKeywordMatches(terms.nouns, normalizedText);
applyWeight(nounMatches.length, weights.sportNoun, 'Sport-Vokabeln', nounMatches);
const hashtagMatches = collectRegexMatches(/#(?:auswärtssieg|heimsieg|derbysieg|bundesliga|liga|pokal|cup|fc[a-z0-9]+|sv[a-z0-9]+|tsv[a-z0-9]+|sg[a-z0-9]+)/g, normalizedText);
applyWeight(hashtagMatches.length, weights.hashtag, 'Sport-Hashtags', hashtagMatches);
const teamMatches = collectRegexMatches(/\b(?:fc|sv|tsv|ssv|bvb|sge|fcb|hsv|vfb|fsv|sg|scl|djk)[\s\-]?[a-zäöüß0-9]+/gi, normalizedText);
applyWeight(teamMatches.length, weights.teamToken, 'Team-Kürzel', teamMatches);
const competitionMatches = collectKeywordMatches(terms.competitions, normalizedText);
applyWeight(competitionMatches.length, weights.competition, 'Liga/Turnier', competitionMatches);
const celebrationMatches = collectKeywordMatches(terms.celebrations, normalizedText);
applyWeight(celebrationMatches.length, weights.celebration, 'Ergebnisbezug', celebrationMatches);
const locationMatches = collectKeywordMatches(terms.locations, normalizedText);
applyWeight(locationMatches.length, weights.location, 'Spielort', locationMatches);
const nonSportMatches = collectKeywordMatches(terms.negatives, normalizedText);
const nonSportHits = nonSportMatches.length;
if (nonSportHits) {
const penalty = Math.min(2, nonSportHits) * 1;
score -= penalty;
hitDetails.push(`Gegenindizien: ${nonSportMatches.slice(0, 10).join(', ')}`);
}
const finalScore = Math.round(score * 10) / 10;
return {
score: finalScore,
threshold,
wouldHide: finalScore >= threshold,
hits: hitDetails
};
}
function buildSportsScoreBadge(scoreInfo) {
if (!scoreInfo) {
return null;
}
if (typeof scoreInfo.score !== 'number' || scoreInfo.score >= 0) {
return null;
}
const badge = document.createElement('span');
const wouldHide = !!scoreInfo.wouldHide;
const bg = wouldHide ? 'rgba(245, 158, 11, 0.18)' : 'rgba(59, 130, 246, 0.12)';
const border = wouldHide ? 'rgba(245, 158, 11, 0.5)' : 'rgba(59, 130, 246, 0.35)';
const color = wouldHide ? '#b45309' : '#1d4ed8';
badge.className = 'fb-tracker-score-badge';
badge.textContent = `Sport-Score ${scoreInfo.score.toFixed(1)} / ${scoreInfo.threshold}`;
if (scoreInfo.hits && scoreInfo.hits.length) {
const lines = scoreInfo.hits.map((hit) => `${hit}`).join('\n');
badge.title = `${lines}\n${wouldHide ? '≥ Schwellwert' : '< Schwellwert'}`;
} else {
badge.title = wouldHide ? 'Über Schwellwert' : 'Unter Schwellwert';
}
badge.style.cssText = `
display: inline-flex;
align-items: center;
gap: 6px;
padding: 4px 10px;
background: ${bg};
border: 1px solid ${border};
border-radius: 999px;
font-weight: 600;
color: ${color};
font-size: 12px;
`;
return badge;
}
function normalizeFacebookPostUrl(rawValue) {
if (typeof rawValue !== 'string') {
return '';
@@ -2456,8 +2829,50 @@ async function createTrackerUI(postElement, buttonBar, postNum = '?', options =
console.log('[FB Tracker] Post #' + postNum + ' - UI created for new post');
// Add AI button for new posts
await addAICommentButton(container, postElement);
// Add AI button for new posts
await addAICommentButton(container, postElement);
}
let sportsScoreInfo = null;
try {
const moderationSettings = await fetchModerationSettings();
if (moderationSettings && moderationSettings.sports_scoring_enabled !== false) {
const postTextForScore = extractPostText(postElement);
if (postTextForScore) {
sportsScoreInfo = evaluateSportsScore(postTextForScore, moderationSettings);
}
if (
moderationSettings.sports_auto_hide_enabled
&& sportsScoreInfo
&& sportsScoreInfo.wouldHide
&& !isTracked
&& !likedByCurrentUser
) {
if (isDialogContext) {
console.log('[FB Tracker] Post #' + postNum + ' - Would auto-hide by sports score but skipping in dialog context');
} else {
console.log('[FB Tracker] Post #' + postNum + ' - Auto-hidden by sports score', sportsScoreInfo);
try {
await recordSearchResultPost(postUrlData.url, postUrlData.allCandidates, { forceHide: true, sportsAutoHide: true });
} catch (error) {
console.debug('[FB Tracker] Auto-hide scoring could not persist hide state:', error);
}
hidePostElement(postElement);
processedPostUrls.set(encodedUrl, {
element: postElement,
createdAt: Date.now(),
hidden: true,
searchSeenCount: searchTrackingInfo && typeof searchTrackingInfo.seen_count === 'number'
? searchTrackingInfo.seen_count
: null
});
return;
}
}
}
} catch (error) {
console.debug('[FB Tracker] Sport-Scoring nicht verfügbar:', error);
}
if (isSearchResult) {
@@ -2596,6 +3011,15 @@ async function createTrackerUI(postElement, buttonBar, postNum = '?', options =
resetHover();
container.insertBefore(info, container.firstChild);
const sportsScoreBadge = buildSportsScoreBadge(sportsScoreInfo);
if (sportsScoreBadge) {
container.insertBefore(sportsScoreBadge, info.nextSibling);
}
} else {
const sportsScoreBadge = buildSportsScoreBadge(sportsScoreInfo);
if (sportsScoreBadge) {
container.insertBefore(sportsScoreBadge, container.firstChild);
}
}
// Insert UI - try multiple strategies to find stable insertion point
@@ -3900,6 +4324,18 @@ async function setCommentText(inputElement, text, options = {}) {
}
}
function sanitizeAIComment(comment) {
if (!comment || typeof comment !== 'string') {
return '';
}
const tempDiv = document.createElement('div');
tempDiv.innerHTML = comment;
const sanitized = tempDiv.textContent || tempDiv.innerText || '';
return sanitized.trim();
}
/**
* Generate AI comment for a post
*/
@@ -3928,7 +4364,13 @@ async function generateAIComment(postText, profileNumber, options = {}) {
}
const data = await response.json();
return data.comment;
const sanitizedComment = sanitizeAIComment(data.comment);
if (!sanitizedComment) {
throw new Error('AI-Antwort enthält keinen gültigen Text');
}
return sanitizedComment;
} catch (error) {
console.error('[FB Tracker] AI comment generation failed:', error);