retroman

Untitled

Jun 18th, 2025
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Google Drive True One‑Click Download
  3. // @namespace    https://example.com
  4. // @version      1.0
  5. // @description  Automatically handles Google Drive’s virus‑scan/confirm token so you only click once.
  6. // @match        https://drive.google.com/file/d/*
  7. // @match        https://drive.google.com/uc?export=download&*id=*
  8. // @grant        GM_xmlhttpRequest
  9. // @connect      google.com
  10. // @connect      docs.google.com
  11. // @run-at       document-start
  12. // ==/UserScript==
  13.  
  14. (function() {
  15.   const url = new URL(location.href);
  16.   let fileId = null;
  17.  
  18.   // Extract ID from /file/d/ID/…
  19.   const m = url.pathname.match(/\/file\/d\/([^\/]+)/);
  20.   if (m) fileId = m[1];
  21.  
  22.   // Or from ?id=…
  23.   if (!fileId && url.searchParams.has('id')) {
  24.     fileId = url.searchParams.get('id');
  25.   }
  26.  
  27.   if (!fileId) return; // nothing to do
  28.  
  29.   // Base download URL (this gives the confirmation page for large files)
  30.   const base = `https://docs.google.com/uc?export=download&id=${fileId}`;
  31.  
  32.   // Fetch it via GM_xmlhttpRequest (bypasses CORS)
  33.   GM_xmlhttpRequest({
  34.     method: 'GET',
  35.     url: base,
  36.     onload(res) {
  37.       let href;
  38.  
  39.       // 1) If Google returned a direct download link, it'll be in a meta-refresh:
  40.       const meta = res.responseText.match(/<meta http-equiv="refresh" content="0;url=(.*?)">/);
  41.       if (meta) {
  42.         href = meta[1];
  43.       } else {
  44.         // 2) Otherwise look for the “confirm=TOKEN” link in the HTML
  45.         const link = res.responseText.match(/href="(\/uc\?export=download&amp;confirm=([0-9A-Za-z_-]+)&amp;id=[^"]+)"/);
  46.         if (link) {
  47.           href = link[1].replace(/&amp;/g, '&');
  48.         }
  49.       }
  50.  
  51.       // 3) Fallback: if neither found, just use `confirm=t`
  52.       if (!href) {
  53.         href = `${base}&confirm=t`;
  54.       } else if (href.startsWith('/')) {
  55.         href = 'https://docs.google.com' + href;
  56.       }
  57.  
  58.       // Redirect only if different
  59.       if (location.href !== href) {
  60.         window.location.replace(href);
  61.       }
  62.     },
  63.     onerror() {
  64.       // If fetch fails, at least try the simple URL
  65.       window.location.replace(`${base}&confirm=t`);
  66.     }
  67.   });
  68. })();
  69.  
Add Comment
Please, Sign In to add comment