Created
October 8, 2024 09:22
-
-
Save bitsnaps/0890ec7227e003703e40223426442324 to your computer and use it in GitHub Desktop.
Showcase examples to File System Access API (created by claude-3.5-sonnet) working on Chrome v109.x
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>File System Access API Demo</title> | |
<style> | |
body { | |
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; | |
line-height: 1.6; | |
padding: 20px; | |
max-width: 800px; | |
margin: 0 auto; | |
background-color: #f4f4f4; | |
color: #333; | |
} | |
h1 { | |
color: #2c3e50; | |
text-align: center; | |
margin-bottom: 30px; | |
} | |
.button-container { | |
display: flex; | |
justify-content: space-between; | |
margin-bottom: 20px; | |
} | |
button { | |
padding: 10px 15px; | |
font-size: 16px; | |
cursor: pointer; | |
background-color: #3498db; | |
color: white; | |
border: none; | |
border-radius: 5px; | |
transition: background-color 0.3s ease; | |
} | |
button:hover { | |
background-color: #2980b9; | |
} | |
button:active { | |
transform: translateY(1px); | |
} | |
#fileContent { | |
width: 100%; | |
height: 300px; | |
margin-top: 20px; | |
padding: 10px; | |
font-size: 16px; | |
border: 1px solid #ccc; | |
border-radius: 5px; | |
resize: vertical; | |
} | |
.content-container { | |
background-color: white; | |
padding: 20px; | |
border-radius: 8px; | |
box-shadow: 0 0 10px rgba(0,0,0,0.1); | |
} | |
h3 { | |
color: #2c3e50; | |
margin-bottom: 10px; | |
} | |
</style> | |
</head> | |
<body> | |
<h1>File System Access API Demo</h1> | |
<div class="button-container"> | |
<button id="openFile">Open File</button> | |
<button id="saveFile">Save File</button> | |
<button id="createFile">Create New File</button> | |
<button id="deleteFile">Delete File</button> | |
</div> | |
<div class="content-container"> | |
<h3>File Content:</h3> | |
<textarea id="fileContent" placeholder="File content will appear here..."></textarea> | |
</div> | |
<script> | |
// The JavaScript remains the same as in the previous example | |
let currentFileHandle = null; | |
document.getElementById('openFile').addEventListener('click', async () => { | |
try { | |
[currentFileHandle] = await window.showOpenFilePicker(); | |
const file = await currentFileHandle.getFile(); | |
const content = await file.text(); | |
document.getElementById('fileContent').value = content; | |
} catch (error) { | |
console.error('Error opening file:', error); | |
} | |
}); | |
document.getElementById('saveFile').addEventListener('click', async () => { | |
if (!currentFileHandle) { | |
alert('Please open a file first'); | |
return; | |
} | |
try { | |
const writable = await currentFileHandle.createWritable(); | |
await writable.write(document.getElementById('fileContent').value); | |
await writable.close(); | |
alert('File saved successfully!'); | |
} catch (error) { | |
console.error('Error saving file:', error); | |
} | |
}); | |
document.getElementById('createFile').addEventListener('click', async () => { | |
try { | |
const options = { | |
types: [ | |
{ | |
description: 'Text Files', | |
accept: { 'text/plain': ['.txt'] }, | |
}, | |
], | |
}; | |
currentFileHandle = await window.showSaveFilePicker(options); | |
const writable = await currentFileHandle.createWritable(); | |
await writable.write('This is a new file.'); | |
await writable.close(); | |
alert('New file created successfully!'); | |
const file = await currentFileHandle.getFile(); | |
const content = await file.text(); | |
document.getElementById('fileContent').value = content; | |
} catch (error) { | |
console.error('Error creating file:', error); | |
} | |
}); | |
document.getElementById('deleteFile').addEventListener('click', async () => { | |
try { | |
if (!currentFileHandle) { | |
[currentFileHandle] = await window.showOpenFilePicker(); | |
} | |
if (currentFileHandle) { | |
// Confirm deletion | |
if (confirm(`Are you sure you want to delete "${currentFileHandle.name}"?`)) { | |
// Get the file name | |
const fileName = currentFileHandle.name; | |
// Open the parent directory | |
const dirHandle = await window.showDirectoryPicker(); | |
// Remove the file from the parent directory | |
await dirHandle.removeEntry(fileName); | |
alert('File deleted successfully!'); | |
currentFileHandle = null; | |
document.getElementById('fileContent').value = ''; | |
} | |
} | |
} catch (error) { | |
console.error('Error deleting file:', error); | |
alert('Error deleting file: ' + error.message); | |
} | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment