Skip to content

Commit b3b85e2

Browse files
committed
Add dropdown for selecting reaction images
1 parent 3c0881a commit b3b85e2

File tree

5 files changed

+84
-239
lines changed

5 files changed

+84
-239
lines changed

src/dropdown.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { ReactionImage } from '.';
2+
3+
export const createDropdown = (
4+
box: Element,
5+
reactionImages: ReactionImage[]
6+
) => {
7+
const dropdown = document.createElement('select');
8+
dropdown.textContent = reactionImages[0].name;
9+
reactionImages.forEach(image => {
10+
const option = document.createElement('option');
11+
option.textContent = image.name;
12+
option.value = image.name;
13+
dropdown.appendChild(option);
14+
});
15+
16+
dropdown.onchange = (e: Event) => {
17+
let target = e.target as HTMLSelectElement;
18+
let selectedImage = reactionImages[target.selectedIndex];
19+
console.log(selectedImage)
20+
window.selectedReactionImage = selectedImage.src;
21+
}
22+
23+
box.append(dropdown);
24+
}

src/index.ts

Lines changed: 54 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,70 @@
11
import { replaceQRSubmit } from './quickReply';
2+
import { createDropdown } from './dropdown';
23

34
import cryingWojak from '../assets/crying_wojak.png';
5+
import cryingSoyjak from '../assets/crying_soyjak.jpg';
6+
import npcWojak from '../assets/npc_wojak.png';
47

58
const quoteColor = '#789922';
69
const bgColor = '#ffffff';
10+
const font = '40px Arial';
11+
12+
const imageWidth = 600;
13+
14+
export type ReactionImage = {
15+
name: string;
16+
src: string;
17+
};
18+
19+
const reactionImages: ReactionImage[] = [
20+
{ name: 'Crying Wojak', src: cryingWojak },
21+
{ name: 'Crying Soyjak', src: cryingSoyjak },
22+
{ name: 'NPC Wojak', src: npcWojak }
23+
];
724

825
declare global {
9-
interface Window { imageData: string; }
26+
interface Window {
27+
imageData: string;
28+
selectedReactionImage: string;
29+
}
1030
interface QR {
1131
submit: Function;
1232
xhr: any;
1333
}
1434
}
1535
window.imageData = '';
36+
window.selectedReactionImage = cryingWojak;
1637

