|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | + <head> |
| 4 | + <title>Clipboard & IndexedDB</title> |
| 5 | + </head> |
| 6 | + <body> |
| 7 | + <!-- Dummy paragraphs to be able to copy some text --> |
| 8 | + <p id="para1">1. This is paragraph 1</p> |
| 9 | + <button id="copy_btn" onclick="copy_store(1)">Copy</button> |
| 10 | + |
| 11 | + <p id="para2">2. This is paragraph 2</p> |
| 12 | + <button id="copy_btn" onclick="copy_store(2)">Copy</button> |
| 13 | + |
| 14 | + <p id="para3">3. This is paragraph 3</p> |
| 15 | + <button id="copy_btn" onclick="copy_store(3)">Copy</button> |
| 16 | + |
| 17 | + <p id="para4">4. This is paragraph 4</p> |
| 18 | + <button id="copy_btn" onclick="copy_store(4)">Copy</button> |
| 19 | + |
| 20 | + <div id="container"></div> |
| 21 | + </body> |
| 22 | + <script> |
| 23 | + // In the following line, you should include the prefixes of implementations you want to test. |
| 24 | + window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB; |
| 25 | + // DON'T use "var indexedDB = ..." if you're not in a function. |
| 26 | + // Moreover, you may need references to some window.IDB* objects: |
| 27 | + window.IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction || window.msIDBTransaction || {READ_WRITE: "readwrite"}; // This line should only be needed if it is needed to support the object's constants for older browsers |
| 28 | + window.IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange || window.msIDBKeyRange; |
| 29 | + |
| 30 | + // Check if IndexedDB is supported |
| 31 | + if (!window.indexedDB) { |
| 32 | + window.alert("Your browser doesn't support a stable version of IndexedDB. Such and such feature will not be available."); |
| 33 | + } |
| 34 | + |
| 35 | + // Open the database |
| 36 | + var db; |
| 37 | + |
| 38 | + // Name the database 'clipboard_db' and the version as 1 |
| 39 | + request = window.indexedDB.open("clipboard_db", 1); |
| 40 | + |
| 41 | + data = []; |
| 42 | + |
| 43 | + // In case there was an error |
| 44 | + request.onerror = function(event) { |
| 45 | + alert("Error in creating database"); |
| 46 | + }; |
| 47 | + |
| 48 | + // If the operation was successful |
| 49 | + request.onsuccess = function(event) { |
| 50 | + db = event.target.result; |
| 51 | + |
| 52 | + // Call the function to retrieve information and display it |
| 53 | + get_data(); |
| 54 | + }; |
| 55 | + |
| 56 | + // This event is only implemented in recent browsers |
| 57 | + request.onupgradeneeded = function(event) { |
| 58 | + var db = event.target.result; |
| 59 | + |
| 60 | + // Create an objectStore to hold information about our clips. |
| 61 | + objectStore = db.createObjectStore("clips", { keyPath: "id" }); |
| 62 | + }; |
| 63 | + |
| 64 | + // Function to select a specific paragraph |
| 65 | + function select(para_num){ |
| 66 | + // Create a Range object |
| 67 | + var range = document.createRange(); |
| 68 | + |
| 69 | + // Gets the Selection object |
| 70 | + var selection = window.getSelection(); |
| 71 | + |
| 72 | + // Specifies what is to be selected |
| 73 | + range.selectNodeContents(document.getElementById('para' + para_num)); |
| 74 | + |
| 75 | + // Removes any previous selections |
| 76 | + selection.removeAllRanges(); |
| 77 | + |
| 78 | + // Adds the selection to only the given range |
| 79 | + selection.addRange(range); |
| 80 | + } |
| 81 | + |
| 82 | + // Function to copy selected text and store information |
| 83 | + function copy_store(para_num){ |
| 84 | + // Get text from <p> tag |
| 85 | + var text = document.getElementById('para' + para_num).innerHTML; |
| 86 | + |
| 87 | + // Store information on the data variable |
| 88 | + data.push({id: para_num, para: text}); |
| 89 | + |
| 90 | + // Create an object store to hold your copied text and open it for read/write |
| 91 | + var dataStore = db.transaction("clips", "readwrite").objectStore("clips"); |
| 92 | + |
| 93 | + // For each item in the data variable, add it to the database |
| 94 | + data.forEach(function(data) { |
| 95 | + dataStore.add(data); |
| 96 | + }); |
| 97 | + |
| 98 | + // Select text for the specific paragraph |
| 99 | + select(para_num); |
| 100 | + |
| 101 | + try { |
| 102 | + // Trigger copy event |
| 103 | + document.execCommand('copy'); |
| 104 | + } catch (err) { |
| 105 | + console.log('Error: ' + err); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + // Function to retrieve data from database and display it |
| 110 | + function get_data(){ |
| 111 | + // Get a reference to the object store where our copied text is stored |
| 112 | + var objectStore = db.transaction(["clips"]).objectStore("clips"); |
| 113 | + |
| 114 | + // Open cursor to iterate over the objects in the object store |
| 115 | + var request = objectStore.openCursor(); |
| 116 | + |
| 117 | + // Get reference to the HTML element where data is displayed |
| 118 | + var container = document.getElementById('container'); |
| 119 | + container.innerHTML = "<h1>Contents</h1>"; |
| 120 | + |
| 121 | + // Handle errors in case object store couldn't be accessed |
| 122 | + request.onerror = function(event) { |
| 123 | + // Handle errors! |
| 124 | + console.log('Error accessing data'); |
| 125 | + }; |
| 126 | + |
| 127 | + // If the request is successful, output it on the screen |
| 128 | + request.onsuccess = function(event) { |
| 129 | + let cursor = event.target.result; |
| 130 | + |
| 131 | + // Get all records |
| 132 | + if (cursor) { |
| 133 | + // Values on the database |
| 134 | + let value = cursor.value; |
| 135 | + |
| 136 | + // Display data in HTML element |
| 137 | + // value.para -> para is the column name given in the object store |
| 138 | + container.innerHTML += value.para + "<br>"; |
| 139 | + |
| 140 | + // Move the cursor to the next item if any |
| 141 | + cursor.continue(); |
| 142 | + } |
| 143 | + }; |
| 144 | + } |
| 145 | + </script> |
| 146 | +</html> |
0 commit comments