-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
132 lines (112 loc) · 3.95 KB
/
main.js
File metadata and controls
132 lines (112 loc) · 3.95 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
import * as pdfjsLib from 'https://cdn.jsdelivr.net/npm/pdfjs-dist@5.5.207/legacy/build/pdf.mjs';
const workerUrl = 'https://cdn.jsdelivr.net/npm/pdfjs-dist@5.5.207/legacy/build/pdf.worker.mjs';
pdfjsLib.GlobalWorkerOptions.workerSrc = workerUrl;
const samplePdfUrl = './compressed.tracemonkey-pldi-09.pdf';
const app = document.querySelector('#app');
app.innerHTML = `
<main>
<h1>Safari repro: pdfjs legacy build getTextContent()</h1>
<p>
Import <code>pdfjs-dist/legacy/build/pdf.mjs</code> from CDN, and run <code>PDFPageProxy.getTextContent()</code> on the first page.
</p>
<div class="actions">
<button id="runButton">Run repro</button>
<label>
<input id="autoRunCheckbox" type="checkbox" checked />
auto run on load
</label>
</div>
<dl class="meta">
<div>
<dt>PDF</dt>
<dd><a href="${samplePdfUrl}" target="_blank" rel="noreferrer">compressed.tracemonkey-pldi-09.pdf</a></dd>
</div>
<div>
<dt>pdf.mjs</dt>
<dd><a href="https://cdn.jsdelivr.net/npm/pdfjs-dist@5.5.207/legacy/build/pdf.mjs" target="_blank" rel="noreferrer">pdfjs-dist@5.5.207/legacy/build/pdf.mjs</a></dd>
</div>
<div>
<dt>workerSrc</dt>
<dd><a href="${workerUrl}" target="_blank" rel="noreferrer">${workerUrl}</a></dd>
</div>
<div>
<dt>User Agent</dt>
<dd id="userAgent"></dd>
</div>
<div>
<dt>Status</dt>
<dd id="status">Idle</dd>
</div>
</dl>
<h2>Console-style log</h2>
<pre id="log"></pre>
</main>
`;
const logEl = document.querySelector('#log');
const statusEl = document.querySelector('#status');
const userAgentEl = document.querySelector('#userAgent');
const runButton = document.querySelector('#runButton');
const autoRunCheckbox = document.querySelector('#autoRunCheckbox');
userAgentEl.textContent = navigator.userAgent;
function serializeError(error) {
if (!(error instanceof Error)) return String(error);
return `${error.name}: ${error.message}\n${error.stack ?? ''}`;
}
function log(...args) {
const line = args
.map((arg) => {
if (arg instanceof Error) return serializeError(arg);
if (typeof arg === 'object' && arg !== null) {
try {
return JSON.stringify(arg, null, 2);
} catch {
return String(arg);
}
}
return String(arg);
})
.join(' ');
console.log(...args);
logEl.textContent += `${line}\n`;
logEl.scrollTop = logEl.scrollHeight;
}
async function runRepro() {
runButton.disabled = true;
logEl.textContent = '';
statusEl.textContent = 'Loading PDF...';
try {
log('Starting repro...');
log('Loading pdf.mjs from', 'https://cdn.jsdelivr.net/npm/pdfjs-dist@5.5.207/legacy/build/pdf.mjs');
log('workerSrc =', workerUrl);
log('Loading PDF from', samplePdfUrl);
const loadingTask = pdfjsLib.getDocument(samplePdfUrl);
const pdf = await loadingTask.promise;
log('PDF loaded:', { numPages: pdf.numPages });
statusEl.textContent = 'Fetching page 1...';
const page = await pdf.getPage(1);
log('Page loaded:', { pageNumber: page.pageNumber, rotate: page.rotate });
statusEl.textContent = 'Calling getTextContent()...';
const textContent = await page.getTextContent();
log('getTextContent() succeeded');
log('items.length =', textContent.items.length);
log('styles keys =', Object.keys(textContent.styles).length);
log('First 5 text items:', textContent.items.slice(0, 5).map((item) => ({
str: item.str,
dir: item.dir,
hasEOL: item.hasEOL,
})));
statusEl.textContent = 'Success';
} catch (error) {
console.error('Repro failed:', error);
log('getTextContent() threw:', error);
statusEl.textContent = `Error: ${error?.name ?? 'UnknownErrorException'}`;
} finally {
runButton.disabled = false;
}
}
runButton.addEventListener('click', () => {
void runRepro();
});
if (autoRunCheckbox.checked) {
void runRepro();
}