Commit d1311178 authored by Stefy Spora's avatar Stefy Spora

Fix story-data retrieval by fetching raw HTML from GET request

- Update getCurrentMediaData function in content.js to fetch raw HTML from current page URL
- Parse story-data script tag from raw HTML response instead of relying on DOM
- Update background.js executePrivacyUpdateFromContext to use same approach
- Handle cases where story-data script is removed by JavaScript after page load
- Add proper error handling and fallback to default values
- Maintain existing functionality while fixing story-data retrieval issue
- Update bulk processing functions to use new story-data retrieval method
parent 8eea4178
......@@ -347,7 +347,7 @@ function executePrivacyUpdateFromContext(privacyLevel = 'friends_only') {
throw new Error('Could not find CSRF token. Please refresh the page and try again.');
}
// Step 3: Parse story-data script tag to get current media data
// Step 3: Fetch raw HTML and parse story-data
let currentData = {
title: '',
caption: '',
......@@ -357,71 +357,105 @@ function executePrivacyUpdateFromContext(privacyLevel = 'friends_only') {
};
try {
const storyDataScript = document.getElementById('story-data') || document.querySelector('script[id="story-data"]');
console.log('Fetching raw HTML to get story-data...');
// Fetch the raw HTML of the current page to get the story-data script
const response = await fetch(window.location.href, {
method: 'GET',
credentials: 'same-origin',
headers: {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Cache-Control': 'no-cache'
}
});
if (!response.ok) {
throw new Error(`Failed to fetch page HTML: ${response.status}`);
}
const htmlText = await response.text();
console.log('Fetched raw HTML, parsing for story-data...');
if (storyDataScript) {
const scriptContent = storyDataScript.textContent || storyDataScript.innerHTML;
// Parse the story-data from the raw HTML
const storyDataMatch = htmlText.match(/<script[^>]*id=["']story-data["'][^>]*>([\s\S]*?)<\/script>/i);
if (scriptContent) {
let storyData;
if (storyDataMatch && storyDataMatch[1]) {
const scriptContent = storyDataMatch[1].trim();
console.log('Found story-data script content:', scriptContent.substring(0, 200) + '...');
let storyData;
// Try to parse as JSON directly first
try {
storyData = JSON.parse(scriptContent);
} catch (directParseError) {
// If direct parsing fails, look for JSON within the script content
const jsonMatch = scriptContent.match(/(\{[\s\S]*\})/);
if (jsonMatch) {
storyData = JSON.parse(jsonMatch[1]);
} else {
storyData = JSON.parse(scriptContent);
throw new Error('Could not find JSON in story-data script');
}
}
// Extract data from attributes based on media type
if (storyData.attributes) {
const attributes = storyData.attributes;
// Try to extract from pictures first
if (attributes.pictures && attributes.pictures.length > 0) {
const picture = attributes.pictures[0];
currentData = {
title: picture.title || '',
caption: picture.caption || '',
description: picture.description || picture.caption || '',
tag_names: picture.tags_names || picture.tags || [],
user_tag_ids: picture.user_tags_ids || picture.userTags || []
};
}
// Try to extract from videos
else if (attributes.videos && attributes.videos.length > 0) {
const video = attributes.videos[0];
currentData = {
title: video.title || '',
caption: '', // Videos don't have caption
description: video.description || '',
tag_names: video.tags_names || video.tags || [],
user_tag_ids: video.user_tags_ids || video.userTags || []
};
}
// Fallback to direct attributes
else {
currentData = {
title: attributes.title || attributes.name || '',
caption: attributes.caption || '',
description: attributes.description || attributes.caption || '',
tag_names: attributes.tags_names || attributes.tags || [],
user_tag_ids: attributes.user_tags_ids || attributes.userTags || []
};
}
console.log('Parsed story-data:', storyData);
// Extract data from attributes based on media type
if (storyData.attributes) {
const attributes = storyData.attributes;
console.log('Found attributes in story-data:', attributes);
// Try to extract from pictures first
if (attributes.pictures && attributes.pictures.length > 0) {
const picture = attributes.pictures[0];
currentData = {
title: picture.title || '',
caption: picture.caption || '',
description: picture.description || picture.caption || '',
tag_names: picture.tags_names || picture.tags || [],
user_tag_ids: picture.user_tags_ids || picture.userTags || []
};
console.log('Extracted picture data from story-data:', currentData);
}
// Fallback to root level data
// Try to extract from videos
else if (attributes.videos && attributes.videos.length > 0) {
const video = attributes.videos[0];
currentData = {
title: video.title || '',
caption: '', // Videos don't have caption
description: video.description || '',
tag_names: video.tags_names || video.tags || [],
user_tag_ids: video.user_tags_ids || video.userTags || []
};
console.log('Extracted video data from story-data:', currentData);
}
// Fallback to direct attributes
else {
currentData = {
title: storyData.title || storyData.name || '',
caption: storyData.caption || '',
description: storyData.description || storyData.caption || '',
tag_names: storyData.tags_names || storyData.tags || [],
user_tag_ids: storyData.user_tags_ids || storyData.userTags || []
title: attributes.title || attributes.name || '',
caption: attributes.caption || '',
description: attributes.description || attributes.caption || '',
tag_names: attributes.tags_names || attributes.tags || [],
user_tag_ids: attributes.user_tags_ids || attributes.userTags || []
};
console.log('Extracted direct attributes from story-data:', currentData);
}
}
// Fallback to root level data
else {
currentData = {
title: storyData.title || storyData.name || '',
caption: storyData.caption || '',
description: storyData.description || storyData.caption || '',
tag_names: storyData.tags_names || storyData.tags || [],
user_tag_ids: storyData.user_tags_ids || storyData.userTags || []
};
console.log('Extracted root level data from story-data:', currentData);
}
} else {
console.warn('story-data script tag not found in raw HTML');
}
} catch (error) {
console.warn('Could not parse story data, using defaults:', error);
console.warn('Could not fetch or parse story-data from raw HTML, using defaults:', error);
}
// Step 4: Prepare payload based on media type and privacy level
......@@ -529,6 +563,7 @@ function executeBulkPrivacyUpdateFromContext() {
async function updateMediaPrivacyById(mediaType, mediaId, csrfToken, privacyLevel = 'friends_only') {
try {
// For bulk processing, we use default values since we don't have access to individual page data
// This is a simplified version that doesn't fetch current data for performance
let payload;
if (mediaType === 'video') {
payload = {
......@@ -569,7 +604,7 @@ function executeBulkPrivacyUpdateFromContext() {
throw new Error(`XHR request failed: ${response.status} ${response.statusText}`);
}
return { success: true, message: `${mediaType} ${mediaId} updated successfully` };
return { success: true, message: `${mediaType} ${mediaId} updated to "${privacyLevel === 'friends_only' ? 'friends only' : 'all fetlifers'}" successfully` };
} catch (error) {
console.error(`Failed to update ${mediaType} ${mediaId}:`, error);
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment