Alldatasheet.com: Single-click PDF access

On the search results page, clicking on the PDF icon will jump you straight to PDF view.

当前为 2016-06-12 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Alldatasheet.com: Single-click PDF access
  3. // @description On the search results page, clicking on the PDF icon will jump you straight to PDF view.
  4. // @namespace giferrari.net
  5. // @include http://*.alldatasheet.com/*
  6. // @version 3
  7. // @grant none
  8. // @require http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.js
  9. // ==/UserScript==
  10.  
  11. // Grab all links that appear to redirect to a PDF page.
  12. // There isn't a class that we can key off of, so we look for all
  13. // anchors that have a PDF icon in them.
  14. var datasheetPdfLinks = $('img[src="http://other.alldatasheet.com/etc/electronic_parts_datasheet.gif"]').closest('a');
  15.  
  16. // Open in same window instead of a new one.
  17. datasheetPdfLinks.attr('target', null);
  18.  
  19. // Compute path to the PDF view page and go there directly.
  20. // I don't know how to compute the path to the PDF itself,
  21. // so we need to load the normal view page first.
  22. datasheetPdfLinks.attr('href', function(i, oldHref) {
  23. // From:
  24. // http://www.alldatasheet.com/datasheet-pdf/pdf/317775/COMSET/2N2222.html
  25. // To:
  26. //http://pdf1.alldatasheet.com/datasheet-pdf/view/317775/COMSET/2N2222.html
  27. return oldHref
  28. .replace(/^http:\/\/www.alldatasheet.com/, 'http://pdf1.alldatasheet.com')
  29. .replace(/\/pdf\//, '/view/');
  30. });
  31.  
  32. // This script also runs on the PDF page itself.
  33. // If there's a PDF on this page, navigate to it directly.
  34. var pdfIframe = $('iframe[src$=".pdf"');
  35. var pdfUrl = pdfIframe.attr('src');
  36. pdfIframe.attr('src', ''); // Hopefully prevent waste of bandwidth, we're about to go there anyway.
  37. // Use a setTimeout to let the ads load; we're not monsters.
  38. setTimeout(function() {
  39. if (pdfUrl) {
  40. window.location.replace(pdfUrl);
  41. }
  42. }, 1000);