Last active
May 21, 2023 23:35
-
-
Save wicket-quest/f0b9edd43f9a804d1c5137c53be137ae to your computer and use it in GitHub Desktop.
Stack Exchange Export as Google Document. Adapted from https://github.com/Glorfindel83/SE-Userscripts/blob/master/archivist/archivist.user.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Export Stack Exchange post as Google Document | |
// @namespace https://github.com/Glorfindel83/ | |
// @description Adds a button to archive all external links and images in a post | |
// @author Wicket | |
// @updateURL https://gist.github.com/wicket-quest/f0b9edd43f9a804d1c5137c53be137ae/raw/c8fa557407aa1df8c90e15527e3e098aae77b7b1/exportAsGDoc.user.js | |
// @downloadURL https://gist.github.com/wicket-quest/f0b9edd43f9a804d1c5137c53be137ae/raw/c8fa557407aa1df8c90e15527e3e098aae77b7b1/exportAsGDoc.user.js | |
// @version 0.2.3 | |
// @match *://*.stackexchange.com/questions/* | |
// @match *://*.stackoverflow.com/questions/* | |
// @match *://*.superuser.com/questions/* | |
// @match *://*.serverfault.com/questions/* | |
// @match *://*.askubuntu.com/questions/* | |
// @match *://*.stackapps.com/questions/* | |
// @match *://*.mathoverflow.net/questions/* | |
// @exclude *://*.stackexchange.com/questions/ask | |
// @exclude *://*.stackoverflow.com/questions/ask | |
// @exclude *://*.superuser.com/questions/ask | |
// @exclude *://*.serverfault.com/questions/ask | |
// @exclude *://*.askubuntu.com/questions/ask | |
// @exclude *://*.stackapps.com/questions/ask | |
// @exclude *://*.mathoverflow.net/questions/ask | |
// @connect script.google.com | |
// @connect script.googleusercontent.com | |
// @require https://greasemonkey.github.io/gm4-polyfill/gm4-polyfill.js | |
// @grant GM_xmlhttpRequest | |
// @grant GM.xmlHttpRequest | |
// ==/UserScript== | |
/** | |
* Export to Google Docs companion (https://stackapps.com/q/9730/105187) | |
* | |
* This userscript add the "Export" button next to post buttons at the bottom of the post. | |
* Clicking on this button will open a web application created using Google Apps Script | |
* in a new window. If the user have not signed-in in a Google account and authorized | |
* the app, it will show a button to sign-in and authorize. If the user is already signed or | |
* after the user signs-in by clicking but and have already authorized the app, it will | |
* export the post to the user's My Drive (Google Drive root folder for the user). | |
* | |
* At this time the authorization is handled using Google Cloud standard project set for | |
* testing purposes. This means that the testers should be registered and the number of | |
* testers is limited to 100. | |
* | |
* This is offered as is. Use under your own risk. | |
*/ | |
/** | |
* Default values for commom parameters | |
*/ | |
var SETTINGS = {}; | |
/** | |
* 0 -> Plain text | |
* 1 -> HTML file format. Markdown is converted into HTML using Showdown JavaScript library. | |
* 2 -> Google Document file format. Markdown is first converted into HTML using Showdown | |
JavaScript library, then the file is converted into Google Document file format using Google Drive API. | |
*/ | |
SETTINGS.option = 2; | |
/** | |
* true -> Puts the content of all the posts into a single file, in other words, creates only on file. | |
* false -> Puts the content of each post into a single file, in other words, creates multiple files, one for each post. | |
*/ | |
SETTINGS.singlepage = true; | |
(function () { | |
"use strict"; | |
$("a.js-share-link").each(function() { | |
let shareButton = $(this); | |
// Find links & images | |
let post = shareButton.parents("div.question")[0]; | |
if (post == null) { | |
post = shareButton.parents("div.answer")[0]; | |
} | |
let menu = shareButton.parent().parent(); | |
let body = $(post).find("div.js-post-body")[0]; | |
let disabled = false; | |
let hoverMessage = 'Export to Google Drive as ' + SETTINGS.option === 0 ? 'Plain Text file' : SETTINGS.option === 1 ? 'HTML file' : SETTINGS.option === 3 ? 'Google Document file' : 'Plain Text file'; | |
let button = $('<button class="s-btn s-btn__link" type="button" href="#" style="' + (disabled ? "color: #BBB" : "") + '" title="' + hoverMessage + '">Export</button>'); | |
let cell = $('<div class="flex--item"></div>'); | |
cell.append(button); | |
menu.append(cell); | |
/** | |
* | |
*/ | |
function startExporting(event) { | |
event.preventDefault(); | |
// TODO: overview of age of last snapshots, with checkmarks | |
if (!confirm('Are you sure you want to export this post?')) | |
return; | |
// Disable further clicks - the button becomes a progress indicator | |
button.off('click', startExporting); | |
button.on('click', function(e) { e.preventDefault(); }); | |
button.css("color", "#BBB"); | |
button.removeAttr("title"); | |
button.text("exporting ..."); | |
let shareButton = button.closest('.js-post-menu'); | |
let postId = shareButton.data('postId'); | |
console.log(postId); | |
/** | |
* Modified | |
*/ | |
function exportToDrive() { | |
let parts = location.host.split('.'); | |
let site = parts <= 3 | |
? parts[0] | |
: location.host.slice(0,location.host.indexOf('.',5)); | |
// Call Export to Google Docs app | |
let archiveLink = "https://script.google.com/macros/s/" | |
/** Google Apps Script ID */ + "AKfycbx6FVgi56a9loC1cWNlqgbFkCM86xtk6BtpOy3wioWGm6aS4p1pREg9LnKJY5pma5vVIQ" | |
/** Path sufix */ + "/exec" | |
/** Query string */ + "?" | |
/** Site parameter */ + "site=" + site | |
/** Post id paramenter */ + "&postId=" + postId | |
/** Output format parameter */ + "&option=" + SETTINGS.option | |
/** Simple page parameter */ + "&singlepage=" + SETTINGS.singlepage | |
// console.log(archiveLink); | |
window.open(archiveLink); | |
} | |
exportToDrive(); | |
// Update archive button | |
button.text("exporting tab opened") | |
} | |
button.on('click', startExporting); | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment