-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.user.js
126 lines (115 loc) · 4.12 KB
/
main.user.js
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
// ==UserScript==
// @name Gather Town Capture
// @namespace https://www.moontai0724.tw
// @version 0.2
// @description Capture screen without UI on gather.town in just a press.
// @author moontai0724
// @match https://gather.town/*
// @icon https://www.google.com/s2/favicons?domain=gather.town
// @grant MIT
// ==/UserScript==
(function () {
"use strict";
const elementsId = [
"canvas",
"canvas-entities",
"canvas-players",
"canvas-foreground",
"canvas-private-areas",
"canvas-names",
];
const settings = {
scale: undefined,
cellNumber: {
height: undefined,
width: undefined,
},
};
const originalSize = {
canvasHeight: undefined,
canvasWidth: undefined,
containerHeight: undefined,
containerWidth: undefined,
};
function download(changeSize = false) {
setVariables();
if (changeSize) changeCanvasSize();
const canvas = document.createElement("canvas");
const { height, width } = document.getElementById("canvas");
canvas.height = height;
canvas.width = width;
const sources = document.getElementsByTagName("canvas");
setTimeout(
() =>
Promise.all(
Array.from(sources).map((element) => {
return new Promise(function (resolve, reject) {
const image = new Image();
image.src = element.toDataURL();
image.addEventListener("load", (onImageLoadEvent) => {
const imageData = onImageLoadEvent.target;
const context = canvas.getContext("2d");
context.drawImage(imageData, 0, 0);
resolve();
});
});
})
).then(() => {
const link = document.createElement("a");
link.download = `${document.title.slice(0, -9)} - ${getTime()}.png`;
link.href = canvas.toDataURL();
link.click();
if (changeSize) recoverCanvasSize();
}),
1000
);
}
function setVariables() {
settings.scale = localStorage.manualCanvasZoom;
settings.cellNumber.height = gameSpace.maps[gameSpace.mapId].dimensions[0];
settings.cellNumber.width = gameSpace.maps[gameSpace.mapId].dimensions[1];
const canvas = document.getElementById("canvas");
originalSize.canvasHeight = canvas.height;
originalSize.canvasWidth = canvas.width;
originalSize.containerHeight = canvas.style.height;
originalSize.containerWidth = canvas.style.width;
}
function getTime() {
const time = new Date();
const year = time.getFullYear();
const month = (time.getMonth() + 1).toString().padStart(2, "0");
const date = time.getDate().toString().padStart(2, "0");
const hour = time.getHours().toString().padStart(2, "0");
const minute = time.getMinutes().toString().padStart(2, "0");
const second = time.getSeconds().toString().padStart(2, "0");
const millisecond = time.getMilliseconds();
return `${year}-${month}-${date}_${hour}-${minute}-${second}.${millisecond}`;
}
function recoverCanvasSize() {
elementsId.forEach((elementId) => {
const element = document.getElementById(elementId);
element.width = originalSize.canvasWidth;
element.height = originalSize.canvasHeight;
element.style.width = originalSize.containerWidth;
element.style.height = originalSize.containerHeight;
});
}
function changeCanvasSize() {
elementsId.forEach((elementId) => {
const element = document.getElementById(elementId);
const height = settings.cellNumber.height * settings.scale * 32;
const canvasRatio = element.width / element.height;
element.width = height * canvasRatio;
element.height = height;
const containerRatio =
parseInt(element.style.width) / parseInt(element.style.height);
element.style.width = height * containerRatio;
element.style.height = height;
});
}
document.addEventListener("keydown", function (keydownEvent) {
if (!keydownEvent.shiftKey) return;
if (!(keydownEvent.code === "KeyF" || keydownEvent.code === "KeyP")) return;
download(keydownEvent.code === "KeyF");
});
})();