storage-complex-keys.html (2050B)
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title>Storage inspector test for correct keys in the sidebar</title> 6 </head> 7 <body> 8 <script type="application/javascript"> 9 "use strict"; 10 11 // Some local storage items ... 12 localStorage.setItem("", "1"); 13 localStorage.setItem("键", "2"); 14 // ... and finally some session storage items too 15 sessionStorage.setItem("Key with spaces", "3"); 16 sessionStorage.setItem("Key#with~special$characters", "4"); 17 // long string 18 const longKey = "a".repeat(1000); 19 sessionStorage.setItem(longKey, "5"); 20 21 const idbGenerator = async function () { 22 const request = indexedDB.open("idb", 1); 23 request.onerror = function() { 24 throw new Error("Error opening database connection"); 25 }; 26 27 const db = await new Promise(done => { 28 request.onupgradeneeded = event => { 29 const _db = event.target.result; 30 const store = _db.createObjectStore("obj", { keyPath: "id" }); 31 store.createIndex("name", "name", { unique: false }); 32 store.transaction.oncomplete = () => { 33 done(_db); 34 }; 35 }; 36 }); 37 38 // Prevents AbortError 39 await new Promise(done => { 40 request.onsuccess = done; 41 }); 42 43 const transaction = db.transaction("obj", "readwrite"); 44 const store = transaction.objectStore("obj"); 45 46 store.add({id: "", name: "foo"}); 47 store.add({id: "键", name: "foo2"}); 48 store.add({id: "Key with spaces", name: "foo3"}); 49 store.add({id: "Key#with~special$characters", name: "foo4"}); 50 store.add({id: longKey, name: "foo5"}); 51 52 db.close(); 53 54 console.log("Added local and session storage items and indexedDB"); 55 }; 56 57 function deleteDB(dbName) { 58 return new Promise(resolve => { 59 dump("removing database " + dbName + " from " + document.location + "\n"); 60 indexedDB.deleteDatabase(dbName).onsuccess = resolve; 61 }); 62 } 63 64 window.setup = async function () { 65 await idbGenerator(); 66 }; 67 68 window.clear = async function () { 69 localStorage.clear(); 70 sessionStorage.clear(); 71 72 await deleteDB("idb"); 73 74 dump("Removed data from " + document.location + "\n"); 75 }; 76 </script> 77 </body> 78 </html>