Direct File for Google Classroom

Directly Download the Files in Google Classroom using Ctrl Click

目前为 2024-12-10 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Direct File for Google Classroom
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.4.1
  5. // @description Directly Download the Files in Google Classroom using Ctrl Click
  6. // @author You
  7. // @match https://classroom.google.com/*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=google.com
  9. // @grant none
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. (function () {
  14.  
  15. 'use strict';
  16. // Your code here...
  17.  
  18. const M_HREF = "https://drive.google.com/file/d/"
  19.  
  20. const linkElmOnClick = function (evt) {
  21.  
  22. // const linkElm = this;
  23. evt.stopPropagation();
  24. evt.stopImmediatePropagation();
  25.  
  26. };
  27.  
  28. let pageChangedFlag = false;
  29.  
  30. const onPageChanged = () => {
  31. if (!pageChangedFlag) return;
  32.  
  33. const linkSelector = `[href*="${M_HREF}"]:not([data-ozhref])`;
  34. if (!document.querySelector(linkSelector)) return;
  35.  
  36. const links = document.querySelectorAll(linkSelector);
  37. for (const linkElm of links) {
  38.  
  39. const orhref = linkElm.href;
  40.  
  41. linkElm.dataset.ozhref = orhref;
  42.  
  43. const mres = orhref.match(/https:\/\/drive\.google\.com\/file\/d\/([0-9a-zA-Z\-_+.·]+)\/\w+/);
  44. // id should be /[0-9a-zA-Z\-_+]+/ in general
  45. if (!mres) continue;
  46.  
  47. const dfileId = `${mres[1]}`;
  48.  
  49. // let newHref=`https://drive.google.com/u/1/uc?id=${mres[1]}&export=download`;
  50.  
  51. let uo = null;
  52. try {
  53. uo = new URL(orhref);
  54. } catch (e) { }
  55. if (!uo) continue;
  56.  
  57. const uo2 = new URL(`https://drive.google.com/uc?export=download&id=${dfileId}`);
  58.  
  59. uo.searchParams.forEach((value, key) => {
  60. if (key === 'export' || key === 'id') return;
  61. uo2.searchParams.set(key, value);
  62. });
  63. const newHref = uo2.toString();
  64.  
  65. if (!newHref || typeof newHref !== 'string') continue;
  66. // console.log(orhref, newHref)
  67. // linkElm.setAttribute('href',newHref)
  68.  
  69. // let newHref = `https://drive.google.com/uc?export=download&id=${dfileId}&usp=drive_web&authuser=2`
  70. linkElm.setAttribute('href', newHref);
  71.  
  72. linkElm.setAttribute('target', '_blank');
  73.  
  74. linkElm.addEventListener('click', linkElmOnClick, true);
  75.  
  76. }
  77.  
  78. };
  79.  
  80. new MutationObserver(() => {
  81. pageChangedFlag = true;
  82. Promise.resolve().then(onPageChanged);
  83. }).observe(document, { subtree: true, childList: true });
  84.  
  85.  
  86. try {
  87. // trigger onPageChanged
  88. document.documentElement.appendChild(document.createElement('noscript'));
  89. } catch (e) { }
  90.  
  91. })();