Files
scripts/js/rotate-datacard-backs.user.js
T

82 lines
2.3 KiB
JavaScript

// ==UserScript==
// @name Rotate Datasheet Card Backs for Printing
// @namespace local.print.helper
// @version 3.0
// @description Adds a button to the print sidebar that rotates the outer wrapper of every unit-card-back card 180 degrees
// @match *://game-datacards.eu/*
// @grant none
// @run-at document-idle
// ==/UserScript==
(function () {
'use strict';
function rotateCardBacks() {
var wrappers = document.querySelectorAll('.unit-card-back-wrapper');
var count = 0;
wrappers.forEach(function (wrapper) {
var outer = wrapper.parentElement;
if (!outer) return;
var current = outer.style.rotate;
if (current === '180deg') {
outer.style.rotate = '0deg';
} else {
outer.style.rotate = '180deg';
}
count++;
});
console.log('Rotated ' + count + ' card back(s).');
return count;
}
function createButton() {
var button = document.createElement('button');
button.id = 'rotate-card-backs-button';
button.textContent = 'Rotate card backs';
button.style.display = 'block';
button.style.width = '100%';
button.style.marginTop = '10px';
button.style.padding = '8px 12px';
button.style.background = '#381a3a';
button.style.color = 'white';
button.style.border = 'none';
button.style.borderRadius = '4px';
button.style.cursor = 'pointer';
button.style.fontFamily = 'sans-serif';
button.style.fontSize = '13px';
button.addEventListener('click', rotateCardBacks);
return button;
}
function tryAddButton() {
var sidebar = document.querySelector('.print-settings-scroll');
if (!sidebar) return false;
// Avoid adding the button more than once.
if (document.getElementById('rotate-card-backs-button')) return true;
sidebar.appendChild(createButton());
console.log('Rotate card backs button added to the sidebar.');
return true;
}
// Try right away in case the sidebar is already there.
if (tryAddButton()) return;
// Otherwise watch the page until the sidebar appears.
var observer = new MutationObserver(function () {
if (tryAddButton()) {
observer.disconnect();
}
});
observer.observe(document.documentElement, {
childList: true,
subtree: true
});
})();