Home | History | Annotate | Download | only in download_links
      1 // Copyright (c) 2012 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 // This extension demonstrates using chrome.downloads.download() to
      6 // download URLs.
      7 
      8 var allLinks = [];
      9 var visibleLinks = [];
     10 
     11 // Display all visible links.
     12 function showLinks() {
     13   var linksTable = document.getElementById('links');
     14   while (linksTable.children.length > 1) {
     15     linksTable.removeChild(linksTable.children[linksTable.children.length - 1])
     16   }
     17   for (var i = 0; i < visibleLinks.length; ++i) {
     18     var row = document.createElement('tr');
     19     var col0 = document.createElement('td');
     20     var col1 = document.createElement('td');
     21     var checkbox = document.createElement('input');
     22     checkbox.checked = true;
     23     checkbox.type = 'checkbox';
     24     checkbox.id = 'check' + i;
     25     col0.appendChild(checkbox);
     26     col1.innerText = visibleLinks[i];
     27     col1.style.whiteSpace = 'nowrap';
     28     col1.onclick = function() {
     29       checkbox.checked = !checkbox.checked;
     30     }
     31     row.appendChild(col0);
     32     row.appendChild(col1);
     33     linksTable.appendChild(row);
     34   }
     35 }
     36 
     37 // Toggle the checked state of all visible links.
     38 function toggleAll() {
     39   var checked = document.getElementById('toggle_all').checked;
     40   for (var i = 0; i < visibleLinks.length; ++i) {
     41     document.getElementById('check' + i).checked = checked;
     42   }
     43 }
     44 
     45 // Download all visible checked links.
     46 function downloadCheckedLinks() {
     47   for (var i = 0; i < visibleLinks.length; ++i) {
     48     if (document.getElementById('check' + i).checked) {
     49       chrome.downloads.download({url: visibleLinks[i]},
     50                                              function(id) {
     51       });
     52     }
     53   }
     54   window.close();
     55 }
     56 
     57 // Re-filter allLinks into visibleLinks and reshow visibleLinks.
     58 function filterLinks() {
     59   var filterValue = document.getElementById('filter').value;
     60   if (document.getElementById('regex').checked) {
     61     visibleLinks = allLinks.filter(function(link) {
     62       return link.match(filterValue);
     63     });
     64   } else {
     65     var terms = filterValue.split(' ');
     66     visibleLinks = allLinks.filter(function(link) {
     67       for (var termI = 0; termI < terms.length; ++termI) {
     68         var term = terms[termI];
     69         if (term.length != 0) {
     70           var expected = (term[0] != '-');
     71           if (!expected) {
     72             term = term.substr(1);
     73             if (term.length == 0) {
     74               continue;
     75             }
     76           }
     77           var found = (-1 !== link.indexOf(term));
     78           if (found != expected) {
     79             return false;
     80           }
     81         }
     82       }
     83       return true;
     84     });
     85   }
     86   showLinks();
     87 }
     88 
     89 // Add links to allLinks and visibleLinks, sort and show them.  send_links.js is
     90 // injected into all frames of the active tab, so this listener may be called
     91 // multiple times.
     92 chrome.extension.onRequest.addListener(function(links) {
     93   for (var index in links) {
     94     allLinks.push(links[index]);
     95   }
     96   allLinks.sort();
     97   visibleLinks = allLinks;
     98   showLinks();
     99 });
    100 
    101 // Set up event handlers and inject send_links.js into all frames in the active
    102 // tab.
    103 window.onload = function() {
    104   document.getElementById('filter').onkeyup = filterLinks;
    105   document.getElementById('regex').onchange = filterLinks;
    106   document.getElementById('toggle_all').onchange = toggleAll;
    107   document.getElementById('download0').onclick = downloadCheckedLinks;
    108   document.getElementById('download1').onclick = downloadCheckedLinks;
    109 
    110   chrome.windows.getCurrent(function (currentWindow) {
    111     chrome.tabs.query({active: true, windowId: currentWindow.id},
    112                       function(activeTabs) {
    113       chrome.tabs.executeScript(
    114         activeTabs[0].id, {file: 'send_links.js', allFrames: true});
    115     });
    116   });
    117 };
    118