1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
<!doctype html>
<html>
<head>
<link rel="stylesheet" href="chrome://resources/css/text_defaults.css">
<style>
body {
display: flex;
flex-direction: column;
margin: 0;
}
#header,
#footer {
display: flex;
flex: none;
}
#header {
align-items: flex-start;
padding-top: 15pt;
}
#footer {
align-items: flex-end;
padding-bottom: 15pt;
}
#content {
flex: auto;
}
.left {
flex: none;
padding-left: 24pt; /* csschecker-disable-line left-right */
padding-right: 6pt; /* csschecker-disable-line left-right */
}
.center {
flex: auto;
padding-left: 24pt; /* csschecker-disable-line left-right */
padding-right: 24pt; /* csschecker-disable-line left-right */
text-align: center;
}
.right {
flex: none;
/* historically does not account for RTL */
padding-left: 6pt; /* csschecker-disable-line left-right */
padding-right: 24pt; /* csschecker-disable-line left-right */
}
.grow {
flex: auto;
}
.text {
font-size: 8pt;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
</style>
<script>
function getComputedStyleAsFloat(style, value) {
return parseFloat(style.getPropertyValue(value).slice(0, -2));
}
function elementIntersects(element, topPos, bottomPos, leftPos, rightPos) {
const rect = element.getBoundingClientRect();
const style = window.getComputedStyle(element);
// Only consider the size of |element|, so remove the padding from |rect|.
// The padding is used for positioning.
rect.top += getComputedStyleAsFloat(style, 'padding-top');
rect.bottom -= getComputedStyleAsFloat(style, 'padding-bottom');
rect.left += getComputedStyleAsFloat(style, 'padding-left');
rect.right -= getComputedStyleAsFloat(style, 'padding-right');
return leftPos < rect.right && rightPos > rect.left && topPos < rect.bottom &&
bottomPos > rect.top;
}
function setupHeaderFooterTemplate(options) {
const body = document.querySelector('body');
const header = document.querySelector('#header');
const footer = document.querySelector('#footer');
body.style.width = `${options.width}px`;
body.style.height = `${options.height}px`;
header.style.height = `${options.topMargin}px`;
footer.style.height = `${options.bottomMargin}px`;
const topMargin = options.topMargin;
const bottomMargin = options.height - options.bottomMargin;
const leftMargin = options.leftMargin;
const rightMargin = options.width - options.rightMargin;
header.innerHTML = options['headerTemplate'] || `
<div id=-internal-print-header class='date text left'></div>
<div id=-internal-print-header class='title text center'></div>`;
footer.innerHTML = options['footerTemplate'] || `
<div id=-internal-print-footer class='url text left grow'></div>
<div id=-internal-print-page-number class='text right'>
<span class='pageNumber' id=-internal-print-page-number></span>/<span class='totalPages' id=-internal-print-page-number></span>
</div>`;
const date = new Date(options.date);
const formatter =
new Intl.DateTimeFormat(
navigator.languages[0].split('@')[0],
{ dateStyle: 'short', timeStyle: 'short' });
options.date = formatter.format(date);
for (const cssClass of ['date', 'title', 'url', 'pageNumber', 'totalPages']) {
for (const element of document.querySelectorAll(`.${cssClass}`)) {
element.textContent = options[cssClass];
}
}
for (const element of document.querySelectorAll(`.text`)) {
if (options.isRtl &&
!element.classList.contains('url') &&
!element.classList.contains('title')) {
element.dir = 'rtl';
}
if (elementIntersects(element, topMargin, bottomMargin, leftMargin,
rightMargin)) {
element.style.visibility = 'hidden';
}
}
}
</script>
</head>
<body>
<div id="header"></div>
<div id="content"></div>
<div id="footer"></div>
</body>
</html>
|