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:
parent
159653d17e
commit
1c7fc924f3
93
main.js
93
main.js
|
|
@ -21,7 +21,8 @@ function createWindow() {
|
|||
webPreferences: {
|
||||
nodeIntegration: true,
|
||||
contextIsolation: false,
|
||||
webviewTag: true
|
||||
webviewTag: true,
|
||||
enableRemoteModule: true
|
||||
}
|
||||
});
|
||||
|
||||
|
|
@ -49,6 +50,16 @@ function createWindow() {
|
|||
|
||||
console.log('Downloading video as:', filename);
|
||||
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));
|
||||
} else {
|
||||
console.warn('⚠ No destination folder configured');
|
||||
|
|
@ -68,35 +79,67 @@ function createWindow() {
|
|||
|
||||
function downloadVideo(url, filepath) {
|
||||
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);
|
||||
if (!fs.existsSync(dir)) {
|
||||
console.log('Creating directory:', dir);
|
||||
fs.mkdirSync(dir, { recursive: true });
|
||||
}
|
||||
const cookieHeader = allCookies
|
||||
.map(cookie => `${cookie.name}=${cookie.value}`)
|
||||
.join('; ');
|
||||
|
||||
const file = fs.createWriteStream(filepath);
|
||||
https.get(url, (response) => {
|
||||
console.log('Download started, status:', response.statusCode);
|
||||
console.log('Content type:', response.headers['content-type']);
|
||||
console.log('Content length:', response.headers['content-length']);
|
||||
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/'
|
||||
}
|
||||
};
|
||||
|
||||
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
|
||||
console.log('Request headers:', options.headers);
|
||||
|
||||
// Rest of the download code...
|
||||
const file = fs.createWriteStream(filepath);
|
||||
https.get(url, options, (response) => {
|
||||
console.log('Download started, status:', response.statusCode);
|
||||
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);
|
||||
fs.unlink(filepath, () => {
|
||||
console.log('Cleaned up failed download file');
|
||||
});
|
||||
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');
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
const { ipcRenderer } = require('electron');
|
||||
const { ipcRenderer, session } = require('electron');
|
||||
|
||||
// Wait for webview to load
|
||||
const webview = document.getElementById('main-content');
|
||||
|
|
@ -10,6 +10,7 @@ webview.addEventListener('did-start-loading', () => {
|
|||
|
||||
webview.addEventListener('did-finish-load', () => {
|
||||
console.log('Webview finished loading');
|
||||
|
||||
// Create and add the configuration button
|
||||
console.log('Creating configuration button');
|
||||
const button = document.createElement('button');
|
||||
|
|
|
|||
Loading…
Reference in New Issue