Commit eed91b63 authored by Stefy Spora's avatar Stefy Spora

Fix executePrivacyUpdateFromContext function definition for proper script injection

- Restructure function to return immediately invoked async function expression (IIFE)
- Fix function scope issues that prevented proper injection into page context
- Ensure all helper functions are properly scoped within the injected function
- Fix bulk processing function with same structure
- Add proper error handling for script execution failures
parent b718d691
...@@ -57,6 +57,10 @@ chrome.runtime.onInstalled.addListener(() => { ...@@ -57,6 +57,10 @@ chrome.runtime.onInstalled.addListener(() => {
chrome.contextMenus.onClicked.addListener(async (info, tab) => { chrome.contextMenus.onClicked.addListener(async (info, tab) => {
if (info.menuItemId === 'privaxy') { if (info.menuItemId === 'privaxy') {
try { try {
console.log('Context menu: privaxy clicked');
console.log('Tab URL:', tab.url);
console.log('Tab ID:', tab.id);
// Execute the privacy update function with default privacy level // Execute the privacy update function with default privacy level
const results = await chrome.scripting.executeScript({ const results = await chrome.scripting.executeScript({
target: { tabId: tab.id }, target: { tabId: tab.id },
...@@ -64,18 +68,26 @@ chrome.contextMenus.onClicked.addListener(async (info, tab) => { ...@@ -64,18 +68,26 @@ chrome.contextMenus.onClicked.addListener(async (info, tab) => {
args: ['friends_only'] args: ['friends_only']
}); });
console.log('Script execution results:', results);
const result = results && results[0] && results[0].result ? results[0].result : null; const result = results && results[0] && results[0].result ? results[0].result : null;
console.log('Extracted result:', result);
if (!result) { if (!result) {
console.error('No result returned from script execution');
console.log('Results object:', JSON.stringify(results, null, 2));
chrome.notifications.create({ chrome.notifications.create({
type: 'basic', type: 'basic',
iconUrl: 'icon48.png', iconUrl: 'icon48.png',
title: 'FetLife Privacy Helper', title: 'FetLife Privacy Helper',
message: 'Error: Could not execute privacy update. Please try again.' message: 'Error: Script execution failed. Check console for details.'
}); });
return; return;
} }
console.log('Result success:', result.success);
console.log('Result message:', result.message);
// Show notification with result // Show notification with result
chrome.notifications.create({ chrome.notifications.create({
type: 'basic', type: 'basic',
...@@ -85,7 +97,12 @@ chrome.contextMenus.onClicked.addListener(async (info, tab) => { ...@@ -85,7 +97,12 @@ chrome.contextMenus.onClicked.addListener(async (info, tab) => {
}); });
} catch (error) { } catch (error) {
console.error('Context menu execution error:', error); console.error('Context menu privaxy execution error:', error);
console.error('Error details:', {
message: error.message,
stack: error.stack,
name: error.name
});
chrome.notifications.create({ chrome.notifications.create({
type: 'basic', type: 'basic',
iconUrl: 'icon48.png', iconUrl: 'icon48.png',
...@@ -120,12 +137,20 @@ chrome.contextMenus.onClicked.addListener(async (info, tab) => { ...@@ -120,12 +137,20 @@ chrome.contextMenus.onClicked.addListener(async (info, tab) => {
} }
} else if (info.menuItemId === 'set-friends-only') { } else if (info.menuItemId === 'set-friends-only') {
try { try {
console.log('Context menu: set-friends-only clicked');
console.log('Tab URL:', tab.url);
console.log('Tab ID:', tab.id);
// Check if we're on a video or picture page // Check if we're on a video or picture page
const url = tab.url; const url = tab.url;
const isVideoPage = /\/(?:[^\/]+\/)?videos\/\d+/.test(url); const isVideoPage = /\/(?:[^\/]+\/)?videos\/\d+/.test(url);
const isPicturePage = /\/(?:[^\/]+\/)?pictures\/\d+/.test(url); const isPicturePage = /\/(?:[^\/]+\/)?pictures\/\d+/.test(url);
console.log('Is video page:', isVideoPage);
console.log('Is picture page:', isPicturePage);
if (!isVideoPage && !isPicturePage) { if (!isVideoPage && !isPicturePage) {
console.log('Not on video or picture page, showing error');
chrome.notifications.create({ chrome.notifications.create({
type: 'basic', type: 'basic',
iconUrl: 'icon48.png', iconUrl: 'icon48.png',
...@@ -135,6 +160,8 @@ chrome.contextMenus.onClicked.addListener(async (info, tab) => { ...@@ -135,6 +160,8 @@ chrome.contextMenus.onClicked.addListener(async (info, tab) => {
return; return;
} }
console.log('Executing script for friends_only...');
// Execute privacy update with friends_only setting // Execute privacy update with friends_only setting
const results = await chrome.scripting.executeScript({ const results = await chrome.scripting.executeScript({
target: { tabId: tab.id }, target: { tabId: tab.id },
...@@ -142,18 +169,26 @@ chrome.contextMenus.onClicked.addListener(async (info, tab) => { ...@@ -142,18 +169,26 @@ chrome.contextMenus.onClicked.addListener(async (info, tab) => {
args: ['friends_only'] args: ['friends_only']
}); });
console.log('Script execution results:', results);
const result = results && results[0] && results[0].result ? results[0].result : null; const result = results && results[0] && results[0].result ? results[0].result : null;
console.log('Extracted result:', result);
if (!result) { if (!result) {
console.error('No result returned from script execution');
console.log('Results object:', JSON.stringify(results, null, 2));
chrome.notifications.create({ chrome.notifications.create({
type: 'basic', type: 'basic',
iconUrl: 'icon48.png', iconUrl: 'icon48.png',
title: 'FetLife Privacy Helper', title: 'FetLife Privacy Helper',
message: 'Error: Could not execute privacy update. Please try again.' message: 'Error: Script execution failed. Check console for details.'
}); });
return; return;
} }
console.log('Result success:', result.success);
console.log('Result message:', result.message);
// Show notification with result // Show notification with result
chrome.notifications.create({ chrome.notifications.create({
type: 'basic', type: 'basic',
...@@ -164,6 +199,11 @@ chrome.contextMenus.onClicked.addListener(async (info, tab) => { ...@@ -164,6 +199,11 @@ chrome.contextMenus.onClicked.addListener(async (info, tab) => {
} catch (error) { } catch (error) {
console.error('Context menu friends-only execution error:', error); console.error('Context menu friends-only execution error:', error);
console.error('Error details:', {
message: error.message,
stack: error.stack,
name: error.name
});
chrome.notifications.create({ chrome.notifications.create({
type: 'basic', type: 'basic',
iconUrl: 'icon48.png', iconUrl: 'icon48.png',
...@@ -173,12 +213,20 @@ chrome.contextMenus.onClicked.addListener(async (info, tab) => { ...@@ -173,12 +213,20 @@ chrome.contextMenus.onClicked.addListener(async (info, tab) => {
} }
} else if (info.menuItemId === 'set-all-fetlifers') { } else if (info.menuItemId === 'set-all-fetlifers') {
try { try {
console.log('Context menu: set-all-fetlifers clicked');
console.log('Tab URL:', tab.url);
console.log('Tab ID:', tab.id);
// Check if we're on a video or picture page // Check if we're on a video or picture page
const url = tab.url; const url = tab.url;
const isVideoPage = /\/(?:[^\/]+\/)?videos\/\d+/.test(url); const isVideoPage = /\/(?:[^\/]+\/)?videos\/\d+/.test(url);
const isPicturePage = /\/(?:[^\/]+\/)?pictures\/\d+/.test(url); const isPicturePage = /\/(?:[^\/]+\/)?pictures\/\d+/.test(url);
console.log('Is video page:', isVideoPage);
console.log('Is picture page:', isPicturePage);
if (!isVideoPage && !isPicturePage) { if (!isVideoPage && !isPicturePage) {
console.log('Not on video or picture page, showing error');
chrome.notifications.create({ chrome.notifications.create({
type: 'basic', type: 'basic',
iconUrl: 'icon48.png', iconUrl: 'icon48.png',
...@@ -188,6 +236,8 @@ chrome.contextMenus.onClicked.addListener(async (info, tab) => { ...@@ -188,6 +236,8 @@ chrome.contextMenus.onClicked.addListener(async (info, tab) => {
return; return;
} }
console.log('Executing script for all_fetlifers...');
// Execute privacy update with all_fetlifers setting // Execute privacy update with all_fetlifers setting
const results = await chrome.scripting.executeScript({ const results = await chrome.scripting.executeScript({
target: { tabId: tab.id }, target: { tabId: tab.id },
...@@ -195,18 +245,26 @@ chrome.contextMenus.onClicked.addListener(async (info, tab) => { ...@@ -195,18 +245,26 @@ chrome.contextMenus.onClicked.addListener(async (info, tab) => {
args: ['all_fetlifers'] args: ['all_fetlifers']
}); });
console.log('Script execution results:', results);
const result = results && results[0] && results[0].result ? results[0].result : null; const result = results && results[0] && results[0].result ? results[0].result : null;
console.log('Extracted result:', result);
if (!result) { if (!result) {
console.error('No result returned from script execution');
console.log('Results object:', JSON.stringify(results, null, 2));
chrome.notifications.create({ chrome.notifications.create({
type: 'basic', type: 'basic',
iconUrl: 'icon48.png', iconUrl: 'icon48.png',
title: 'FetLife Privacy Helper', title: 'FetLife Privacy Helper',
message: 'Error: Could not execute privacy update. Please try again.' message: 'Error: Script execution failed. Check console for details.'
}); });
return; return;
} }
console.log('Result success:', result.success);
console.log('Result message:', result.message);
// Show notification with result // Show notification with result
chrome.notifications.create({ chrome.notifications.create({
type: 'basic', type: 'basic',
...@@ -217,6 +275,11 @@ chrome.contextMenus.onClicked.addListener(async (info, tab) => { ...@@ -217,6 +275,11 @@ chrome.contextMenus.onClicked.addListener(async (info, tab) => {
} catch (error) { } catch (error) {
console.error('Context menu all-fetlifers execution error:', error); console.error('Context menu all-fetlifers execution error:', error);
console.error('Error details:', {
message: error.message,
stack: error.stack,
name: error.name
});
chrome.notifications.create({ chrome.notifications.create({
type: 'basic', type: 'basic',
iconUrl: 'icon48.png', iconUrl: 'icon48.png',
...@@ -228,7 +291,12 @@ chrome.contextMenus.onClicked.addListener(async (info, tab) => { ...@@ -228,7 +291,12 @@ chrome.contextMenus.onClicked.addListener(async (info, tab) => {
}); });
// Function to be injected for context menu execution - XHR-based approach // Function to be injected for context menu execution - XHR-based approach
// This function must be at the top level to be properly injected
function executePrivacyUpdateFromContext(privacyLevel = 'friends_only') { function executePrivacyUpdateFromContext(privacyLevel = 'friends_only') {
return (async function() {
try {
console.log('Starting XHR-based privacy update from context menu...');
// Function to extract media ID and type from URL // Function to extract media ID and type from URL
function getMediaInfo() { function getMediaInfo() {
const url = window.location.href; const url = window.location.href;
...@@ -263,11 +331,6 @@ function executePrivacyUpdateFromContext(privacyLevel = 'friends_only') { ...@@ -263,11 +331,6 @@ function executePrivacyUpdateFromContext(privacyLevel = 'friends_only') {
return null; return null;
} }
// Main XHR-based privacy update
async function updatePrivacy() {
try {
console.log('Starting XHR-based privacy update from context menu...');
// Step 1: Detect media type and ID from URL // Step 1: Detect media type and ID from URL
const mediaInfo = getMediaInfo(); const mediaInfo = getMediaInfo();
if (!mediaInfo) { if (!mediaInfo) {
...@@ -338,13 +401,13 @@ function executePrivacyUpdateFromContext(privacyLevel = 'friends_only') { ...@@ -338,13 +401,13 @@ function executePrivacyUpdateFromContext(privacyLevel = 'friends_only') {
console.error('XHR privacy update failed:', error); console.error('XHR privacy update failed:', error);
return { success: false, message: error.message }; return { success: false, message: error.message };
} }
} })();
return updatePrivacy();
} }
// Function for bulk privacy update from context menu - XHR-based approach // Function for bulk privacy update from context menu - XHR-based approach
function executeBulkPrivacyUpdateFromContext() { function executeBulkPrivacyUpdateFromContext() {
return (async function() {
try {
// Helper functions // Helper functions
function getCSRFToken() { function getCSRFToken() {
const metaTag = document.querySelector('meta[name="csrf-token"]'); const metaTag = document.querySelector('meta[name="csrf-token"]');
...@@ -455,7 +518,6 @@ function executeBulkPrivacyUpdateFromContext() { ...@@ -455,7 +518,6 @@ function executeBulkPrivacyUpdateFromContext() {
} }
// Main bulk processing function // Main bulk processing function
async function runBulkUpdate() {
let pageCount = 0; let pageCount = 0;
let successCount = 0; let successCount = 0;
let errorCount = 0; let errorCount = 0;
...@@ -476,8 +538,8 @@ function executeBulkPrivacyUpdateFromContext() { ...@@ -476,8 +538,8 @@ function executeBulkPrivacyUpdateFromContext() {
if (mediaItems.length === 0) { if (mediaItems.length === 0) {
// If no media items found, try single page update // If no media items found, try single page update
const singleResult = await executePrivacyUpdateFromContext(); const singleResult = await executePrivacyUpdateFromContext('friends_only');
if (singleResult.success) { if (singleResult && singleResult.success) {
successCount++; successCount++;
} else { } else {
errorCount++; errorCount++;
...@@ -545,9 +607,11 @@ function executeBulkPrivacyUpdateFromContext() { ...@@ -545,9 +607,11 @@ function executeBulkPrivacyUpdateFromContext() {
console.error('Bulk processing failed:', error); console.error('Bulk processing failed:', error);
return { pageCount: 0, successCount: 0, errorCount: 1 }; return { pageCount: 0, successCount: 0, errorCount: 1 };
} }
} catch (error) {
console.error('Bulk update function failed:', error);
return { pageCount: 0, successCount: 0, errorCount: 1 };
} }
})();
return runBulkUpdate();
} }
// Handle messages from popup and notifications // Handle messages from popup and notifications
......
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