-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclipb_db.html
108 lines (89 loc) · 3.71 KB
/
clipb_db.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<!DOCTYPE html>
<html>
<head>
<title>Clipboard & WebSQL</title>
</head>
<body>
<!-- Dummy paragraphs to be able to copy some text -->
<p id="para1">1. This is paragraph 1</p>
<button id="copy_btn" onclick="copy_store(1)">Copy</button>
<p id="para2">2. This is paragraph 2</p>
<button id="copy_btn" onclick="copy_store(2)">Copy</button>
<p id="para3">3. This is paragraph 3</p>
<button id="copy_btn" onclick="copy_store(3)">Copy</button>
<p id="para4">4. This is paragraph 4</p>
<button id="copy_btn" onclick="copy_store(4)">Copy</button>
<div id="container"></div>
</body>
<script>
// Open the database
// Args list: openDatabase( db_name, version, description, size )
var db = openDatabase('clipboard', '1.0', 'Clipboard DB', 2 * 1024 * 1024);
// Onload
create_local_table(); // Create table called clips
get_data(); // Retrieve data from table and display
// Function to create a table in the database
function create_local_table(){
db.transaction (
function(transaction) {
transaction.executeSql("CREATE TABLE IF NOT EXISTS clips(text TEXT NOT NULL)");
}
);
}
// Function to retrieve information from the database and display it
function get_data(){
db.transaction(
function(transaction){
transaction.executeSql('SELECT * FROM clips;', [],
function dataHandler(transaction, results){
// Reference to the HTML element that'll hold the data
var container = document.getElementById('container');
// Add content to the container with retrieved rows
var html = "<h2>Contents</h2>";
for(var i = 0; i < results.rows.length; i++){
html += results.rows.item(i).text + "<br>";
}
container.innerHTML = html;
}
);
}
);
}
// Function to select a specific paragraph
function select(para_num){
// Create a Range object
var range = document.createRange();
// Gets the Selection object
var selection = window.getSelection();
// Specifies what is to be selected
range.selectNodeContents(document.getElementById('para' + para_num));
// Removes any previous selections
selection.removeAllRanges();
// Adds the selection to only the given range
selection.addRange(range);
}
// Function to copy selected text and store information
function copy_store(para_num){
// Select text for the specific paragraph
select(para_num);
// Get text from <p> tag
var text = document.getElementById('para' + para_num).innerHTML;
try {
// Trigger copy event
document.execCommand('copy');
} catch (err) {
console.log('Error: ' + err);
}
// Added copied text to the DB
db.transaction(
function(transaction) {
transaction.executeSql('INSERT INTO clips (text) VALUES ("' + text + '");');
},
);
}
// Detect when text is copied
document.addEventListener('copy', function(e){
alert('copied');
});
</script>
</html>