Project

General

Profile

Download (5.62 KB) Statistics
| Branch: | Revision:

hydrilla-fixes-bundle / src / drive-google-com-fix-download / google_drive_files.js @ ecc6c218

1
/**
2
 * SPDX-License-Identifier: LicenseRef-GPL-3.0-or-later-WITH-js-exceptions
3
 *
4
 * Make files on drive.google.com downloadable without nonfree js
5
 *
6
 * Copyright (C) 2021 Wojtek Kosior <koszko@koszko.org>
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * As additional permission under GNU GPL version 3 section 7, you
19
 * may distribute forms of that code without the copy of the GNU
20
 * GPL normally required by section 4, provided you include this
21
 * license notice and, in case of non-source distribution, a URL
22
 * through which recipients can access the Corresponding Source.
23
 * If you modify file(s) with this exception, you may extend this
24
 * exception to your version of the file(s), but you are not
25
 * obligated to do so. If you do not wish to do so, delete this
26
 * exception statement from your version.
27
 *
28
 * As a special exception to the GPL, any HTML file which merely
29
 * makes function calls to this code, and for that purpose
30
 * includes it by reference shall be deemed a separate work for
31
 * copyright law purposes. If you modify this code, you may extend
32
 * this exception to your version of the code, but you are not
33
 * obligated to do so. If you do not wish to do so, delete this
34
 * exception statement from your version.
35
 *
36
 * You should have received a copy of the GNU General Public License
37
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
38
 *
39
 * I, Wojtek Kosior, thereby promise not to sue for violation of this file's
40
 * license. Although I request that you do not make use of this code in a
41
 * proprietary program, I am not going to enforce this in court.
42
 */
43

    
44
/* Use with: https://drive.google.com/file/d/** */
45

    
46
const og = {};
47

    
48
for (const node of document.head.childNodes) {
49
    if (node.tagName === "STYLE") {
50
	document.head.removeChild(node);
51
	continue;
52
    }
53

    
54
    if (node.tagName !== "META")
55
	continue;
56

    
57
    const match = /^og:(.+)/.exec(node.getAttribute("property"));
58
    if (!match)
59
	continue;
60

    
61
    og[match[1]] = node.getAttribute("content");
62
}
63

    
64
const match = new RegExp("/file/d/([^/]+)").exec(document.URL);
65
const file_id = match && match[1];
66

    
67
/* Extract file's mime type from script. */
68

    
69
let mime_type;
70

    
71
for (const script of document.scripts) {
72
    const match = /itemJson\s*:\s*(\[.*\])\s*}\s*;$/.exec(script.textContent);
73
    if (!match)
74
	continue;
75

    
76
    let data;
77
    try {
78
	data = JSON.parse(match[1]);
79
    } catch(e) {
80
	console.log(e);
81
	continue;
82
    }
83

    
84
    if (/^[^\/]+\/[^\/]+$/.test(data[11]))
85
	mime_type = data[11];
86
}
87

    
88
/* If file is folder, redirect to its page. */
89

    
90
const redirect = file_id &&
91
      /^application\/vnd.google-apps.(folder|shortcut)$/.test(mime_type);
92
if (redirect)
93
    window.location.href = `https://drive.google.com/drive/folders/${file_id}`;
94

    
95
const download_link =
96
      file_id && `https://drive.google.com/uc?export=download&id=${file_id}`;
97

    
98
const body = document.createElement("body");
99
const name_div = document.createElement("div");
100
const download_div = document.createElement("div");
101
const download_button = document.createElement("a");
102
const type_div = document.createElement("div");
103
const type_span = document.createElement("span");
104
const show_image_div = document.createElement("div");
105
const show_image_button = document.createElement("button");
106
const image = document.createElement("img");
107

    
108

    
109
let button_text = "download";
110

    
111
if (!og.title)
112
    button_text += " file";
113
else
114
    name_div.textContent = og.title;
115

    
116
name_div.setAttribute("style", "font-weight: bold; display:inline-block;");
117

    
118

    
119
if (download_link)
120
    download_button.setAttribute("href", download_link);
121
else
122
    button_text += " (unavailable)";
123

    
124
download_button.textContent = button_text;
125
download_button.setAttribute("style", "border-radius: 5px; padding: 10px; color: black; background-color: lightgreen; text-decoration: none; box-shadow: -4px 8px 8px #888;");
126

    
127

    
128
download_div.appendChild(download_button);
129
download_div.setAttribute("style", "padding: 10px; display: inline-block; margin: 0 0 10px;");
130

    
131

    
132
type_span.textContent = "type: ";
133
type_span.setAttribute("style", "font-weight: bold; display:inline-block; white-space: pre;");
134

    
135
type_div.setAttribute("style", "margin: 0 0 10px;");
136
type_div.appendChild(type_span);
137
if (mime_type || og.type)
138
    type_div.append(mime_type || og.type);
139

    
140

    
141
function show_image()
142
{
143
    const image = document.createElement("img");
144
    image.setAttribute("src", og.image);
145
    image.setAttribute("width", og["image:width"]);
146
    image.setAttribute("height", og["image:height"]);
147
    image.setAttribute("style", "border: 1px solid #eee;");
148

    
149
    document.body.replaceChild(image, show_image_div);
150
}
151

    
152
show_image_button.setAttribute("style", "border-radius: 5px; padding: 10px; color: black; background-color: lightgreen; box-shadow: -4px 8px 8px #888; font-family: inherit; font-size: inherit; border: none;");
153
show_image_button.textContent = "show image";
154
show_image_button.addEventListener("click", show_image);
155
show_image_div.appendChild(show_image_button);
156

    
157

    
158
body.setAttribute("style", "margin: 20px;");
159
if (og.title)
160
    body.appendChild(name_div);
161
body.appendChild(download_div);
162
if (og.type)
163
    body.appendChild(type_div);
164
if (og.image && og["image:width"] && og["image:height"])
165
    body.appendChild(show_image_div);
166

    
167
if (!redirect)
168
    document.documentElement.replaceChild(body, document.body);
(1-1/2)