forked from GoogleChrome/chrome-extensions-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scan.js
69 lines (61 loc) · 2.17 KB
/
scan.js
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
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
var requestButton = document.getElementById("requestButton");
var scanButton = document.getElementById('scanButton');
var scannedImages = document.getElementById('scannedImages');
var waitAnimation = document.getElementById('waitAnimation');
var imageMimeType;
function setOnlyChild(parent, child) {
while (parent.firstChild) {
parent.removeChild(parent.firstChild);
}
parent.appendChild(child);
}
var gotPermission = function(result) {
waitAnimation.style.display = 'block';
requestButton.style.display = 'none';
scanButton.style.display = 'block';
console.log('App was granted the "documentScan" permission.');
waitAnimation.style.display = 'none';
};
var permissionObj = {permissions: ['documentScan']};
requestButton.addEventListener('click', function() {
waitAnimation.style.display = 'block';
chrome.permissions.request( permissionObj, function(result) {
if (result) {
gotPermission();
} else {
console.log('App was not granted the "documentScan" permission.');
console.log(chrome.runtime.lastError);
}
});
});
var onScanCompleted = function(scan_results) {
waitAnimation.style.display = 'none';
if (chrome.runtime.lastError) {
console.log('Scan failed: ' + chrome.runtime.lastError.message);
return;
}
numImages = scan_results.dataUrls.length;
console.log('Scan completed with ' + numImages + ' images.');
for (var i = 0; i < numImages; i++) {
urlData = scan_results.dataUrls[i]
console.log('Scan ' + i + ' data length ' +
urlData.length + '.');
console.log('URL is ' + urlData);
var scannedImage = document.createElement('img');
scannedImage.src = urlData;
scannedImages.insertBefore(scannedImage, scannedImages.firstChild);
}
};
scanButton.addEventListener('click', function() {
var scanProperties = {};
waitAnimation.style.display = 'block';
chrome.documentScan.scan(scanProperties, onScanCompleted);
});
chrome.permissions.contains(permissionObj, function(result) {
if (result) {
gotPermission();
}
});