|
| 1 | +// ==UserScript== |
| 2 | +// @id PDF_Tools@https://github.com/jerone/UserScripts |
| 3 | +// @name PDF Tools |
| 4 | +// @version 1.0 |
| 5 | +// @namespace https://github.com/jerone/UserScripts |
| 6 | +// @author jerone |
| 7 | +// @description |
| 8 | +// @include *.pdf |
| 9 | +// @include *.pdf?* |
| 10 | +// @include *.pdf#* |
| 11 | +// @run-at document-end |
| 12 | +// ==/UserScript== |
| 13 | + |
| 14 | +(function() { |
| 15 | + |
| 16 | + //console.log(PDFJS.version); // "1.0.277" |
| 17 | + |
| 18 | + var mimetype = "png"; |
| 19 | + |
| 20 | + var SecondaryToolbar = { |
| 21 | + opened: false, |
| 22 | + initialize: function secondaryToolbarInitialize() { |
| 23 | + this.toolbar = document.createElement("div"); |
| 24 | + this.toolbar.classList.add("secondaryToolbar", "doorHangerRight", "hidden"); |
| 25 | + this.toolbar.style.right = "180px"; |
| 26 | + document.getElementById("mainContainer").appendChild(this.toolbar); |
| 27 | + |
| 28 | + this.buttonContainer = document.createElement("div"); |
| 29 | + this.buttonContainer.classList.add("secondaryToolbarButtonContainer"); |
| 30 | + this.toolbar.appendChild(this.buttonContainer); |
| 31 | + |
| 32 | + this.attachEvents(); |
| 33 | + }, |
| 34 | + |
| 35 | + attachEvents: function() { |
| 36 | + /// https://github.com/mozilla/pdf.js/blob/2f5c6d6c3a75f9f44826c776dd356e2f786f35de/web/viewer.js#L2248 |
| 37 | + window.addEventListener("click", function click(evt) { |
| 38 | + if (SecondaryToolbar.opened && unsafeWindow.PDFView.container.contains(evt.target)) { |
| 39 | + SecondaryToolbar.close(); |
| 40 | + } |
| 41 | + }, false); |
| 42 | + /// https://github.com/mozilla/pdf.js/blob/2f5c6d6c3a75f9f44826c776dd356e2f786f35de/web/viewer.js#L2381 |
| 43 | + window.addEventListener("keydown", function keydown(evt) { |
| 44 | + if (SecondaryToolbar.opened && evt.keyCode === 27) { // esc; |
| 45 | + SecondaryToolbar.close(); |
| 46 | + } |
| 47 | + }); |
| 48 | + }, |
| 49 | + |
| 50 | + render: function() { |
| 51 | + console.log(unsafeWindow.PDFView.pages); |
| 52 | + console.log(unsafeWindow.PDFView.pages[0].draw); |
| 53 | + |
| 54 | + var pages = unsafeWindow.PDFView.pages; |
| 55 | + |
| 56 | + for (var i = 0, ii = pages.length; i < ii; i++) { |
| 57 | + var page = pages[i]; |
| 58 | + console.log(page, page.draw); |
| 59 | + var img = document.createElement("a"); |
| 60 | + img.classList.add("secondaryToolbarButton", "download"); |
| 61 | + img.dataset.pageIndex = i; |
| 62 | + img.setAttribute("download", "page" + page.id + "." + mimetype); |
| 63 | + img.setAttribute("title", "Download 'page" + page.id + "." + mimetype + "'"); |
| 64 | + img.style.display = "inline-block"; |
| 65 | + img.style.boxSizing = "border-box"; |
| 66 | + img.appendChild(document.createTextNode("Page " + page.id)); |
| 67 | + img.addEventListener("click", function() { |
| 68 | + var page = pages[this.dataset.pageIndex]; |
| 69 | + if (!page.canvas) { page.draw(); } |
| 70 | + this.href = page.canvas.toDataURL("image/" + mimetype); |
| 71 | + //window.open( page.canvas.toDataURL("image/" + mimetype)); |
| 72 | + }); |
| 73 | + //this.buttonContainer.appendChild(img); |
| 74 | + |
| 75 | + console.log(page.canvas, img, arguments); |
| 76 | + |
| 77 | + |
| 78 | + //if (!page.canvas) { page.draw(); } |
| 79 | + var img2 = document.createElement("img"); |
| 80 | + //img2.style.width = "16px"; |
| 81 | + img2.style.height = "16px"; |
| 82 | + img2.style.border = "1px solid red"; |
| 83 | + //img2.src = page.canvas.toDataURL("image/" + mimetype); |
| 84 | + img2.src = page.canvas && page.canvas.toDataURL("image/" + mimetype) || ""; |
| 85 | + this.buttonContainer.appendChild(img2); |
| 86 | + |
| 87 | + } |
| 88 | + |
| 89 | + /* |
| 90 | + unsafeWindow.PDFView.pages.forEach(function(page) { |
| 91 | + console.log(page, page.draw); |
| 92 | + if (page.draw) page.draw(); |
| 93 | +
|
| 94 | + var img = document.createElement("button"); |
| 95 | + img.classList.add("secondaryToolbarButton", "download"); |
| 96 | + img.dataset.canvasURL = page.canvas.toDataURL("image/" + mimetype); |
| 97 | + img.setAttribute("download", page.canvas.id + "." + mimetype); |
| 98 | + img.setAttribute("title", "Download " + page.canvas.id + "." + mimetype); |
| 99 | + img.style.display = "inline-block"; |
| 100 | + img.appendChild(document.createTextNode(page.canvas.id)); |
| 101 | + img.addEventListener("click", function() { |
| 102 | + this.href = this.dataset.canvasURL; |
| 103 | + }); |
| 104 | + this.buttonContainer.appendChild(img); |
| 105 | + console.log(page.canvas, img, arguments); |
| 106 | + }); |
| 107 | +/* |
| 108 | + var canvases = document.querySelectorAll("canvas:not(.thumbnailImage)"); |
| 109 | + console.log("test", canvases); |
| 110 | + Array.prototype.forEach.call(canvases, function(canvas) { |
| 111 | + var img = document.createElement("button"); |
| 112 | + img.classList.add("secondaryToolbarButton", "download"); |
| 113 | + img.dataset.canvasURL = canvas.toDataURL("image/" + mimetype); |
| 114 | + img.setAttribute("download", canvas.id + "." + mimetype); |
| 115 | + img.setAttribute("title", "Download " + canvas.id + "." + mimetype); |
| 116 | + img.style.display = "inline-block"; |
| 117 | + img.appendChild(document.createTextNode(canvas.id)); |
| 118 | + img.addEventListener("click", function() { |
| 119 | + this.href = this.dataset.canvasURL; |
| 120 | + }); |
| 121 | + this.buttonContainer.appendChild(img); |
| 122 | + console.log(canvas, img, arguments); |
| 123 | + });*/ |
| 124 | + }, |
| 125 | + |
| 126 | + empty: function() { |
| 127 | + while (this.buttonContainer.hasChildNodes()) { |
| 128 | + this.buttonContainer.removeChild(this.buttonContainer.lastChild); |
| 129 | + } |
| 130 | + }, |
| 131 | + |
| 132 | + open: function secondaryToolbarOpen() { |
| 133 | + if (this.opened) { |
| 134 | + return; |
| 135 | + } |
| 136 | + this.opened = true; |
| 137 | + this.toolbar.classList.remove('hidden'); |
| 138 | + this.render(); |
| 139 | + }, |
| 140 | + |
| 141 | + close: function secondaryToolbarClose(target) { |
| 142 | + if (!this.opened) { |
| 143 | + return; |
| 144 | + } else if (target && !this.toolbar.contains(target)) { |
| 145 | + return; |
| 146 | + } |
| 147 | + this.opened = false; |
| 148 | + this.toolbar.classList.add('hidden'); |
| 149 | + this.empty(); |
| 150 | + }, |
| 151 | + |
| 152 | + toggle: function secondaryToolbarToggle() { |
| 153 | + if (this.opened) { |
| 154 | + this.close(); |
| 155 | + } else { |
| 156 | + this.open(); |
| 157 | + } |
| 158 | + } |
| 159 | + }; |
| 160 | + |
| 161 | + SecondaryToolbar.initialize(); |
| 162 | + |
| 163 | + var toolbar = document.getElementById("toolbarViewerRight"); |
| 164 | + var btn = document.createElement("button"); |
| 165 | + btn.classList.add("toolbarButton", "zoomIn"); |
| 166 | + toolbar.insertBefore(btn, toolbar.firstChild); |
| 167 | + btn.addEventListener("click", function() { |
| 168 | + SecondaryToolbar.toggle(); |
| 169 | + }); |
| 170 | + |
| 171 | +})(); |
0 commit comments