-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocalstorage.html
207 lines (180 loc) · 6.34 KB
/
localstorage.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<!DOCTYPE html>
<html>
<head>
<title>Scanning with LocalStorage</title>
<link rel="stylesheet" href="css/global.css" />
<link rel="stylesheet" href="css/scan.css" />
</head>
<body>
<div class="ds-div-scanPageHeader">
<div class="ds-div-title">Scanning with LocalStorage</div>
<p>The Local Storage feature automatically saves the scanned images from the control to your disk, allowing you
to restore the content of the previous session when you reopen the webpage. This is especially useful if
your computer goes to sleep or shuts down unexpectedly during a large scan. To test this feature, enable
Local Storage, close the page after scanning, and then reopen the page to see that content from the previous
session gets restored.</p>
</div>
<div class="ds-div-scanPageDemo">
<div class="ds-div-scanPage-column ds-div-scanPage-column0">
<div><span>Select Source:</span><select size="1" id="source"></select></div>
<div>
<label for="chkAutoSaveToStorage"><input type="checkbox" id="chkAutoSaveToStorage"
onchange="checkBoxChanged_AutoSaveStorage();" checked>Auto-Save to Storage</label>
</div>
<div>
<input type="button" value="Scan" class="ds-button-bg-black" style="margin-bottom: 20px;"
onclick="AcquireImage();" />
<input style="margin-left:20px" onclick="RefreshPage()" class="ds-button-bg-white" type="button"
value="Refresh browser" />
</div>
</div>
<div class="ds-div-scanPage-column ds-div-scanPage-column1">
<div id="dwtcontrolContainer" style="width: 100%; height: 100%;"></div>
</div>
</div>
<style>
.ds-div-scanPage-column0>div {
margin: 7px;
}
.ds-div-scanPage-column0 label {
width: 180px;
display: inline-block;
}
.ds-div-scanPageDemo {
margin-top: 7px;
}
.ds-div-scanPageAPI,
.ds-div-scanPageHeader,
.ds-div-scanPageNote,
.ds-div-scanPageDemo,
.ds-div-scanPageDemo-button,
.ds-div-scanPageTry {
min-width: 890px;
}
</style>
<script src="https://cdn.jsdelivr.net/npm/dwt@latest/dist/dynamsoft.webtwain.min.js"></script>
<script type="text/javascript">
Dynamsoft.DWT.Containers = [{ ContainerId: 'dwtcontrolContainer', Width: '100%', Height: '100%' }];
Dynamsoft.DWT.ProductKey = "DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9";
Dynamsoft.DWT.ResourcesPath = 'https://cdn.jsdelivr.net/npm/dwt@latest/dist';
Dynamsoft.DWT.AutoLoad = true;
var localStoreUid, storeName = 'DynamicWebTWAIN_LocalStorage', storeName_EnableAutoSaveStorage = 'DynamicWebTWAIN_EnableAutoSaveStorage';
(function () {
var chkAutoSaveToStorage = document.getElementById('chkAutoSaveToStorage');
var checked = localStorage[storeName_EnableAutoSaveStorage];
if (checked === "false") {
chkAutoSaveToStorage.checked = false;
localStoreUid = '';
delete localStorage[storeName];
} else {
chkAutoSaveToStorage.checked = true;
localStoreUid = localStorage[storeName];
localStorage[storeName_EnableAutoSaveStorage] = "true";
}
})();
var DWTObject, deviceList;
Dynamsoft.DWT.RegisterEvent("OnWebTwainReady", function () {
DWTObject = Dynamsoft.DWT.GetWebTwain('dwtcontrolContainer'); // Get the Dynamic Web TWAIN object that is embeded in the div with id 'dwtcontrolContainer'
if (DWTObject) {
deviceList = [];
DWTObject.GetDevicesAsync().then(function (devices) {
for (var i = 0; i < devices.length; i++) { // Get how many sources are installed in the system
document.getElementById('source').options.add(new Option(devices[i].displayName, i)); // Add the sources in a drop-down list
deviceList.push(devices[i]);
}
if (isStorageEnabled()) {
restoreStorage();
DWTObject.RegisterEvent('OnBufferChanged', Dynamsoft_OnBufferChanged);
}
}).catch(function (exp) {
alert(exp.message);
});
}
});
function AcquireImage() {
if (DWTObject) {
var ddlSource = document.getElementById('source');
DWTObject.SelectDeviceAsync(deviceList[ddlSource.selectedIndex]).then(function () {
return DWTObject.AcquireImageAsync({
IfShowUI: false,
IfCloseSourceAfterAcquire: true // Scanner source will be disabled/closed automatically after the scan.
});
}).catch(function (exp) {
console.error(exp);
});
}
}
function Dynamsoft_OnBufferChanged(p1) {
if (isStorageEnabled() && p1) {
if (p1.action === 'shift' || p1.action === 'filter') return;
saveStorage();
}
}
function checkBoxChanged_AutoSaveStorage() {
if (isStorageEnabled()) {
localStorage[storeName_EnableAutoSaveStorage] = "true";
DWTObject.RegisterEvent('OnBufferChanged', Dynamsoft_OnBufferChanged);
saveStorage();
} else {
localStorage[storeName_EnableAutoSaveStorage] = "false";
removeStorage();
}
}
async function restoreStorage() {
return _saveOrRestoreStorage(false);
}
async function saveStorage() {
return _saveOrRestoreStorage(true);
}
function removeStorage() {
DWTObject.UnregisterEvent('OnBufferChanged', Dynamsoft_OnBufferChanged);
if (localStoreUid) {
var _localStoreUid = localStoreUid;
localStoreUid = '';
delete localStorage[storeName];
DWTObject.localStorageExist(_localStoreUid).then(function (ifExist) {
if (ifExist) {
DWTObject.removeLocalStorage({ uid: _localStoreUid });
}
}).catch(function (err) { console.log(err); });
}
}
async function _saveOrRestoreStorage(bSave) {
if (!isStorageEnabled()) {
return;
}
try {
var ifExist = false;
if (localStoreUid) {
ifExist = await DWTObject.localStorageExist(localStoreUid);
}
if (ifExist && localStoreUid) {
if (bSave) {
await DWTObject.saveToLocalStorage({ uid: localStoreUid });
} else {
await DWTObject.loadFromLocalStorage({ uid: localStoreUid });
}
} else {
localStoreUid = await DWTObject.createLocalStorage();
localStorage[storeName] = localStoreUid;
await DWTObject.saveToLocalStorage({ uid: localStoreUid });
}
} catch (_ex2) {
console.log(_ex2);
}
}
function isStorageEnabled() {
var chkAutoSaveToStorage = document.getElementById('chkAutoSaveToStorage');
if (chkAutoSaveToStorage)
return chkAutoSaveToStorage.checked;
return false;
}
function RefreshPage() {
var url = new URL(top.location.href);
url.searchParams.delete("t");
url.searchParams.append("t", new Date().getTime());
top.location.href = url;
}
</script>
</body>
</html>