17-
const onButtonClick = (postText: string) => {
38+
const onButtonClick = (postText: string, button: Element) => {
1839
const image = new Image();
1940

20-
image.src = cryingWojak as unknown as string;
41+
image.src = window.selectedReactionImage;
2142

2243
const canvas = document.createElement('canvas');
2344
image.onload = () => {
24-
canvas.width = image.width;
25-
canvas.height = image.height + 100;
45+
const imageHeight = imageWidth * (image.height / image.width);
46+
canvas.width = imageWidth;
47+
canvas.height = imageHeight + 100;
2648
const ctx = canvas.getContext('2d');
2749

28-
ctx.font = '48px serif';
29-
let lines = countLines(ctx, postText, image.width);
50+
ctx.font = font;
51+
let lines = countLines(ctx, postText, imageWidth);
3052
canvas.height = canvas.height + (lines + 2) * 48;
3153

3254
ctx.fillStyle = bgColor;
3355
ctx.fillRect(0, 0, canvas.width, canvas.height);
3456

35-
ctx.font = '48px serif';
57+
ctx.font = font;
3658
ctx.fillStyle = quoteColor;
37-
wrapText(ctx, postText, 0, image.height + 100, image.width, 48);
59+
wrapText(ctx, postText, 0, imageHeight + 100, imageWidth, 48);
3860

3961
ctx.imageSmoothingEnabled = false;
40-
ctx.drawImage(image, 0, 0);
62+
ctx.drawImage(image, 0, 0, imageWidth, imageHeight);
4163

4264
let data = canvas.toDataURL();
4365
window.imageData = data;
66+
67+
button.textContent = 'Wojakified'
4468
};
4569
}
4670

@@ -53,7 +77,7 @@ const wrapText = (context: CanvasRenderingContext2D, text: string, x: number, y:
5377
var metrics = context.measureText(testLine);
5478
var testWidth = metrics.width;
5579
if (testWidth > maxWidth && n > 0) {
56-
context.font = '48px serif';
80+
context.font = font;
5781
context.fillStyle = quoteColor;
5882

5983
context.fillText(line, x, y);
@@ -84,41 +108,44 @@ const countLines = (context: CanvasRenderingContext2D, text: string, maxWidth: n
84108
return linesN;
85109
}
86110

87-
const b64toBlob = (b64Data: string, contentType='', sliceSize=512) => {
111+
const b64toBlob = (b64Data: string, contentType = '', sliceSize = 512) => {
88112
const byteCharacters = atob(b64Data);
89113
const byteArrays = [];
90114

91115
for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {
92-
const slice = byteCharacters.slice(offset, offset + sliceSize);
116+
const slice = byteCharacters.slice(offset, offset + sliceSize);
93117

94-
const byteNumbers = new Array(slice.length);
95-
for (let i = 0; i < slice.length; i++) {
96-
byteNumbers[i] = slice.charCodeAt(i);
97-
}
118+
const byteNumbers = new Array(slice.length);
119+
for (let i = 0; i < slice.length; i++) {
120+
byteNumbers[i] = slice.charCodeAt(i);
121+
}
98122

99-
const byteArray = new Uint8Array(byteNumbers);
100-
byteArrays.push(byteArray);
123+
const byteArray = new Uint8Array(byteNumbers);
124+
byteArrays.push(byteArray);
101125
}
102126

103-
const blob = new Blob(byteArrays, {type: contentType});
127+
const blob = new Blob(byteArrays, { type: contentType });
104128
return blob;
105129
};
106130

107131
const setFile = (t: any) => { //type of t is hidden somewhere in the minified QR code
108-
if(window.imageData) {
132+
if (window.imageData) {
109133
console.log('test', t)
110134
t.set("upfile", new File([b64toBlob(window.imageData.substring(22))], "you.png"));
111135
window.imageData = null;
112136
}
113137
};
114138

115-
const createButton = (box: Element, postText: string, buttonText: string, onClick: (text: string) => void) => {
116-
const button = document.createElement('button', {
117-
118-
});
139+
const createButton = (
140+
box: Element,
141+
postText: string,
142+
buttonText: string,
143+
onClick: (text: string, button: Element) => void
144+
) => {
145+
const button = document.createElement('button', {});
119146
button.textContent = buttonText;
120147
button.setAttribute('style', 'cursor: pointer;');
121-
button.addEventListener("click", e => { e.preventDefault(), onClick(`>${postText}`); });
148+
button.addEventListener("click", e => { e.preventDefault(), onClick(`>${postText}`, button); });
122149
box.append(button);
123150
}
124151

@@ -135,6 +162,7 @@ const findPosts = () => {
135162
if (box) {
136163
const postText = boxWT.getElementsByTagName('blockquote')[0].innerText;
137164
createButton(box, postText, 'Wojakify', onButtonClick);
165+
createDropdown(box, reactionImages);
138166
}
139167
}
140168
}

src/quickReply.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export const replaceQRSubmit = setFile => {
2-
let QR = window.QR;
2+
let QR = window.QR;
33
QR.submit = function (e) {
44
var t;
55
QR.hidePostError(),

src/retard.js

Lines changed: 0 additions & 211 deletions
This file was deleted.

tsconfig.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
"module": "commonjs",
66
"target": "es5",
77
"allowJs": true,
8-
"esModuleInterop": true
8+
"esModuleInterop": true,
9+
"typeRoots": [
10+
"typings",
11+
"node_modules/@types"
12+
]
913
}
1014
}

0 commit comments

Comments
 (0)