Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" />
- <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
- <title>IndexedDB Pastebin</title>
- <link href="https://cdn.jsdelivr.net/npm/[email protected]/themes/prism-tomorrow.css" rel="stylesheet"/>
- <style>
- body { font-family: sans-serif; background: #1e1e1e; color: #f5f5f5; margin: 0; padding: 1rem; }
- .container { max-width: 800px; margin: auto; background: #2d2d2d; padding: 1rem; border-radius: 8px; }
- textarea, select, button, input[type="file"] {
- width: 100%; margin-top: 1rem; padding: 0.5rem; font-size: 1rem;
- background: #1e1e1e; color: #f5f5f5; border: 1px solid #444; border-radius: 4px;
- }
- pre { padding: 1rem; border-radius: 8px; overflow-x: auto; background: #1e1e1e; }
- .meta { margin-top: 1rem; font-size: 0.85rem; color: #aaa; }
- .hidden { display: none; }
- </style>
- </head>
- <body>
- <div class="container" id="create-section">
- <h2>Create a New Paste</h2>
- <textarea id="pasteText" rows="10" placeholder="Paste your text or code here..."></textarea>
- <select id="language">
- <option value="plaintext">Plain Text</option>
- <option value="javascript">JavaScript</option>
- <option value="python">Python</option>
- <option value="bash">Bash</option>
- <option value="json">JSON</option>
- <option value="html">HTML</option>
- </select>
- <button onclick="createPaste()">Create Paste</button>
- <button onclick="exportPastes()">Export Pastes</button>
- <input type="file" id="importFile" accept=".json" onchange="importPastes()">
- </div>
- <div class="container hidden" id="view-section">
- <div style="display: flex; justify-content: space-between; align-items: center;">
- <h2>View Paste</h2>
- <button onclick="copyLink()">Copy Link</button>
- </div>
- <pre><code id="pasteOutput" class="language-plaintext"></code></pre>
- <div class="meta" id="metaInfo"></div>
- </div>
- <!-- Prism for syntax highlighting -->
- <script src="https://cdn.jsdelivr.net/npm/[email protected]/prism.js"></script>
- <script src="https://cdn.jsdelivr.net/npm/[email protected]/components/prism-python.min.js"></script>
- <script src="https://cdn.jsdelivr.net/npm/[email protected]/components/prism-javascript.min.js"></script>
- <script src="https://cdn.jsdelivr.net/npm/[email protected]/components/prism-json.min.js"></script>
- <script src="https://cdn.jsdelivr.net/npm/[email protected]/components/prism-bash.min.js"></script>
- <script src="https://cdn.jsdelivr.net/npm/[email protected]/components/prism-markup.min.js"></script>
- <script>
- const DB_NAME = 'pastebin';
- const STORE_NAME = 'pastes';
- let db;
- function openDatabase() {
- return new Promise((resolve, reject) => {
- const request = indexedDB.open(DB_NAME, 1);
- request.onerror = () => reject("Failed to open DB");
- request.onsuccess = () => {
- db = request.result;
- resolve();
- };
- request.onupgradeneeded = (e) => {
- const db = e.target.result;
- db.createObjectStore(STORE_NAME, { keyPath: "id" });
- };
- });
- }
- function savePaste(paste) {
- return new Promise((resolve, reject) => {
- const tx = db.transaction(STORE_NAME, "readwrite");
- const store = tx.objectStore(STORE_NAME);
- const request = store.add(paste);
- request.onsuccess = () => resolve();
- request.onerror = () => reject("Failed to save paste");
- });
- }
- function getPaste(id) {
- return new Promise((resolve, reject) => {
- const tx = db.transaction(STORE_NAME, "readonly");
- const store = tx.objectStore(STORE_NAME);
- const request = store.get(id);
- request.onsuccess = () => resolve(request.result);
- request.onerror = () => reject("Failed to fetch paste");
- });
- }
- async function createPaste() {
- const text = document.getElementById("pasteText").value;
- const language = document.getElementById("language").value;
- const id = Math.random().toString(36).substr(2, 8);
- const createdAt = new Date().toISOString();
- const paste = { id, text, language, createdAt };
- await savePaste(paste);
- window.location.hash = id;
- location.reload();
- }
- async function showPaste(id) {
- const paste = await getPaste(id);
- if (!paste) {
- alert("Paste not found");
- return;
- }
- document.getElementById("create-section").classList.add("hidden");
- document.getElementById("view-section").classList.remove("hidden");
- const codeBlock = document.getElementById("pasteOutput");
- codeBlock.className = "language-" + paste.language;
- codeBlock.textContent = paste.text;
- Prism.highlightElement(codeBlock);
- document.getElementById("metaInfo").textContent =
- "Created at: " + new Date(paste.createdAt).toLocaleString();
- }
- function copyLink() {
- navigator.clipboard.writeText(window.location.href);
- alert("Link copied to clipboard!");
- }
- async function exportPastes() {
- const tx = db.transaction(STORE_NAME, "readonly");
- const store = tx.objectStore(STORE_NAME);
- const request = store.getAll();
- request.onsuccess = () => {
- const data = JSON.stringify(request.result, null, 2);
- const blob = new Blob([data], { type: "application/json" });
- const url = URL.createObjectURL(blob);
- const a = document.createElement("a");
- a.href = url;
- a.download = "pastes-export.json";
- a.click();
- URL.revokeObjectURL(url);
- };
- request.onerror = () => alert("Failed to export pastes");
- }
- async function importPastes() {
- const fileInput = document.getElementById("importFile");
- const file = fileInput.files[0];
- if (!file) return;
- const text = await file.text();
- let data;
- try {
- data = JSON.parse(text);
- } catch (e) {
- alert("Invalid JSON file");
- return;
- }
- const tx = db.transaction(STORE_NAME, "readwrite");
- const store = tx.objectStore(STORE_NAME);
- for (const paste of data) {
- try {
- store.put(paste); // Upsert
- } catch (e) {
- console.error("Failed to import paste:", paste.id, e);
- }
- }
- alert("Import completed! Reload the page to view imported pastes.");
- }
- (async () => {
- await openDatabase();
- const hash = window.location.hash.replace("#", "");
- if (hash) {
- await showPaste(hash);
- }
- })();
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment