Download protected PDF file from Google Drive

You can download protected PDF file

目前為 2024-04-22 提交的版本,檢視 最新版本

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

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址