/**
* Canva Design & Slides Downloader
* Author: Vamz Spectre (VamxAPI)
*
* Features:
* - Auto URL normalizer (handles /view, /edit, /watch, and query params)
* - Extracts design metadata (title, total pages, dimensions)
* - Reverse-engineers direct AWS S3 pre-signed slide image download URLs
* - Extracts full-resolution preview and thumbnail images for every page
*
* Usage:
* curl -sL "https://code.vamxapi.my.id/snippet/raw/canva-downloader" -o canva-downloader.js
* node canva-downloader.js "https://www.canva.com/design/DAGwU2DtUxk/UDIxzR2nbX8gglGx1V5RgQ/view"
*/
const https = require('https');
const http = require('http');
const { URL } = require('url');
class CanvaDownloader {
constructor(options = {}) {
this.timeout = options.timeout || 15000;
this.userAgent = options.userAgent || 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36';
}
/**
* Normalize any Canva design URL into a canonical /view endpoint
*/
normalizeUrl(rawUrl) {
if (!rawUrl || typeof rawUrl !== 'string') {
throw new Error('URL Canva tidak valid.');
}
let clean = rawUrl.trim();
const urlObj = new URL(clean);
// Replace edit or watch action with view
urlObj.pathname = urlObj.pathname.replace(/\/(edit|watch)(\/.*)?$/, '/view');
if (!urlObj.pathname.endsWith('/view')) {
urlObj.pathname = urlObj.pathname.replace(/\/+$/, '') + '/view';
}
// Keep clean pathname
urlObj.search = '';
urlObj.hash = '';
return urlObj.toString();
}
/**
* Perform HTTP GET request following redirects
*/
fetchHtml(targetUrl, maxRedirects = 5) {
return new Promise((resolve, reject) => {
if (maxRedirects <= 0) {
return reject(new Error('Terlalu banyak redirect.'));
}
const client = targetUrl.startsWith('https') ? https : http;
const req = client.get(targetUrl, {
timeout: this.timeout,
headers: {
'User-Agent': this.userAgent,
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8',
'Accept-Language': 'en-US,en;q=0.9,id;q=0.8',
'Sec-Ch-Ua': '"Chromium";v="122", "Not(A:Brand";v="24", "Google Chrome";v="122"',
'Sec-Ch-Ua-Mobile': '?0',
'Sec-Ch-Ua-Platform': '"Windows"',
'Sec-Fetch-Dest': 'document',
'Sec-Fetch-Mode': 'navigate',
'Sec-Fetch-Site': 'none'
}
}, (res) => {
if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
const redirectUrl = new URL(res.headers.location, targetUrl).toString();
return resolve(this.fetchHtml(redirectUrl, maxRedirects - 1));
}
if (res.statusCode !== 200) {
return reject(new Error(`Gagal mengambil halaman Canva (HTTP ${res.statusCode})`));
}
let body = '';
res.on('data', (chunk) => body += chunk);
res.on('end', () => resolve(body));
});
req.on('error', reject);
req.on('timeout', () => {
req.destroy();
reject(new Error('Request timeout saat mengakses Canva.'));
});
});
}
/**
* Extract slides and media from Canva design page
*/
async download(url) {
const canonicalUrl = this.normalizeUrl(url);
const html = await this.fetchHtml(canonicalUrl);
// Extract Title
let title = 'Canva Design';
const ogTitle = html.match(/([^<]+)<\/title>/i);
if (ogTitle && ogTitle[1]) {
title = ogTitle[1].trim();
} else if (docTitle && docTitle[1]) {
title = docTitle[1].replace(/\s*[–-]\s*Canva\s*$/i, '').trim();
}
// Extract og:image preview if present
let coverPreview = null;
const ogImage = html.match(/ existingResolution) {
pagesMap.set(pageIndex, {
page: pageIndex,
width: item.width || null,
height: item.height || null,
key: item.key || null,
download_url: directDownloadUrl
});
}
}
} catch (_) {}
}
const pages = Array.from(pagesMap.values()).sort((a, b) => a.page - b.page);
if (pages.length === 0) {
throw new Error('Tidak ditemukan halaman/slide yang dapat diunduh dari link Canva ini. Pastikan link bersifat publik (View Link).');
}
return {
status: true,
creator: 'Vamz Spectre',
data: {
title,
url: canonicalUrl,
total_pages: pages.length,
cover: coverPreview,
pages
}
};
}
}
// CLI Runner
if (require.main === module) {
const inputUrl = process.argv[2];
if (!inputUrl) {
console.log(JSON.stringify({
status: false,
message: 'Parameter URL Canva wajib disertakan.',
usage: 'node canva-downloader.js "https://www.canva.com/design/DAGwU2DtUxk/UDIxzR2nbX8gglGx1V5RgQ/view"'
}, null, 2));
process.exit(1);
}
const downloader = new CanvaDownloader();
downloader.download(inputUrl)
.then((result) => {
console.log(JSON.stringify(result, null, 2));
})
.catch((err) => {
console.log(JSON.stringify({
status: false,
error: err.message
}, null, 2));
process.exit(1);
});
}
module.exports = CanvaDownloader;