Enhance video download functionality by adding cookie handling and improved request headers. Enable remote module in web preferences and log cookies for debugging. Update renderer to include session management for better cookie access.

This commit is contained in:
Tiger Ren 2025-01-08 00:21:16 +08:00
parent 159653d17e
commit 1c7fc924f3
2 changed files with 70 additions and 26 deletions

93
main.js
View File

@ -21,7 +21,8 @@ function createWindow() {
webPreferences: { webPreferences: {
nodeIntegration: true, nodeIntegration: true,
contextIsolation: false, contextIsolation: false,
webviewTag: true webviewTag: true,
enableRemoteModule: true
} }
}); });
@ -49,6 +50,16 @@ function createWindow() {
console.log('Downloading video as:', filename); console.log('Downloading video as:', filename);
console.log('To folder:', destFolder); console.log('To folder:', destFolder);
console.log("cookies:", session.defaultSession.cookies);
// Print all cookies
session.defaultSession.cookies.get({})
.then((cookies) => {
console.log("cookies:", cookies);
})
.catch((error) => {
console.error('Failed to load cookies:', error);
});
downloadVideo(details.url, path.join(destFolder, filename)); downloadVideo(details.url, path.join(destFolder, filename));
} else { } else {
console.warn('⚠ No destination folder configured'); console.warn('⚠ No destination folder configured');
@ -68,35 +79,67 @@ function createWindow() {
function downloadVideo(url, filepath) { function downloadVideo(url, filepath) {
console.log('Starting video download:', url); console.log('Starting video download:', url);
console.log('Save path:', filepath);
// Get cookies from all relevant domains
Promise.all([
session.defaultSession.cookies.get({domain: '.onedrive.com'}),
session.defaultSession.cookies.get({domain: '.1drv.com'}),
session.defaultSession.cookies.get({domain: '.microsoftpersonalcontent.com'})
])
.then((cookieArrays) => {
// Flatten and combine all cookies
const allCookies = [].concat(...cookieArrays);
console.log('All cookies found:', allCookies);
const dir = path.dirname(filepath); const cookieHeader = allCookies
if (!fs.existsSync(dir)) { .map(cookie => `${cookie.name}=${cookie.value}`)
console.log('Creating directory:', dir); .join('; ');
fs.mkdirSync(dir, { recursive: true });
}
const file = fs.createWriteStream(filepath); const options = {
https.get(url, (response) => { headers: {
console.log('Download started, status:', response.statusCode); 'Cookie': cookieHeader,
console.log('Content type:', response.headers['content-type']); 'Accept': '*/*',
console.log('Content length:', response.headers['content-length']); 'User-Agent': 'Mozilla/5.0',
'Authorization': `Bearer ${allCookies.find(c => c.name === 'AccessToken-OneDrive.ReadWrite')?.value || ''}`,
'Origin': 'https://photos.onedrive.com',
'Referer': 'https://photos.onedrive.com/'
}
};
response.pipe(file); console.log('Request headers:', options.headers);
file.on('finish', () => {
file.close(); // Rest of the download code...
console.log('✓ Video saved successfully:', filepath); const file = fs.createWriteStream(filepath);
mainWindow.webContents.send('video-saved', { https.get(url, options, (response) => {
filename: path.basename(filepath), console.log('Download started, status:', response.statusCode);
path: filepath console.log('Response headers:', response.headers);
if (response.statusCode === 401 || response.statusCode === 403) {
console.error('Authentication failed:', response.statusCode);
mainWindow.webContents.send('video-save-error', 'Authentication failed');
return;
}
response.pipe(file);
file.on('finish', () => {
file.close();
console.log('✓ Video saved successfully:', filepath);
mainWindow.webContents.send('video-saved', {
filename: path.basename(filepath),
path: filepath
});
}); });
}).on('error', (err) => {
console.error('⚠ Download error:', err);
fs.unlink(filepath, () => {
console.log('Cleaned up failed download file');
});
mainWindow.webContents.send('video-save-error', err.message);
}); });
}).on('error', (err) => { })
console.error('⚠ Download error:', err); .catch((error) => {
fs.unlink(filepath, () => { console.error('Failed to load cookies for download:', error);
console.log('Cleaned up failed download file'); mainWindow.webContents.send('video-save-error', 'Failed to load authentication cookies');
});
mainWindow.webContents.send('video-save-error', err.message);
}); });
} }

View File

@ -1,4 +1,4 @@
const { ipcRenderer } = require('electron'); const { ipcRenderer, session } = require('electron');
// Wait for webview to load // Wait for webview to load
const webview = document.getElementById('main-content'); const webview = document.getElementById('main-content');
@ -10,6 +10,7 @@ webview.addEventListener('did-start-loading', () => {
webview.addEventListener('did-finish-load', () => { webview.addEventListener('did-finish-load', () => {
console.log('Webview finished loading'); console.log('Webview finished loading');
// Create and add the configuration button // Create and add the configuration button
console.log('Creating configuration button'); console.log('Creating configuration button');
const button = document.createElement('button'); const button = document.createElement('button');