Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Downloading ALL pdf from links in a specific div/table element of a webpage [closed]

I have some link to PDFs given in a table inside a webpage, I want to download all of them via javascript, what do I do?

Edit:

Clearly speaking, I have a webpage here, I just look at it inside a browser, and I want to do some javascript that it automatically dwonloads all PDFs inside a specific table, that's all.

like image 521
Michael Lee Avatar asked Sep 03 '25 09:09

Michael Lee


1 Answers

Well finally I have got a way to do the task properly, see my fiddle if you want to look further at it. This script automatically do everything as required by my previous problem, and it forces pdf download without disabling the viewer.

code:

(function(ElementTypeToDownload, DownloadFromWhere) {

  // from http://stackoverflow.com/questions/3077242/force-download-a-pdf-link-using-javascript-ajax-jquery and http://stackoverflow.com/questions/16611497/how-can-i-get-the-name-of-an-html-page-in-javascript
  // Anonymous "self-invoking" function
  if (!(typeof jQuery === 'function')) {
    (function() {
      var startingTime = new Date().getTime();
      // Load the script
      var script = document.createElement("SCRIPT");
      script.src = 'https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js';
      script.type = 'text/javascript';
      document.getElementsByTagName("head")[0].appendChild(script);

      // Poll for jQuery to come into existance
      var checkReady = function(callback) {
        if (window.jQuery) {
          callback(jQuery);
        } else {
          window.setTimeout(function() {
            checkReady(callback);
          }, 20);
        }
      };

      // Start polling...
      checkReady(function($) {
        $(function() {
          var endingTime = new Date().getTime();
          var tookTime = endingTime - startingTime;
        });
      });
    })();
  }
  // http://facebook.com/anders.tornblad
  // [email protected]

  // from http://stackoverflow.com/questions/3077242/force-download-a-pdf-link-using-javascript-ajax-jquery
  function SaveToDisk(fileURL, fileName) {
    // for non-IE
    if (!window.ActiveXObject) {
      var save = document.createElement('a');
      save.href = fileURL;
      save.target = '_blank';
      save.download = fileName || fileURL.split("/").pop() || 'unknown';

      var evt = new MouseEvent('click', {
        'view': window,
        'bubbles': true,
        'cancelable': false
      });
      save.dispatchEvent(evt);

      (window.URL || window.webkitURL).revokeObjectURL(save.href);
    }

    // for IE < 11
    else if (!!window.ActiveXObject && document.execCommand) {
      var _window = window.open(fileURL, '_blank');
      _window.document.close();
      _window.document.execCommand('SaveAs', true, fileName || fileURL)
      _window.close();
    }
  }

  // from http://stackoverflow.com/questions/4623982/test-if-jquery-is-loaded-not-using-the-document-ready-event
  function preInit() {
    // wait until jquery is loeaded
    if (!(typeof jQuery === 'function')) {
      window.setTimeout(function() {
        //console.log(count++);
        preInit();
      }, 10); // Try again every 10 ms..
      return;
    }

    $(ElementTypeToDownload || prompt("Enter the type of element you are going to download, e.g. 'a' for link, 'img' or 'image' for images (do not add quotes, deafult is 'a') ") || 'a', DownloadFromWhere || prompt("jQuery Selector for the place that you want to download content from, e.g. '#DivName', '.ClassName', 'TagName', body by deafult") || 'body').each(function() {
      var tag = (ElementTypeToDownload == 'img' || ElementTypeToDownload == 'image') ? 'src' : 'href';
      SaveToDisk($(this).attr(tag));
    });
  }

  preInit();
})();

Anyway thank you for you guys helping

like image 147
Michael Lee Avatar answered Sep 04 '25 22:09

Michael Lee