Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Google Drive True One‑Click Download
- // @namespace https://example.com
- // @version 1.0
- // @description Automatically handles Google Drive’s virus‑scan/confirm token so you only click once.
- // @match https://drive.google.com/file/d/*
- // @match https://drive.google.com/uc?export=download&*id=*
- // @grant GM_xmlhttpRequest
- // @connect google.com
- // @connect docs.google.com
- // @run-at document-start
- // ==/UserScript==
- (function() {
- const url = new URL(location.href);
- let fileId = null;
- // Extract ID from /file/d/ID/…
- const m = url.pathname.match(/\/file\/d\/([^\/]+)/);
- if (m) fileId = m[1];
- // Or from ?id=…
- if (!fileId && url.searchParams.has('id')) {
- fileId = url.searchParams.get('id');
- }
- if (!fileId) return; // nothing to do
- // Base download URL (this gives the confirmation page for large files)
- const base = `https://docs.google.com/uc?export=download&id=${fileId}`;
- // Fetch it via GM_xmlhttpRequest (bypasses CORS)
- GM_xmlhttpRequest({
- method: 'GET',
- url: base,
- onload(res) {
- let href;
- // 1) If Google returned a direct download link, it'll be in a meta-refresh:
- const meta = res.responseText.match(/<meta http-equiv="refresh" content="0;url=(.*?)">/);
- if (meta) {
- href = meta[1];
- } else {
- // 2) Otherwise look for the “confirm=TOKEN” link in the HTML
- const link = res.responseText.match(/href="(\/uc\?export=download&confirm=([0-9A-Za-z_-]+)&id=[^"]+)"/);
- if (link) {
- href = link[1].replace(/&/g, '&');
- }
- }
- // 3) Fallback: if neither found, just use `confirm=t`
- if (!href) {
- href = `${base}&confirm=t`;
- } else if (href.startsWith('/')) {
- href = 'https://docs.google.com' + href;
- }
- // Redirect only if different
- if (location.href !== href) {
- window.location.replace(href);
- }
- },
- onerror() {
- // If fetch fails, at least try the simple URL
- window.location.replace(`${base}&confirm=t`);
- }
- });
- })();
Add Comment
Please, Sign In to add comment