forked from jerone/UserScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPDF_Tools.user.js
More file actions
174 lines (152 loc) · 5.85 KB
/
Copy pathPDF_Tools.user.js
File metadata and controls
174 lines (152 loc) · 5.85 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// ==UserScript==
// @id PDF_Tools@https://github.com/jerone/UserScripts
// @name PDF Tools
// @description An userscript that enhances the pdf.js window in Firefox.
// @version 1.0
// @namespace https://github.com/jerone/UserScripts
// @author jerone
// @license CC-BY-NC-SA-4.0; https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode
// @license GPL-3.0-or-later; http://www.gnu.org/licenses/gpl-3.0.txt
// @supportURL https://github.com/jerone/UserScripts/issues
// @contributionURL https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=VCYMHWQ7ZMBKW
// @include *.pdf
// @include *.pdf?*
// @include *.pdf#*
// @run-at document-end
// ==/UserScript==
(function() {
//console.log(PDFJS.version); // "1.0.277"
var mimetype = "png";
var SecondaryToolbar = {
opened: false,
initialize: function secondaryToolbarInitialize() {
this.toolbar = document.createElement("div");
this.toolbar.classList.add("secondaryToolbar", "doorHangerRight", "hidden");
this.toolbar.style.right = "180px";
document.getElementById("mainContainer").appendChild(this.toolbar);
this.buttonContainer = document.createElement("div");
this.buttonContainer.classList.add("secondaryToolbarButtonContainer");
this.toolbar.appendChild(this.buttonContainer);
this.attachEvents();
},
attachEvents: function() {
/// https://github.com/mozilla/pdf.js/blob/2f5c6d6c3a75f9f44826c776dd356e2f786f35de/web/viewer.js#L2248
window.addEventListener("click", function click(evt) {
if (SecondaryToolbar.opened && unsafeWindow.PDFView.container.contains(evt.target)) {
SecondaryToolbar.close();
}
}, false);
/// https://github.com/mozilla/pdf.js/blob/2f5c6d6c3a75f9f44826c776dd356e2f786f35de/web/viewer.js#L2381
window.addEventListener("keydown", function keydown(evt) {
if (SecondaryToolbar.opened && evt.keyCode === 27) { // esc;
SecondaryToolbar.close();
}
});
},
render: function() {
console.log(unsafeWindow.PDFView.pages);
console.log(unsafeWindow.PDFView.pages[0].draw);
var pages = unsafeWindow.PDFView.pages;
for (var i = 0, ii = pages.length; i < ii; i++) {
var page = pages[i];
console.log(page, page.draw);
var img = document.createElement("a");
img.classList.add("secondaryToolbarButton", "download");
img.dataset.pageIndex = i;
img.setAttribute("download", "page" + page.id + "." + mimetype);
img.setAttribute("title", "Download 'page" + page.id + "." + mimetype + "'");
img.style.display = "inline-block";
img.style.boxSizing = "border-box";
img.appendChild(document.createTextNode("Page " + page.id));
img.addEventListener("click", function() {
var page = pages[this.dataset.pageIndex];
if (!page.canvas) { page.draw(); }
this.href = page.canvas.toDataURL("image/" + mimetype);
//window.open( page.canvas.toDataURL("image/" + mimetype));
});
//this.buttonContainer.appendChild(img);
console.log(page.canvas, img, arguments);
//if (!page.canvas) { page.draw(); }
var img2 = document.createElement("img");
//img2.style.width = "16px";
img2.style.height = "16px";
img2.style.border = "1px solid red";
//img2.src = page.canvas.toDataURL("image/" + mimetype);
img2.src = page.canvas && page.canvas.toDataURL("image/" + mimetype) || "";
this.buttonContainer.appendChild(img2);
}
/*
unsafeWindow.PDFView.pages.forEach(function(page) {
console.log(page, page.draw);
if (page.draw) page.draw();
var img = document.createElement("button");
img.classList.add("secondaryToolbarButton", "download");
img.dataset.canvasURL = page.canvas.toDataURL("image/" + mimetype);
img.setAttribute("download", page.canvas.id + "." + mimetype);
img.setAttribute("title", "Download " + page.canvas.id + "." + mimetype);
img.style.display = "inline-block";
img.appendChild(document.createTextNode(page.canvas.id));
img.addEventListener("click", function() {
this.href = this.dataset.canvasURL;
});
this.buttonContainer.appendChild(img);
console.log(page.canvas, img, arguments);
});
/*
var canvases = document.querySelectorAll("canvas:not(.thumbnailImage)");
console.log("test", canvases);
Array.prototype.forEach.call(canvases, function(canvas) {
var img = document.createElement("button");
img.classList.add("secondaryToolbarButton", "download");
img.dataset.canvasURL = canvas.toDataURL("image/" + mimetype);
img.setAttribute("download", canvas.id + "." + mimetype);
img.setAttribute("title", "Download " + canvas.id + "." + mimetype);
img.style.display = "inline-block";
img.appendChild(document.createTextNode(canvas.id));
img.addEventListener("click", function() {
this.href = this.dataset.canvasURL;
});
this.buttonContainer.appendChild(img);
console.log(canvas, img, arguments);
});*/
},
empty: function() {
while (this.buttonContainer.hasChildNodes()) {
this.buttonContainer.removeChild(this.buttonContainer.lastChild);
}
},
open: function secondaryToolbarOpen() {
if (this.opened) {
return;
}
this.opened = true;
this.toolbar.classList.remove('hidden');
this.render();
},
close: function secondaryToolbarClose(target) {
if (!this.opened) {
return;
} else if (target && !this.toolbar.contains(target)) {
return;
}
this.opened = false;
this.toolbar.classList.add('hidden');
this.empty();
},
toggle: function secondaryToolbarToggle() {
if (this.opened) {
this.close();
} else {
this.open();
}
}
};
SecondaryToolbar.initialize();
var toolbar = document.getElementById("toolbarViewerRight");
var btn = document.createElement("button");
btn.classList.add("toolbarButton", "zoomIn");
toolbar.insertBefore(btn, toolbar.firstChild);
btn.addEventListener("click", function() {
SecondaryToolbar.toggle();
});
})();