// --- UTILITIES ---
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function fetchText(url) {
try {
const res = await fetch(url, { cache: "no-store" });
if (!res.ok) return { ok: false, error: ${res.status} ${res.statusText} };
const text = await res.text();
return { ok: true, text };
} catch (e) {
return { ok: false, error: e.message };
}
}
function getDoctypeStr() {
const dt = document.doctype;
if (!dt) return '';
return <!DOCTYPE ${dt.name}${dt.publicId ? PUBLIC "{dt.publicId}"` : ''}{dt.systemId ? "${dt.systemId}" : ''}>\n`;
}
// Cleans the cloned HTML by removing script and style tags to avoid unwanted execution or duplication of UI
function cleanClonedHTML(docElement) {
// Remove all and tags from the clone
const scripts = docElement.querySelectorAll('script');
scripts.forEach(el => el.remove());
const styles = docElement.querySelectorAll('style');
styles.forEach(el => el.remove());
// Also remove link tags that might reference CSS (optional, but safer)
const links = docElement.querySelectorAll('link[rel="stylesheet"]');
links.forEach(el => el.remove());
// Remove event handlers (attributes starting with "on")
const elements = docElement.querySelectorAll('*');
elements.forEach(el => {
[...el.attributes].forEach(attr => {
if (attr.name.startsWith('on')) el.removeAttribute(attr.name);
});
});
// Append overlay to body last
document.body.appendChild(overlay);
// --- STATE ---
let paused = false;
let stopped = false;
let progress = 0; // 0-1
let logLines = [];
let snapshotBuilder = [];
let lastCodeUpdateTime = 0;
let codeViewerIsVisible = false;
// Settings variables
let verbosePrint = config.verbosePrint;
let speedMultiplier = 1;
let maxLogLines = config.maxLogLines;
let maxCodeViewerLines = config.codeViewerMaxLines;
// Simple pretty print HTML - basic indentation (not robust)
function prettyPrintHTML(htmlString) {
const tab = ' ';
let indent = 0;
return htmlString.split(/>\s*</).map(function(element) {
if (element.match(/^/\w/)) indent--;
let pad = '';
for (let i = 0; i < indent; i++) pad += tab;
let line = pad + '<' + element + '>';
if (element.match(/^<?\w[^>]*[^/]$/)) indent++;
return line;
}).join('\n').replace(/>\n</g, '>\n<');
}
// Update code viewer content throttled
function updateCodeViewer() {
if (!codeViewerIsVisible) return;
const now = Date.now();
if (now - lastCodeUpdateTime < 200) return;
lastCodeUpdateTime = now;
let fullCode = snapshotBuilder.join('');
if (fullCode.length > maxCodeViewerLines * 100) {
fullCode = fullCode.slice(-maxCodeViewerLines * 100);
}
const pretty = prettyPrintHTML(fullCode);
codeViewer.value = pretty;
codeViewer.scrollTop = codeViewer.scrollHeight;
}
// Append to snapshot string builder and update code viewer
function appendSnapshot(str) {
snapshotBuilder.push(str);
updateCodeViewer();
}
function clearCodeViewer() {
snapshotBuilder = [];
updateCodeViewer();
}
// --- CONTROL HELPERS ---
function waitWhilePaused() {
return new Promise(resolve => {
const interval = setInterval(() => {
if (stopped) {
clearInterval(interval);
resolve(false);
}
else if (!paused) {
clearInterval(interval);
resolve(true);
}
// else remain paused
}, 100);
});
}
// --- STORAGE DUMP FUNCTIONS ---
function dumpLocalStorage() {
appendLog("!!! Dumping localStorage:");
let count = 0;
for (let i = 0; i < localStorage.length; i++) {
const k = localStorage.key(i);
const v = localStorage.getItem(k);
appendLog( Key: ${k} => Value: ${v ? v.substring(0, 100) : ''});
count++;
if (count >= 50) break;
}
appendLog(!!! localStorage dumped ${count} items.);
}
function dumpSessionStorage() {
appendLog("!!! Dumping sessionStorage:");
let count = 0;
for (let i = 0; i < sessionStorage.length; i++) {
const k = sessionStorage.key(i);
const v = sessionStorage.getItem(k);
appendLog( Key: ${k} => Value: ${v ? v.substring(0, 100) : ''});
count++;
if (count >= 50) break;
}
appendLog(!!! sessionStorage dumped ${count} items.);
}
function dumpCookies() {
appendLog("!!! Dumping cookies:");
const cookies = document.cookie.split(';').filter(c => c.trim().length > 0);
if (cookies.length === 0) {
appendLog(" No cookies found.");
return;
}
let count = 0;
for (const c of cookies) {
appendLog( Cookie: ${c.trim()});
count++;
if (count >= 50) break;
}
appendLog(!!! Cookies dumped ${count} items.);
}
async function dumpIndexedDB() {
appendLog("!!! Dumping IndexedDB databases (async)...");
if (!('indexedDB' in window)) {
appendLog(" IndexedDB NOT supported in this browser.");
return;
}
if (!('databases' in indexedDB)) {
appendLog(" Browser does not support indexedDB.databases() API. Skipping.");
return;
}
try {
const dbs = await indexedDB.databases();
if (!dbs.length) {
appendLog(" No IndexedDB databases found.");
return;
}
for (let i = 0; i < dbs.length; i++) {
const dbInfo = dbs[i];
appendLog( DB #${i + 1}: ${dbInfo.name});
await waitWhilePaused();
if (stopped) {
appendLog("!!! Stopped during IndexedDB dump.");
return;
}
try {
await dumpOneIndexedDB(dbInfo.name);
} catch (e) {
appendLog( ERROR reading DB ${dbInfo.name}: ${e.message});
}
}
appendLog("!!! Finished dumping IndexedDB.");
} catch (e) {
appendLog( ERROR: ${e.message});
}
}
async function dumpOneIndexedDB(name) {
return new Promise((resolve, reject) => {
const openReq = indexedDB.open(name);
openReq.onerror = () => {
appendLog( ERROR opening DB ${name}: ${openReq.error?.message || 'Unknown error'});
reject(openReq.error);
};
openReq.onsuccess = async () => {
const db = openReq.result;
appendLog( Opened DB ${name}, version ${db.version});
const storeNames = Array.from(db.objectStoreNames);
for (let i = 0; i < storeNames.length; i++) {
if (stopped) {
appendLog("!!! Stopped during ObjectStore dump.");
db.close();
resolve();
return;
}
await waitWhilePaused();
appendLog( ObjectStore #${i + 1}/${storeNames.length}: ${storeNames[i]});
try {
await dumpObjectStore(db, storeNames[i]);
} catch (e) {
appendLog( ERROR accessing ObjectStore ${storeNames[i]}: ${e.message});
}
}
db.close();
resolve();
};
});
}
function processCommand(cmd) {
switch (cmd) {
case 'pause':
if (!paused) {
paused = true;
appendLog("!!! Command: pause - process paused");
btnPauseResume.textContent = 'Resume';
} else {
appendLog("!!! Process is already paused.");
}
break;
case 'resume':
if (paused) {
paused = false;
appendLog("!!! Command: resume - process resumed");
btnPauseResume.textContent = 'Pause';
} else {
appendLog("!!! Process is already running.");
}
break;
case 'stop':
if (!stopped) {
stopped = true;
appendLog("!!! Command: stop - process stopping...");
} else {
appendLog("!!! Process is already stopped.");
}
break;
case 'help':
appendLog("!!! Commands available: pause, resume, stop, help");
break;
default:
appendLog(!!! Unknown command: ${cmd}. Type 'help' for list.);
break;
}
}
// --- LONG PROCESS ---
appendLog("!!! Starting page copying process ...");
appendLog("!!! You can control the process with the command input below.");
appendLog("!!! Type 'help' and press Enter for commands.");
clearCodeViewer();
async function longProcess() {
// Clear vars
snapshotBuilder = [];
updateCodeViewer();
// Step 1: Dump storage etc
appendLog("!!! Step 1: Reading localStorage");
dumpLocalStorage();
if (!(await waitWhilePaused())) return;
await sleep(2000 / speedMultiplier);
if (stopped) return;
appendLog("!!! Step 2: Reading sessionStorage");
dumpSessionStorage();
if (!(await waitWhilePaused())) return;
await sleep(2000 / speedMultiplier);
if (stopped) return;
appendLog("!!! Step 3: Reading cookies");
dumpCookies();
if (!(await waitWhilePaused())) return;
await sleep(1500 / speedMultiplier);
if (stopped) return;
appendLog("!!! Step 4: Reading IndexedDB (may take long)");
await dumpIndexedDB();
if (stopped) return;
// Step 5: Get doctype and clone html
appendLog("!!! Step 5: Getting DOCTYPE");
appendSnapshot(getDoctypeStr());
if (!(await waitWhilePaused())) return;
appendLog("!!! Step 6: Cloning <html> element");
const htmlClone = document.documentElement.cloneNode(true);
cleanClonedHTML(htmlClone);
appendLog("!!! Removed scripts, styles and event handlers from cloned <html>");
if (!(await waitWhilePaused())) return;
// Step 7: Gather CSS
appendLog("!!! Step 7: Gathering CSS");
let combinedCSS = '';
const allStylesheets = Array.from(document.styleSheets);
for (let i = 0; i < allStylesheets.length; i++) {
if (stopped) break;
if (!(await waitWhilePaused())) return;
const sheet = allStylesheets[i];
if (sheet.href) {
appendLog(`!!! Loading external CSS ${i + 1}/${allStylesheets.length}: ${sheet.href}`);
const res = await fetchText(sheet.href);
if (res.ok) combinedCSS += `/* CSS from ${sheet.href} */\n${res.text}\n\n`;
else combinedCSS += `/* ERROR loading CSS ${sheet.href} - ${res.error} */\n\n`;
} else {
appendLog(`!!! Reading inline CSS ${i + 1}/${allStylesheets.length}`);
try {
let cssText = '';
for (const rule of sheet.cssRules) cssText += rule.cssText + "\n";
combinedCSS += `/* Inline CSS */\n${cssText}\n\n`;
} catch (e) {
combinedCSS += '/* Inline CSS inaccessible - skipped */\n\n';
appendLog('!!! Warning: Inline CSS inaccessible due to CORS/restrictions');
}
}
updateProgress((i / allStylesheets.length) * 0.1);
await sleep(1500 / speedMultiplier);
}
if (stopped) return;
// Step 8: Gather JS
appendLog("!!! Step 8: Gathering JS");
let combinedJS = '';
const scripts = Array.from(document.querySelectorAll('script'));
for (let i = 0; i < scripts.length; i++) {
if (stopped) break;
if (!(await waitWhilePaused())) return;
const script = scripts[i];
if (script.src && script.src.trim()) {
appendLog(`!!! Loading external JS ${i + 1}/${scripts.length}: ${script.src}`);
const res = await fetchText(script.src);
if (res.ok) combinedJS += `/* JS from ${script.src} */\n${res.text}\n\n`;
else combinedJS += `/* ERROR loading JS ${script.src} - ${res.error} */\n\n`;
} else {
appendLog(`!!! Reading inline JS ${i + 1}/${scripts.length}`);
combinedJS += `/* Inline JS */\n${script.textContent}\n\n`;
}
updateProgress(0.1 + (i / scripts.length) * 0.1);
await sleep(1500 / speedMultiplier);
}
if (stopped) return;
// Step 9: Inject CSS and JS into cloned document
appendLog("!!! Step 9: Injecting CSS and JS into cloned document");
let head = htmlClone.querySelector('head');
if (!head) {
appendLog("!!! Warning: no <head> found, creating one");
head = document.createElement('head');
htmlClone.insertBefore(head, htmlClone.firstChild);
}
const styleEl = document.createElement('style');
styleEl.type = 'text/css';
styleEl.textContent = combinedCSS;
head.appendChild(styleEl);
let body = htmlClone.querySelector('body');
if (!body) {
appendLog("!!! Warning: no <body> found, creating one");
body = document.createElement('body');
htmlClone.appendChild(body);
}
const scriptEl = document.createElement('script');
scriptEl.type = 'text/javascript';
scriptEl.textContent = combinedJS;
body.appendChild(scriptEl);
updateProgress(0.25);
if (!(await waitWhilePaused())) return;
// Step 10: Add banner inside body
appendLog("!!! Step 10: Adding info banner inside snapshot");
const infoDiv = document.createElement('div');
infoDiv.textContent = `This page is a copied snapshot created on ${new Date().toLocaleString()}`;
Object.assign(infoDiv.style, {
position: 'fixed',
top: '0',
left: '0',
width: '100%',
backgroundColor: '#0f0',
color: '#000',
padding: '8px',
fontWeight: 'bold',
textAlign: 'center',
zIndex: '999999999',
fontFamily: 'monospace',
});
body.insertBefore(infoDiv, body.firstChild);
await sleep(1500 / speedMultiplier);
updateProgress(0.3);
// Step 11: Serialize document and build snapshot progressively
appendLog("!!! Step 11: Serializing final HTML snapshot progressively");
let finalHTML = getDoctypeStr() + htmlClone.outerHTML;
// Split lines for progressive building
// (actual HTML may not have many newlines, so add some replacement for readability)
finalHTML = finalHTML.replace(/></g, ">\n<");
const lines = finalHTML.split(/\r?\n/);
snapshotBuilder = [];
for (let i = 0; i < lines.length; i++) {
if (stopped) break;
if (!(await waitWhilePaused())) return;
snapshotBuilder.push(lines[i] + "\n");
if (i % 50 === 0) updateCodeViewer();
let fraction = 0.3 + ((i / lines.length) * 0.68);
updateProgress(fraction);
await sleep(15 / speedMultiplier); // adjust speed if needed
}
updateCodeViewer();
if (stopped) return;
// Step 12: Trigger download
appendLog("!!! Step 12: Preparing to download snapshot...");
updateProgress(0.99);
const blobText = snapshotBuilder.join('');
const blob = new Blob([blobText], { type: 'text/html' });
const url = URL.createObjectURL(blob);
const filename = `page-copied-${new Date().toISOString().replace(/[T:.]/g, '-').slice(0, -5)}.html`;
const a = document.createElement('a');
a.href = url;
a.download = filename;
document.body.appendChild(a);
appendLog(`!!! Triggering download: ${filename}`);
a.click();
setTimeout(() => {
URL.revokeObjectURL(url);
a.remove();
updateProgress(1);
appendLog("!!! Download complete. Cleaning up overlay...");
cleanup();
}, 2500);
}
function cleanup() {
overlay.remove();
codeViewer.remove();
settingsPanel.remove();
}
To ensure both the code viewer and settings panel cover the entire viewport and appear fully on top of the overlay when clicked, you need to:
Set their position styles to cover the entire screen (full viewport width and height).
Make sure their z-index is higher than the main overlay, so they are visually above.
Adjust their top, left, width, and height styles accordingly.
Optionally set backgroundColor and overflow for usability.
Below is a minimal snippet illustrating how to modify the existing style definitions for your codeViewer and settingsPanel elements to cover the full viewport:
// Modify codeViewer styles to cover whole viewport and be on top
Object.assign(codeViewer.style, {
position: 'fixed',
top: '0',
left: '0',
width: '100vw',
height: '100vh',
background: '#111',
color: '#0f0',
fontFamily: 'Consolas, monospace',
fontSize: '12px',
border: 'none',
padding: '10px',
overflow: 'auto',
resize: 'none', // optional: usually no need to resize fullscreen
zIndex: '2147483650', // keep sufficiently high to be above overlay
display: 'none',
});
// Modify settingsPanel styles similarly
Object.assign(settingsPanel.style, {
position: 'fixed',
top: '0',
left: '0',
width: '100vw',
height: '100vh',
background: '#111',
color: '#0f0',
fontFamily: 'Consolas, monospace',
fontSize: '13px',
border: 'none',
padding: '20px',
overflowY: 'auto',
zIndex: '2147483650', // same high z-index
display: 'none',
});
Explanation:
position: fixed keeps the element fixed relative to viewport.
top: 0; left: 0; width: 100vw; height: 100vh; expands the element over the full viewport.
zIndex: 2147483650 (or similar large number) makes sure the panel appears above your overlay.
You can set border to none or keep it visually as you want.
overflow: auto allows scrolling inside the panel if content overflows.
display: none initially hides the panel; toggled on button click.
How to integrate:
Find the part in your script where you define the codeViewer and settingsPanel styles and replace those sections with the above.
Example snippet integration in your script (replace the existing style assignments):
Add a close button styled to float at the top-right for ease of dismissal.
Set cursor: pointer on close buttons.
Add tabindex and keyboard shortcut support (e.g., ESC to close).
This will make your code viewer and settings cover the entire viewport above all other UI layers when toggled visible, preventing them from appearing behind the overlay.