Skip to content

Instantly share code, notes, and snippets.

@schuyberg
Last active July 19, 2019 21:31
Show Gist options
  • Save schuyberg/794d78bd34a0661cb72d5833a2a83934 to your computer and use it in GitHub Desktop.
Save schuyberg/794d78bd34a0661cb72d5833a2a83934 to your computer and use it in GitHub Desktop.
add buttons to open articles and statblocks from d20pfsrd for easy saving / printing
// ==UserScript==
// @name d20pfsrd Statblockerer
// @match *://www.d20pfsrd.com/*
// @description adds buttons open a statblock from d20pfsrd.com in a new window to print or save
// @author schuyberg
// @grant none
// @version 0.1
// @updateURL https://gist.github.com/schuyberg/794d78bd34a0661cb72d5833a2a83934/raw/statblockerer.user.js
// @downloadURL https://gist.github.com/schuyberg/794d78bd34a0661cb72d5833a2a83934/raw/statblockerer.user.js
// @run-at document-end
// ==/UserScript==
(function() {
'use strict';
const article = document.getElementsByTagName('article')[0];
const h1 = article.getElementsByTagName('h1')[0];
const article_text = document.querySelector('.article-text');
const statblock = document.querySelector('.statblock');
const css = `.product-right, .section15, .ogn-childpages { display: none; }
.printblock {
display: block;
position: absolute;
top: 0;
left: 0;
display: block;
font-size: 10pt;
font-family: Arial, sans-serif;
margin:auto;
padding: 1em;
}
.divider {
border-bottom: 1px solid darkgray;
border-top: 1px solid darkgray;
}
.title {
border-bottom: 2px solid;
font-size: 1.15em;
font-weight: bold;
}
.extract-buttons {
display: none;
}
@media print {
.printblock: { margin: 0; max-width: none; padding: 0;}
}
`;
const extract_buttons = document.createElement("div");
extract_buttons.innerText = "Extract:";
extract_buttons.classList.add('extract-buttons');
extract_buttons.setAttribute("style", "color: gray; font-size: 0.8em; padding: 2px;");
article_text.prepend(extract_buttons);
addButton(article_text, "article");
addButton(statblock, "statblock");
function addButton(element, label) {
let button = document.createElement("button");
button.innerHTML = label;
extract_buttons.append(button);
button.addEventListener ("click", function() {
return extractBlock(element)
});
}
function extractBlock(selection) {
let theBlock = selection.cloneNode(true);
theBlock.classList.add('printblock');
let blockWindow = window.open('', 'Statblock', 'height=600,width=800');
blockWindow.document.write('<html><head><style>' + css + '</style></head><body></body></html>');
blockWindow.document.body.appendChild(theBlock);
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment