~stick/print-xkcd

ref: 1f1bfe369ab1659935edb2a1a451de83c24f1aa7 print-xkcd/html/index.html -rw-r--r-- 2.5 KiB
1f1bfe36Stick first try 1 year, 2 months ago
                                                                                
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
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="icon" href="logo.svg"/>
    <script>
      function updateComic() {
        const comicNum = document.getElementById('comicNum').value;
        fetch('https://hackwhacktackbang.fyi/xkcd-proxy/' + comicNum).then(response => {
          return response.text()
        }).then(text => {
          const xkcd = new DOMParser().parseFromString(text, 'text/html');

          // Get the page title
          const ctitle = xkcd.getElementById('ctitle').innerHTML;
          document.title = 'print xkcd: ' + ctitle;
          document.getElementById('ctitle').innerHTML = ctitle;

          // Get the comic image
          const img = xkcd.getElementById('comic').innerHTML;
          document.getElementById('comic').innerHTML = img;

          // Get the mouseover
          const mouseover = document.getElementsByTagName('img')[0].getAttribute('title');
          document.getElementById('mouseover').innerHTML = mouseover;
        })

        // Set the link
        document.getElementById('link').innerHTML = 'https://xkcd.com/' + comicNum
        return false;
      }
    </script>
    <style>
      html {
        background-color: #96a8c8;
      }
      body {
        align-items: center;
        background-color: #ffff;
        display: flex;
        flex-direction: column;
        margin: 0 auto;
        max-width: 60em;
        min-height: 100vh;
        width: 100vw;
      }
      body > * {
        margin: 1rem 0;
      }
      h2 {
        margin: 0;
      }
      #link {
        padding: 0.3em;
      }
      main {
        align-items: center;
        display: flex;
        flex-direction: column;
        text-align: center;
      }
      #mouseover {
        background-color: #eee;
        border: 1px solid black;
        font-size: 0.9em;
        max-width: 60%;
        padding: 0.2em;
      }
      @media print {
        h1, form, button {
          display: none;
        }
      }
    </style>
    <title>print xkcd</title>
  </head>
  <body>
    <h1>print xkcd</h1>
    <main>
      <h2 id='ctitle'></h2>
      <div id="link"></div>
      <div id="comic"></div>
      <div id="mouseover"></div>
    </main>
    <form onsubmit="return updateComic();">
      <label for="comicNum">Comic Number:</label>
      <input id="comicNum" type="text" value="1234">
      <input type="submit" value="Update">
    </form>
    <button type="button" onclick="window.print(); return false;">Print</button>
  </body>
</html>