Skip to content

Instantly share code, notes, and snippets.

@patrick91
Created July 16, 2025 07:07
Show Gist options
  • Save patrick91/b672e2476e48a956d255264ff3f3e1f4 to your computer and use it in GitHub Desktop.
Save patrick91/b672e2476e48a956d255264ff3f3e1f4 to your computer and use it in GitHub Desktop.
function exportCodeHearthData() {
const codeHearthData = {};
let totalKeys = 0;
// Collect all codeheart_v2 data
for (let i = 0; i < localStorage.length; i++) {
const key = localStorage.key(i);
if (key && key.startsWith('codeheart_v2')) {
try {
const value = localStorage.getItem(key);
// Try to parse as JSON, fallback to string
try {
codeHearthData[key] = JSON.parse(value);
} catch (parseError) {
codeHearthData[key] = value;
}
totalKeys++;
} catch (error) {
console.error(`Error retrieving key "${key}":`, error);
codeHearthData[key] = null;
}
}
}
// Create export object with metadata
const exportData = {
exportDate: new Date().toISOString(),
totalKeys: totalKeys,
domain: window.location.hostname,
data: codeHearthData
};
// Convert to JSON string
const jsonString = JSON.stringify(exportData, null, 2);
// Print the JSON data
console.log('πŸ“Š Exported Data:');
console.log(jsonString);
// Generate the bookmarklet code
const bookmarkletCode = `javascript:(function(){const predefinedData=${JSON.stringify(exportData)};function importCodeHearthData(importData){if(!importData||!importData.hasOwnProperty('data')){alert('Invalid backup file format!');return;}let importedKeys=0;let skippedKeys=0;if(importData.totalKeys===0){alert('Backup file is empty (0 keys found).\\nOriginal export from: '+importData.domain+'\\nExport date: '+importData.exportDate);return;}Object.entries(importData.data).forEach(([key,value])=>{try{if(localStorage.getItem(key)!==null){const overwrite=confirm(\`Key "\${key}" already exists. Overwrite?\`);if(!overwrite){skippedKeys++;return;}}const valueToStore=typeof value==='object'?JSON.stringify(value):value;localStorage.setItem(key,valueToStore);importedKeys++;}catch(error){console.error(\`Error importing key "\${key}":\`,error);}});alert(\`Import complete!\\nImported: \${importedKeys} keys\\nSkipped: \${skippedKeys} keys\\nOriginal export from: \${importData.domain}\\nExport date: \${importData.exportDate}\`);console.log(\`βœ… Import complete: \${importedKeys} imported, \${skippedKeys} skipped\`);}importCodeHearthData(predefinedData);})();`;
// Copy to clipboard
navigator.clipboard.writeText(bookmarkletCode).then(() => {
console.log('\nπŸ”– Bookmarklet Code (copied to clipboard):');
console.log(bookmarkletCode);
console.log(`\nβœ… Found ${totalKeys} codeheart_v2 keys`);
console.log('πŸ“‹ Bookmarklet code has been copied to clipboard!');
console.log('πŸ’‘ Just paste it as the URL when creating a new bookmark.');
// Show success alert
alert(`βœ… Export Complete!\n\nπŸ“Š Found: ${totalKeys} codeheart_v2 keys\nπŸ“‹ Bookmarklet code copied to clipboard!\n\nNext steps:\n1. Create a new bookmark\n2. Paste the code as the URL\n3. Use the bookmark on another device`);
}).catch(err => {
// Fallback if clipboard API fails
console.error('Failed to copy to clipboard:', err);
console.log('\nπŸ”– Bookmarklet Code (manual copy required):');
console.log(bookmarkletCode);
console.log(`\nβœ… Found ${totalKeys} codeheart_v2 keys`);
console.log('⚠️ Could not copy to clipboard automatically. Please copy the code above manually.');
// Show fallback alert
alert(`βœ… Export Complete!\n\nπŸ“Š Found: ${totalKeys} codeheart_v2 keys\n⚠️ Could not copy to clipboard automatically.\n\nPlease copy the bookmarklet code from the console manually.`);
});
return exportData;
}
// Run the export
exportCodeHearthData();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment