Index: chrome/common/extensions/docs/examples/api/document_scan/scan.js |
diff --git a/chrome/common/extensions/docs/examples/api/document_scan/scan.js b/chrome/common/extensions/docs/examples/api/document_scan/scan.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..167e6dda0874168a061d91faaa7ca008d6ea608f |
--- /dev/null |
+++ b/chrome/common/extensions/docs/examples/api/document_scan/scan.js |
@@ -0,0 +1,115 @@ |
+var requestButton = document.getElementById("requestButton"); |
+var listScanners = document.getElementById('listScanners'); |
+var scanButton = document.getElementById('scanButton'); |
+var scanOption = document.getElementById('scanOption'); |
+var scanModes = [ |
+ document.getElementById('scanModeColor'), |
+ document.getElementById('scanModeGrey'), |
+ document.getElementById('scanModeBW') |
+ ]; |
+var scannerName = document.getElementById('scannerName'); |
+var scannedImages = document.getElementById('scannedImages'); |
+var waitAnimation = document.getElementById('waitAnimation'); |
+var scanner; |
+var imageMimeType; |
+ |
+function setOnlyChild(parent, child) { |
+ while (parent.firstChild) { |
+ parent.removeChild(parent.firstChild); |
+ } |
+ parent.appendChild(child); |
+} |
+ |
+var gotScannerList = function(scanners) { |
+ waitAnimation.style.display = 'none'; |
+ console.log('Got ' + scanners.length + ' scanners'); |
+ if (scanners.length > 0) { |
+ scanOption.style.display = 'block'; |
+ scanner = scanners[0].name; |
+ imageMimeType = scanners[0].imageMimeType; |
+ console.log('Using scanner ' + scanner); |
+ setOnlyChild(scannerName, document.createTextNode(scanner)); |
+ } else { |
+ scanOption.style.display = 'none'; |
+ } |
+}; |
+ |
+var gotPermission = function(result) { |
+ waitAnimation.style.display = 'block'; |
+ requestButton.style.display = 'none'; |
+ listScanners.style.display = 'block'; |
+ console.log('App was granted the "documentScan" permission.'); |
+ chrome.documentScan.listScanners(gotScannerList); |
+}; |
+ |
+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); |
+ } |
+ }); |
+}); |
+ |
+listScanners.addEventListener('click', function() { |
+ waitAnimation.style.display = 'block'; |
+ chrome.documentScan.listScanners(gotScannerList); |
+}); |
+ |
+function getPngDataUrl(buffer) { |
+ // Much discussion on the relative performance of various methods for doing |
+ // this conversion at http://jsperf.com/encoding-xhr-image-data/16 and |
+ // https://gist.github.com/jonleighton/958841. Picking the most concise |
+ // version that works. |
+ console.log('Making bytes from buffer'); |
+ var bytes = new Uint8Array(buffer); |
+ var len = bytes.byteLength; |
+ console.log('Making binary from bytes'); |
+ var binary = ''; |
+ for (var i = 0; i < len; i++) { |
+ binary += String.fromCharCode(bytes[i]); |
+ } |
+ console.log('Returning btoa'); |
+ return 'data:' + imageMimeType + ';base64,' + btoa(binary); |
+} |
+ |
+var onScanCompleted = function(buffer) { |
+ waitAnimation.style.display = 'none'; |
+ if (chrome.runtime.lastError) { |
+ console.log('Scan failed: ' + chrome.runtime.lastError.message); |
+ return; |
+ } |
+ console.log('Scan completed with data length ' + |
+ buffer.byteLength + '.'); |
+ console.log(buffer); |
+ urlData = getPngDataUrl(buffer); |
+ console.log('URL is ' + urlData); |
+ var scannedImage = document.createElement('img'); |
+ scannedImage.src = urlData; |
+ scannedImages.insertBefore(scannedImage, scannedImages.firstChild); |
+}; |
+ |
+scanButton.addEventListener('click', function() { |
+ var scanProperties = {}; |
+ for (var idx = 0; idx < scanModes.length; ++idx) { |
+ if (scanModes[idx].checked) { |
+ var scanMode = scanModes[idx].value; |
+ console.log('Scanning type is ' + scanMode); |
+ scanProperties['mode'] = scanMode; |
+ break; |
+ } |
+ } |
+ waitAnimation.style.display = 'block'; |
+ chrome.documentScan.scan(scanner, scanProperties, onScanCompleted); |
+}); |
+ |
+chrome.permissions.contains(permissionObj, function(result) { |
+ if (result) { |
+ gotPermission(); |
+ } |
+}); |