1
1
import { replaceQRSubmit } from './quickReply' ;
2
+ import { createDropdown } from './dropdown' ;
2
3
3
4
import cryingWojak from '../assets/crying_wojak.png' ;
5
+ import cryingSoyjak from '../assets/crying_soyjak.jpg' ;
6
+ import npcWojak from '../assets/npc_wojak.png' ;
4
7
5
8
const quoteColor = '#789922' ;
6
9
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
+ ] ;
7
24
8
25
declare global {
9
- interface Window { imageData : string ; }
26
+ interface Window {
27
+ imageData : string ;
28
+ selectedReactionImage : string ;
29
+ }
10
30
interface QR {
11
31
submit : Function ;
12
32
xhr : any ;
13
33
}
14
34
}
15
35
window . imageData = '' ;
36
+ window . selectedReactionImage = cryingWojak ;
16
37
17
- const onButtonClick = ( postText : string ) => {
38
+ const onButtonClick = ( postText : string , button : Element ) => {
18
39
const image = new Image ( ) ;
19
40
20
- image . src = cryingWojak as unknown as string ;
41
+ image . src = window . selectedReactionImage ;
21
42
22
43
const canvas = document . createElement ( 'canvas' ) ;
23
44
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 ;
26
48
const ctx = canvas . getContext ( '2d' ) ;
27
49
28
- ctx . font = '48px serif' ;
29
- let lines = countLines ( ctx , postText , image . width ) ;
50
+ ctx . font = font ;
51
+ let lines = countLines ( ctx , postText , imageWidth ) ;
30
52
canvas . height = canvas . height + ( lines + 2 ) * 48 ;
31
53
32
54
ctx . fillStyle = bgColor ;
33
55
ctx . fillRect ( 0 , 0 , canvas . width , canvas . height ) ;
34
56
35
- ctx . font = '48px serif' ;
57
+ ctx . font = font ;
36
58
ctx . fillStyle = quoteColor ;
37
- wrapText ( ctx , postText , 0 , image . height + 100 , image . width , 48 ) ;
59
+ wrapText ( ctx , postText , 0 , imageHeight + 100 , imageWidth , 48 ) ;
38
60
39
61
ctx . imageSmoothingEnabled = false ;
40
- ctx . drawImage ( image , 0 , 0 ) ;
62
+ ctx . drawImage ( image , 0 , 0 , imageWidth , imageHeight ) ;
41
63
42
64
let data = canvas . toDataURL ( ) ;
43
65
window . imageData = data ;
66
+
67
+ button . textContent = 'Wojakified'
44
68
} ;
45
69
}
46
70
@@ -53,7 +77,7 @@ const wrapText = (context: CanvasRenderingContext2D, text: string, x: number, y:
53
77
var metrics = context . measureText ( testLine ) ;
54
78
var testWidth = metrics . width ;
55
79
if ( testWidth > maxWidth && n > 0 ) {
56
- context . font = '48px serif' ;
80
+ context . font = font ;
57
81
context . fillStyle = quoteColor ;
58
82
59
83
context . fillText ( line , x , y ) ;
@@ -84,41 +108,44 @@ const countLines = (context: CanvasRenderingContext2D, text: string, maxWidth: n
84
108
return linesN ;
85
109
}
86
110
87
- const b64toBlob = ( b64Data : string , contentType = '' , sliceSize = 512 ) => {
111
+ const b64toBlob = ( b64Data : string , contentType = '' , sliceSize = 512 ) => {
88
112
const byteCharacters = atob ( b64Data ) ;
89
113
const byteArrays = [ ] ;
90
114
91
115
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 ) ;
93
117
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
+ }
98
122
99
- const byteArray = new Uint8Array ( byteNumbers ) ;
100
- byteArrays . push ( byteArray ) ;
123
+ const byteArray = new Uint8Array ( byteNumbers ) ;
124
+ byteArrays . push ( byteArray ) ;
101
125
}
102
126
103
- const blob = new Blob ( byteArrays , { type : contentType } ) ;
127
+ const blob = new Blob ( byteArrays , { type : contentType } ) ;
104
128
return blob ;
105
129
} ;
106
130
107
131
const setFile = ( t : any ) => { //type of t is hidden somewhere in the minified QR code
108
- if ( window . imageData ) {
132
+ if ( window . imageData ) {
109
133
console . log ( 'test' , t )
110
134
t . set ( "upfile" , new File ( [ b64toBlob ( window . imageData . substring ( 22 ) ) ] , "you.png" ) ) ;
111
135
window . imageData = null ;
112
136
}
113
137
} ;
114
138
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' , { } ) ;
119
146
button . textContent = buttonText ;
120
147
button . setAttribute ( 'style' , 'cursor: pointer;' ) ;
121
- button . addEventListener ( "click" , e => { e . preventDefault ( ) , onClick ( `>${ postText } ` ) ; } ) ;
148
+ button . addEventListener ( "click" , e => { e . preventDefault ( ) , onClick ( `>${ postText } ` , button ) ; } ) ;
122
149
box . append ( button ) ;
123
150
}
124
151
@@ -135,6 +162,7 @@ const findPosts = () => {
135
162
if ( box ) {
136
163
const postText = boxWT . getElementsByTagName ( 'blockquote' ) [ 0 ] . innerText ;
137
164
createButton ( box , postText , 'Wojakify' , onButtonClick ) ;
165
+ createDropdown ( box , reactionImages ) ;
138
166
}
139
167
}
140
168
}
0 commit comments