Skip to content

Instantly share code, notes, and snippets.

@cmargroff
Last active July 29, 2017 21:34
Show Gist options
  • Save cmargroff/66a327d60bd53fa87e318959419af76c to your computer and use it in GitHub Desktop.
Save cmargroff/66a327d60bd53fa87e318959419af76c to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Pkmn Cards Proxy Print Layout
// @namespace https://pkmncards.com/
// @version 0.2.2
// @description Format print layout better
// @author Chris Margroff
// @match https://pkmncards.com/proxy/*
// @grant none
// ==/UserScript==
(function(d){
function getQueryVariable(variable) {
var query = window.location.search.substring(1);
var vars = query.split('&');
for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split('=');
if (decodeURIComponent(pair[0]) == variable) {
return decodeURIComponent(pair[1]);
}
}
return false;
}
var set = getQueryVariable('set');
if (set) {
set = set.split(',');
var ids = [];
for (var i = 0; i < set.length; i++) {
var card = set[i].split(':');
for (var j = 0; j < card[1]; j++) {
ids.push(card[0]);
}
}
document.cookie = "pkmn_proxy="+ids.join('%2C')+";Path=/";
window.location = window.location.origin+window.location.pathname;
}else{
draw();
}
function draw(){
var styleEl = d.createElement('style');
var style = `
.page{
width: 8.5in;
height: 11in;
display: flex;
justify-content: space-around;
flex-wrap: wrap;
align-items: center;
padding: 0.2in 0.4in;
box-sizing: border-box;
}
.page img{
width: 63mm;
height: 88mm;
}
@media not print{
body{
margin: 20px;
}
.page{
margin: 0 auto;
-webkit-box-shadow: 0 0 10px 0 rgba(0,0,0,0.1);
box-shadow: 0 0 10px 0 rgba(0,0,0,0.1);
}
.page + .page{
margin-top: 50px;
}
}
@media only print{
}
`;
styleEl.innerHTML = style;
d.head.appendChild(styleEl);
var cards = d.getElementsByClassName('wp-post-image');
var sources = [];
var deletes = [];
for (var i = 0; i < cards.length; i++) {
var card = cards[i];
var srcs = card.srcset.split(',');
var src = srcs.pop().replace(/\s\d*w/, '');
deletes.push(card.parentElement.href);
sources.push(src);
}
d.body.innerHTML = "";
var pageCount = Math.ceil(sources.length/9);
var pages = [];
for (var i = 0; i < pageCount; i++) {
pages[i] = d.createElement('div');
pages[i].className = "page";
d.body.appendChild(pages[i]);
for (var j = i*9; j < i*9+9; j++) {
if (sources[j]) {
var link = d.createElement('a');
link.href = deletes[j];
link.title = "Delete";
var img = new Image();
img.src = sources[j];
link.appendChild(img);
pages[i].appendChild(link);
}
}
}
}
}(document));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment