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