Created
July 16, 2025 07:07
-
-
Save patrick91/b672e2476e48a956d255264ff3f3e1f4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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