-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-html2canvas-refinitiv.html
More file actions
603 lines (487 loc) · 24.8 KB
/
test-html2canvas-refinitiv.html
File metadata and controls
603 lines (487 loc) · 24.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HTML2Canvas Refinitiv Test</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 1200px;
margin: 0 auto;
padding: 20px;
}
.test-container {
border: 2px solid #ddd;
margin: 20px 0;
padding: 20px;
border-radius: 8px;
}
.status {
padding: 10px;
margin: 10px 0;
border-radius: 4px;
font-weight: bold;
}
.status.success { background: #d4edda; color: #155724; border: 1px solid #c3e6cb; }
.status.error { background: #f8d7da; color: #721c24; border: 1px solid #f5c6cb; }
.status.warning { background: #fff3cd; color: #856404; border: 1px solid #ffeaa7; }
.status.info { background: #d1ecf1; color: #0c5460; border: 1px solid #bee5eb; }
button {
background: #007bff;
color: white;
border: none;
padding: 10px 20px;
margin: 5px;
border-radius: 4px;
cursor: pointer;
font-size: 14px;
}
button:hover { background: #0056b3; }
button:disabled { background: #6c757d; cursor: not-allowed; }
.image-container {
border: 1px solid #ccc;
padding: 10px;
margin: 10px 0;
text-align: center;
min-height: 200px;
background: #f8f9fa;
}
.image-container img {
max-width: 100%;
height: auto;
border: 1px solid #ddd;
}
.log-container {
background: #f8f9fa;
border: 1px solid #ddd;
padding: 15px;
margin: 10px 0;
border-radius: 4px;
font-family: 'Courier New', monospace;
font-size: 12px;
max-height: 300px;
overflow-y: auto;
}
.progress-bar {
width: 100%;
height: 20px;
background: #e9ecef;
border-radius: 10px;
overflow: hidden;
margin: 10px 0;
}
.progress-fill {
height: 100%;
background: #28a745;
width: 0%;
transition: width 0.3s ease;
}
</style>
</head>
<body>
<h1>HTML2Canvas Refinitiv Test</h1>
<p>Testing URL: <strong>https://refini.tv/4gm0WdA</strong></p>
<div class="test-container">
<h2>Step 1: Image Loading Test</h2>
<button onclick="loadImage()">Load Image</button>
<button onclick="clearImage()">Clear Image</button>
<div id="image-status" class="status info">Click "Load Image" to start</div>
<div class="image-container" id="image-container">
<p>Image will appear here</p>
</div>
</div>
<div class="test-container">
<h2>Step 2: HTML2Canvas Tests</h2>
<button onclick="testBasicCapture()" id="basicCaptureBtn">Basic Capture</button>
<button onclick="testCaptureWithOptions()" id="optionsCaptureBtn">Capture with Options</button>
<button onclick="testCaptureWithTimeout()" id="timeoutCaptureBtn">Capture with Timeout</button>
<div id="capture-status" class="status info">Load image first, then test capture methods</div>
<div class="progress-bar" id="progress-bar" style="display: none;">
<div class="progress-fill" id="progress-fill"></div>
</div>
</div>
<div class="test-container">
<h2>Step 3: PDF Generation Test</h2>
<button onclick="generatePDF()" id="pdfBtn">Generate PDF</button>
<div id="pdf-status" class="status info">Capture image successfully first, then generate PDF</div>
</div>
<div class="test-container">
<h2>Debug Log</h2>
<button onclick="clearLog()">Clear Log</button>
<div id="log-container" class="log-container"></div>
</div>
<!-- Load required libraries -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.4.1/html2canvas.min.js"
onerror="this.onerror=null; this.src='https://cdn.jsdelivr.net/npm/html2canvas@1.4.1/dist/html2canvas.min.js'"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js"></script>
<script>
const REFINITIV_URL = 'https://refini.tv/4gm0WdA';
let currentImage = null;
let capturedCanvas = null;
let capturedDataURL = null;
// Logging function
function log(message, type = 'info') {
const timestamp = new Date().toLocaleTimeString();
const logContainer = document.getElementById('log-container');
const logEntry = document.createElement('div');
logEntry.style.color = type === 'error' ? 'red' : type === 'success' ? 'green' : type === 'warning' ? 'orange' : 'black';
logEntry.textContent = `[${timestamp}] ${message}`;
logContainer.appendChild(logEntry);
logContainer.scrollTop = logContainer.scrollHeight;
console.log(`[${type.toUpperCase()}] ${message}`);
}
function clearLog() {
document.getElementById('log-container').innerHTML = '';
}
function updateStatus(elementId, message, type) {
const element = document.getElementById(elementId);
element.textContent = message;
element.className = `status ${type}`;
}
function updateProgress(percent) {
const progressBar = document.getElementById('progress-bar');
const progressFill = document.getElementById('progress-fill');
if (percent > 0) {
progressBar.style.display = 'block';
progressFill.style.width = percent + '%';
} else {
progressBar.style.display = 'none';
}
}
// Step 1: Load Image
function loadImage() {
log('Starting image load test...');
updateStatus('image-status', 'Loading image...', 'info');
const container = document.getElementById('image-container');
container.innerHTML = '<p>Loading image...</p>';
// Try multiple loading strategies
tryLoadImageStrategy1();
}
// Strategy 1: Load without crossOrigin (most likely to work)
function tryLoadImageStrategy1() {
log('Strategy 1: Loading image without crossOrigin...');
const img = new Image();
const startTime = Date.now();
img.onload = function() {
const loadTime = Date.now() - startTime;
log(`✅ Strategy 1 successful: Image loaded without crossOrigin in ${loadTime}ms`, 'success');
log(`Image dimensions: ${img.naturalWidth}x${img.naturalHeight}`, 'info');
updateStatus('image-status', `Image loaded (${img.naturalWidth}x${img.naturalHeight}) in ${loadTime}ms - No CORS`, 'success');
const container = document.getElementById('image-container');
container.innerHTML = '';
container.appendChild(img);
currentImage = img;
// Enable capture buttons
document.getElementById('basicCaptureBtn').disabled = false;
document.getElementById('optionsCaptureBtn').disabled = false;
document.getElementById('timeoutCaptureBtn').disabled = false;
log('⚠️ Note: Image loaded without CORS - html2canvas may have limitations', 'warning');
};
img.onerror = function() {
const loadTime = Date.now() - startTime;
log(`❌ Strategy 1 failed after ${loadTime}ms`, 'error');
log('Trying Strategy 2: With crossOrigin=anonymous...', 'info');
tryLoadImageStrategy2();
};
img.src = REFINITIV_URL;
}
// Strategy 2: Load with crossOrigin='anonymous'
function tryLoadImageStrategy2() {
log('Strategy 2: Loading image with crossOrigin=anonymous...');
const img = new Image();
img.crossOrigin = 'anonymous';
const startTime = Date.now();
img.onload = function() {
const loadTime = Date.now() - startTime;
log(`✅ Strategy 2 successful: Image loaded with CORS in ${loadTime}ms`, 'success');
log(`Image dimensions: ${img.naturalWidth}x${img.naturalHeight}`, 'info');
updateStatus('image-status', `Image loaded (${img.naturalWidth}x${img.naturalHeight}) in ${loadTime}ms - With CORS`, 'success');
const container = document.getElementById('image-container');
container.innerHTML = '';
container.appendChild(img);
currentImage = img;
// Enable capture buttons
document.getElementById('basicCaptureBtn').disabled = false;
document.getElementById('optionsCaptureBtn').disabled = false;
document.getElementById('timeoutCaptureBtn').disabled = false;
log('✅ CORS enabled - html2canvas should work fully', 'success');
};
img.onerror = function() {
const loadTime = Date.now() - startTime;
log(`❌ Strategy 2 failed after ${loadTime}ms`, 'error');
log('Trying Strategy 3: With crossOrigin=use-credentials...', 'info');
tryLoadImageStrategy3();
};
img.src = REFINITIV_URL;
}
// Strategy 3: Load with crossOrigin='use-credentials'
function tryLoadImageStrategy3() {
log('Strategy 3: Loading image with crossOrigin=use-credentials...');
const img = new Image();
img.crossOrigin = 'use-credentials';
const startTime = Date.now();
img.onload = function() {
const loadTime = Date.now() - startTime;
log(`✅ Strategy 3 successful: Image loaded with credentials in ${loadTime}ms`, 'success');
log(`Image dimensions: ${img.naturalWidth}x${img.naturalHeight}`, 'info');
updateStatus('image-status', `Image loaded (${img.naturalWidth}x${img.naturalHeight}) in ${loadTime}ms - With Credentials`, 'success');
const container = document.getElementById('image-container');
container.innerHTML = '';
container.appendChild(img);
currentImage = img;
// Enable capture buttons
document.getElementById('basicCaptureBtn').disabled = false;
document.getElementById('optionsCaptureBtn').disabled = false;
document.getElementById('timeoutCaptureBtn').disabled = false;
log('✅ Credentials enabled - html2canvas should work fully', 'success');
};
img.onerror = function() {
const loadTime = Date.now() - startTime;
log(`❌ Strategy 3 failed after ${loadTime}ms`, 'error');
log('Trying Strategy 4: Via fetch API...', 'info');
tryLoadImageStrategy4();
};
img.src = REFINITIV_URL;
}
// Strategy 4: Fetch as blob and create object URL
async function tryLoadImageStrategy4() {
log('Strategy 4: Fetching image as blob...');
try {
const startTime = Date.now();
const response = await fetch(REFINITIV_URL, {
mode: 'no-cors' // This allows the request but limits what we can do with response
});
log(`Fetch response status: ${response.status}, type: ${response.type}`);
const blob = await response.blob();
const objectURL = URL.createObjectURL(blob);
const img = new Image();
img.onload = function() {
const loadTime = Date.now() - startTime;
log(`✅ Strategy 4 successful: Image loaded via fetch in ${loadTime}ms`, 'success');
log(`Image dimensions: ${img.naturalWidth}x${img.naturalHeight}`, 'info');
updateStatus('image-status', `Image loaded (${img.naturalWidth}x${img.naturalHeight}) in ${loadTime}ms - Via Fetch`, 'success');
const container = document.getElementById('image-container');
container.innerHTML = '';
container.appendChild(img);
currentImage = img;
// Enable capture buttons
document.getElementById('basicCaptureBtn').disabled = false;
document.getElementById('optionsCaptureBtn').disabled = false;
document.getElementById('timeoutCaptureBtn').disabled = false;
log('✅ Blob-based image - html2canvas should work', 'success');
};
img.onerror = function() {
const loadTime = Date.now() - startTime;
log(`❌ Strategy 4 failed after ${loadTime}ms`, 'error');
allStrategiesFailed();
};
img.src = objectURL;
} catch (error) {
log(`❌ Strategy 4 fetch failed: ${error.message}`, 'error');
allStrategiesFailed();
}
}
function allStrategiesFailed() {
log('❌ All image loading strategies failed', 'error');
updateStatus('image-status', 'All image loading strategies failed - CORS blocked', 'error');
const container = document.getElementById('image-container');
container.innerHTML = `
<p style="color: red;">❌ Failed to load image</p>
<p style="font-size: 12px;">This is likely due to CORS restrictions.</p>
<p style="font-size: 12px;">The Refinitiv server doesn't allow cross-origin access.</p>
<p style="font-size: 12px;">Check the console log for detailed error information.</p>
`;
log('💡 Suggestion: The image may display in browser but cannot be captured by html2canvas due to CORS', 'warning');
log('💡 Alternative: Consider using a CORS proxy or server-side solution', 'warning');
}
function clearImage() {
log('Clearing image...');
document.getElementById('image-container').innerHTML = '<p>Image will appear here</p>';
updateStatus('image-status', 'Image cleared', 'info');
currentImage = null;
capturedCanvas = null;
capturedDataURL = null;
// Disable capture buttons
document.getElementById('basicCaptureBtn').disabled = true;
document.getElementById('optionsCaptureBtn').disabled = true;
document.getElementById('timeoutCaptureBtn').disabled = true;
document.getElementById('pdfBtn').disabled = true;
}
// Step 2: HTML2Canvas Tests
async function testBasicCapture() {
if (!currentImage) {
log('❌ No image loaded', 'error');
return;
}
log('Testing basic html2canvas capture...');
updateStatus('capture-status', 'Capturing with basic settings...', 'info');
updateProgress(10);
try {
if (typeof html2canvas === 'undefined') {
throw new Error('html2canvas library not loaded');
}
log('Calling html2canvas with basic options...');
const startTime = Date.now();
const canvas = await html2canvas(currentImage);
const captureTime = Date.now() - startTime;
log(`✅ Basic capture completed in ${captureTime}ms`, 'success');
log(`Canvas dimensions: ${canvas.width}x${canvas.height}`, 'info');
capturedCanvas = canvas;
capturedDataURL = canvas.toDataURL('image/png');
updateStatus('capture-status', `Basic capture successful (${captureTime}ms)`, 'success');
updateProgress(100);
document.getElementById('pdfBtn').disabled = false;
setTimeout(() => updateProgress(0), 2000);
} catch (error) {
log(`❌ Basic capture failed: ${error.message}`, 'error');
updateStatus('capture-status', `Basic capture failed: ${error.message}`, 'error');
updateProgress(0);
}
}
async function testCaptureWithOptions() {
if (!currentImage) {
log('❌ No image loaded', 'error');
return;
}
log('Testing html2canvas with advanced options...');
updateStatus('capture-status', 'Capturing with advanced options...', 'info');
updateProgress(10);
try {
if (typeof html2canvas === 'undefined') {
throw new Error('html2canvas library not loaded');
}
log('Calling html2canvas with advanced options...');
const startTime = Date.now();
const canvas = await html2canvas(currentImage, {
allowTaint: true,
useCORS: true,
scale: 1,
backgroundColor: null,
logging: true,
width: currentImage.offsetWidth,
height: currentImage.offsetHeight,
timeout: 15000
});
const captureTime = Date.now() - startTime;
log(`✅ Advanced capture completed in ${captureTime}ms`, 'success');
log(`Canvas dimensions: ${canvas.width}x${canvas.height}`, 'info');
capturedCanvas = canvas;
capturedDataURL = canvas.toDataURL('image/png');
updateStatus('capture-status', `Advanced capture successful (${captureTime}ms)`, 'success');
updateProgress(100);
document.getElementById('pdfBtn').disabled = false;
setTimeout(() => updateProgress(0), 2000);
} catch (error) {
log(`❌ Advanced capture failed: ${error.message}`, 'error');
updateStatus('capture-status', `Advanced capture failed: ${error.message}`, 'error');
updateProgress(0);
}
}
async function testCaptureWithTimeout() {
if (!currentImage) {
log('❌ No image loaded', 'error');
return;
}
log('Testing html2canvas with timeout protection...');
updateStatus('capture-status', 'Capturing with timeout protection...', 'info');
updateProgress(10);
try {
if (typeof html2canvas === 'undefined') {
throw new Error('html2canvas library not loaded');
}
log('Calling html2canvas with timeout wrapper...');
const startTime = Date.now();
// Create timeout promise
const timeoutPromise = new Promise((_, reject) => {
setTimeout(() => {
reject(new Error('html2canvas timeout after 10 seconds'));
}, 10000);
});
// Create capture promise
const capturePromise = html2canvas(currentImage, {
allowTaint: true,
useCORS: true,
scale: 1,
backgroundColor: null,
logging: true,
width: currentImage.offsetWidth,
height: currentImage.offsetHeight
});
// Race between capture and timeout
const canvas = await Promise.race([capturePromise, timeoutPromise]);
const captureTime = Date.now() - startTime;
log(`✅ Timeout-protected capture completed in ${captureTime}ms`, 'success');
log(`Canvas dimensions: ${canvas.width}x${canvas.height}`, 'info');
capturedCanvas = canvas;
capturedDataURL = canvas.toDataURL('image/png');
updateStatus('capture-status', `Timeout-protected capture successful (${captureTime}ms)`, 'success');
updateProgress(100);
document.getElementById('pdfBtn').disabled = false;
setTimeout(() => updateProgress(0), 2000);
} catch (error) {
log(`❌ Timeout-protected capture failed: ${error.message}`, 'error');
updateStatus('capture-status', `Timeout-protected capture failed: ${error.message}`, 'error');
updateProgress(0);
}
}
// Step 3: PDF Generation
function generatePDF() {
if (!capturedDataURL) {
log('❌ No captured image available', 'error');
return;
}
log('Generating PDF...');
updateStatus('pdf-status', 'Generating PDF...', 'info');
try {
if (typeof window.jspdf === 'undefined') {
throw new Error('jsPDF library not loaded');
}
const { jsPDF } = window.jspdf;
const pdf = new jsPDF();
// Add title
pdf.setFontSize(16);
pdf.text('Refinitiv Chart Test', 20, 20);
// Add image info
pdf.setFontSize(10);
pdf.text('URL: https://refini.tv/4gm0WdA', 20, 35);
pdf.text(`Generated: ${new Date().toLocaleString()}`, 20, 45);
// Add the captured image
log('Adding captured image to PDF...');
pdf.addImage(capturedDataURL, 'PNG', 20, 55, 170, 120);
// Save the PDF
pdf.save('refinitiv-chart-test.pdf');
log('✅ PDF generated successfully', 'success');
updateStatus('pdf-status', 'PDF generated and downloaded successfully', 'success');
} catch (error) {
log(`❌ PDF generation failed: ${error.message}`, 'error');
updateStatus('pdf-status', `PDF generation failed: ${error.message}`, 'error');
}
}
// Initialize
window.onload = function() {
log('Test page loaded');
log('Checking library availability...');
// Check html2canvas
setTimeout(() => {
if (typeof html2canvas !== 'undefined') {
log('✅ html2canvas library loaded', 'success');
} else {
log('❌ html2canvas library not loaded', 'error');
}
// Check jsPDF
if (typeof window.jspdf !== 'undefined') {
log('✅ jsPDF library loaded', 'success');
} else {
log('❌ jsPDF library not loaded', 'error');
}
}, 1000);
// Disable capture and PDF buttons initially
document.getElementById('basicCaptureBtn').disabled = true;
document.getElementById('optionsCaptureBtn').disabled = true;
document.getElementById('timeoutCaptureBtn').disabled = true;
document.getElementById('pdfBtn').disabled = true;
};
</script>
</body>
</html>