Home | History | Annotate | Download | only in document_scan_test_app
      1 // Copyright 2015 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 var requestButton = document.getElementById("requestButton");
      6 var scanButton = document.getElementById('scanButton');
      7 var scannedImages = document.getElementById('scannedImages');
      8 var waitAnimation = document.getElementById('waitAnimation');
      9 var imageMimeType;
     10 
     11 function setOnlyChild(parent, child) {
     12   while (parent.firstChild) {
     13     parent.removeChild(parent.firstChild);
     14   }
     15   parent.appendChild(child);
     16 }
     17 
     18 var gotPermission = function(result) {
     19   waitAnimation.style.display = 'block';
     20   requestButton.style.display = 'none';
     21   scanButton.style.display = 'block';
     22   console.log('App was granted the "documentScan" permission.');
     23   waitAnimation.style.display = 'none';
     24 };
     25 
     26 var permissionObj = {permissions: ['documentScan']};
     27 
     28 requestButton.addEventListener('click', function() {
     29   waitAnimation.style.display = 'block';
     30   chrome.permissions.request( permissionObj, function(result) {
     31     if (result) {
     32       gotPermission();
     33     } else {
     34       console.log('App was not granted the "documentScan" permission.');
     35       console.log(chrome.runtime.lastError);
     36     }
     37   });
     38 });
     39 
     40 var onScanCompleted = function(scan_results) {
     41   waitAnimation.style.display = 'none';
     42   if (chrome.runtime.lastError) {
     43     console.log('Scan failed: ' + chrome.runtime.lastError.message);
     44     return;
     45   }
     46   numImages = scan_results.dataUrls.length;
     47   console.log('Scan completed with ' + numImages + ' images.');
     48   for (var i = 0; i < numImages; i++) {
     49     urlData = scan_results.dataUrls[i]
     50     console.log('Scan ' + i + ' data length ' +
     51                 urlData.length + '.');
     52     console.log('URL is ' + urlData);
     53     var scannedImage = document.createElement('img');
     54     scannedImage.src = urlData;
     55     scannedImages.insertBefore(scannedImage, scannedImages.firstChild);
     56   }
     57 };
     58 
     59 scanButton.addEventListener('click', function() {
     60   var scanProperties = {};
     61   waitAnimation.style.display = 'block';
     62   chrome.documentScan.scan(scanProperties, onScanCompleted);
     63 });
     64 
     65 chrome.permissions.contains(permissionObj, function(result) {
     66   if (result) {
     67     gotPermission();
     68   }
     69 });
     70