aboutsummaryrefslogtreecommitdiff
path: root/phttps___docs.google.com_spreadsheets_d___.json
blob: ee2aad3faf7d82fe3e11538f85db2972aa513817 (about) (plain)
1
[{"sgoogle sheets download":{"url":"","hash":"","text":"/**\n * Copyright 2021 Wojtek Kosior\n *\n * This program is free software; you can redistribute it\n * and/or modify it under the terms of either:\n * - the GNU General Public License as published by the Free\n *   Software Foundation; either version 3 of the License, or (at\n *   your option) any later version, or\n * - the \"A\" license: <https://koszko.org/alicense.txt>; explained\n *   at: <https://koszko.org/en/articles/my-new-license.html>\n *\n * As additional permission under GNU GPL version 3 section 7, you\n * may distribute forms of that code without the copy of the GNU\n * GPL normally required by section 4, provided you include this\n * license notice and, in case of non-source distribution, a URL\n * through which recipients can access the Corresponding Source.\n * If you modify file(s) with this exception, you may extend this\n * exception to your version of the file(s), but you are not\n * obligated to do so. If you do not wish to do so, delete this\n * exception statement from your version. If you delete this\n * exception statement from all source files in the program, then\n * also delete it here.\n *\n * As a special exception to the GPL, any HTML file which merely\n * makes function calls to this code, and for that purpose\n * includes it by reference shall be deemed a separate work for\n * copyright law purposes. If you modify this code, you may extend\n * this exception to your version of the code, but you are not\n * obligated to do so. If you do not wish to do so, delete this\n * exception statement from your version.\n */\n\n\n/* Make the view scrollable. */\n\ndocument.body.setAttribute(\"style\",\n\t\t\t   \"width: 100vw; height: 100vh; overflow: scroll;\" +\n\t\t\t   (document.body.getAttribute(\"style\") || \"\"));\n\nlet container = document.querySelectorAll(\".waffle\")[0];\nlet main_gid = null;\n\nwhile (container) {\n    container = container.parentElement;\n    console.log(container);\n    if (container === document.body || !container)\n\tbreak;\n\n    const match = /([0-9]+)-grid-container/.exec(container.id);\n    if (match)\n\tmain_gid = match[1];\n\n    container.setAttribute(\"style\",\n\t\t\t   \"width: fit-content; width: -moz-fit-content;\");\n}\n\n\n/* Remove editor toolbars and bottom bar - these don't work anyway. */\n\nconst docs_chrome = document.getElementById(\"docs-chrome\");\nif (docs_chrome)\n    docs_chrome.remove()\nconst grid_bottom_bar = document.getElementById(\"grid-bottom-bar\");\nif (grid_bottom_bar)\n    grid_bottom_bar.remove()\n\n\n/* Remove no Javascript warning. */\n\nfor (const no_js_warning of document.querySelectorAll(\"noscript\"))\n    no_js_warning.remove();\n\n\n/* Get opengraph data. */\n\nconst og = {};\n\nfor (const node of document.head.childNodes) {\n    if (node.tagName === \"STYLE\") {\n\tdocument.head.removeChild(node);\n\tcontinue;\n    }\n\n    if (node.tagName !== \"META\")\n\tcontinue;\n\n    const match = /^og:(.+)/.exec(node.getAttribute(\"property\"));\n    if (!match)\n\tcontinue;\n\n    og[match[1]] = node.getAttribute(\"content\");\n}\n\n\n/* Construct download link. */\n\nlet download_link = null;\n\nconst match = new RegExp(\"/spreadsheets/d/([^/]+)\").exec(document.URL);\nif (match)\n    download_link = `https://docs.google.com/spreadsheets/d/${match[1]}/export`;\n\n\n/* Add title bar with sheet name and download button. */\n\nconst title_bar = document.createElement(\"div\");\nconst title_heading = document.createElement(\"h1\");\nconst title_text = document.createElement(\"span\");\nconst main_download_button = document.createElement(\"a\");\n\nmain_download_button.textContent = \"download\";\nmain_download_button.setAttribute(\"style\", \"border-radius: 10px; padding: 20px; color: #333; background-color: lightgreen; text-decoration: none; box-shadow: -4px 8px 8px #888; display: inline-block;\");\n\nif (og.title) {\n    title_text.textContent = og.title;\n    title_heading.appendChild(title_text);\n}\n\ntitle_text.setAttribute(\"style\", \"margin-right: 10px;\");\n\nif (download_link) {\n    main_download_button.setAttribute(\"href\", download_link);\n    title_heading.appendChild(main_download_button);\n}\n\ntitle_bar.setAttribute(\"style\", \"padding: 0 20px; color: #555;\");\n\ntitle_bar.appendChild(title_heading);\n\ndocument.body.insertBefore(title_bar, document.body.firstElementChild);\n\n\n/* Extract sheet data from a script that sets the `bootstrapData' variable. */\n\nlet data = null;\nfor (const script of document.scripts) {\n    const match = /bootstrapData = ({([^;]|[^}];)+})/.exec(script.textContent);\n    if (!match)\n\tcontinue;\n    data = JSON.parse(match[1]);\n}\n\n/*\n * Add download buttons for individual sheets belonging to this spreadsheet.\n * Data schema has been observed by looking at various spreadsheets.\n */\n\nfunction add_sheet_download(data)\n{\n    if (!Array.isArray(data))\n\treturn;\n\n    const gid = data[2];\n    if (![\"string\", \"number\"].includes(typeof gid))\n\treturn;\n\n    const sheet_download_link = `${download_link}?gid=${gid}`;\n    const sheet_download_button = document.createElement(\"a\");\n\n    sheet_download_button.setAttribute(\"style\", \"border-radius: 5px; padding: 10px; color: #333; background-color: lightgreen; text-decoration: none; box-shadow: -4px 8px 8px #888; display: inline-block; margin: 0 5px 5px 0;\");\n    sheet_download_button.setAttribute(\"href\", sheet_download_link);\n\n    let sheet_name = null;\n    if (Array.isArray(data[3]) &&\n\tdata[3][0] && typeof data[3][0] === \"object\"\n\t&& Array.isArray(data[3][0][1]) &&\n\tArray.isArray(data[3][0][1][0]) &&\n\ttypeof data[3][0][1][0][2] === \"string\") {\n\n\tconst sheet_name = data[3][0][1][0][2];\n\tsheet_download_button.textContent = sheet_name;\n        if (gid == main_gid)\n\t    title_text.textContent = `${title_text.textContent} - ${sheet_name}`;\n    } else {\n\tsheet_download_button.textContent = `<sheet gid=${gid}>`;\n    }\n\n    title_bar.appendChild(sheet_download_button);\n}\n\nif (download_link) {\n    for (const entry of data.changes.topsnapshot) {\n\tif (!Array.isArray(entry) || entry[0] !== 21350203 ||\n\t    typeof entry[1] !== \"string\")\n\t    continue;\n\n\tlet entry_data = null;\n\n\ttry {\n\t    entry_data = JSON.parse(entry[1]);\n\t} catch (e) {\n\t    console.log(e);\n\t    continue;\n\t}\n\n\tadd_sheet_download(entry_data);\n    }\n}\n"}},{"phttps://docs.google.com/spreadsheets/d/**":{"components":["s","google sheets download"],"allow":false}}]