Greasy Fork 支持简体中文。

Download protected PDF file from Google Drive

You can download protected PDF file

  1. // ==UserScript==
  2. // @name Download protected PDF file from Google Drive
  3. // @namespace Download protected PDF file
  4. // @description You can download protected PDF file
  5. // @version 1.1
  6. // @match https://drive.google.com/*
  7. // @grant GM_registerMenuCommand
  8. // @require https://unpkg.com/jspdf@latest/dist/jspdf.umd.min.js
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13. function rescale(width, height, fitWidth, fitHeight) {
  14. let ratio = width / height;
  15. let fitRatio = fitWidth / fitHeight;
  16. if (ratio <= fitRatio) {
  17. return [width, width / fitRatio];
  18. } else {
  19. return [height * fitRatio, height];
  20. }
  21. }
  22.  
  23. function imageToBase64(img) {
  24. let canvas = document.createElement("canvas");
  25. let context = canvas.getContext("2d");
  26. canvas.width = img.naturalWidth;
  27. canvas.height = img.naturalHeight;
  28. context.drawImage(img, 0, 0, img.naturalWidth, img.naturalHeight);
  29. return canvas.toDataURL("image/png", 1.0);
  30. }
  31.  
  32. function downloadPDF() {
  33. try {
  34. let jsPDF = window.jspdf.jsPDF;
  35. let pdf = new jsPDF();
  36. let pdfWidth = pdf.internal.pageSize.getWidth();
  37. let pdfHeight = pdf.internal.pageSize.getHeight();
  38. let elements = document.getElementsByTagName("img");
  39. for (let img of elements) {
  40. if (!/^blob:/.test(img.src)) {
  41. continue;
  42. }
  43. console.log("adding image", img.src);
  44. let imgData = imageToBase64(img);
  45. let [newWidth, newHeight] = rescale(pdfWidth, pdfHeight, img.naturalWidth, img.naturalHeight);
  46. pdf.addImage(imgData, "png", 0, 0, newWidth, newHeight);
  47. pdf.addPage();
  48. }
  49. pdf.deletePage(pdf.internal.getNumberOfPages());
  50. pdf.save("download.pdf");
  51. } catch(e) {
  52. console.log(e);
  53. }
  54. }
  55.  
  56. GM_registerMenuCommand("Download PDF file", downloadPDF, "d");
  57. })();