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

61
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,19 +79,46 @@ 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);
const dir = path.dirname(filepath); // Get cookies from all relevant domains
if (!fs.existsSync(dir)) { Promise.all([
console.log('Creating directory:', dir); session.defaultSession.cookies.get({domain: '.onedrive.com'}),
fs.mkdirSync(dir, { recursive: true }); 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 cookieHeader = allCookies
.map(cookie => `${cookie.name}=${cookie.value}`)
.join('; ');
const options = {
headers: {
'Cookie': cookieHeader,
'Accept': '*/*',
'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/'
} }
};
console.log('Request headers:', options.headers);
// Rest of the download code...
const file = fs.createWriteStream(filepath); const file = fs.createWriteStream(filepath);
https.get(url, (response) => { https.get(url, options, (response) => {
console.log('Download started, status:', response.statusCode); console.log('Download started, status:', response.statusCode);
console.log('Content type:', response.headers['content-type']); console.log('Response headers:', response.headers);
console.log('Content length:', response.headers['content-length']);
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); response.pipe(file);
file.on('finish', () => { file.on('finish', () => {
@ -98,6 +136,11 @@ function downloadVideo(url, filepath) {
}); });
mainWindow.webContents.send('video-save-error', err.message); mainWindow.webContents.send('video-save-error', err.message);
}); });
})
.catch((error) => {
console.error('Failed to load cookies for download:', error);
mainWindow.webContents.send('video-save-error', 'Failed to load authentication cookies');
});
} }
app.whenReady().then(createWindow); app.whenReady().then(createWindow);

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');