22//
33// SPDX-License-Identifier: GPL-3.0-or-later
44
5- $ ( document ) . ready ( ( ) => {
5+ document . addEventListener ( "DOMContentLoaded" , ( ) => {
66 // The paste trigger button
7- const $pasteScreenshotBtn = $ ( "#paste-screenshot-btn" ) ;
7+ const pasteScreenshotBtn = document . getElementById ( "paste-screenshot-btn" ) ;
8+ if ( pasteScreenshotBtn === null ) {
9+ return ;
10+ }
811 // The file input to store the screenshot file
9- const $screenshotFileInput = $ ( "#screenshot-form-container input#id_image" ) ;
12+ const screenshotFileInput = document . querySelector (
13+ "#screenshot-form-container input#id_image" ,
14+ ) ;
1015
1116 // Check if the browser supports the Clipboard API
1217 if ( ! navigator . clipboard ?. read ) {
13- $pasteScreenshotBtn . remove ( ) ;
18+ pasteScreenshotBtn . remove ( ) ;
19+ return ;
1420 }
1521
16- $ pasteScreenshotBtn. on ( "click" , async ( e ) => {
22+ pasteScreenshotBtn . addEventListener ( "click" , async ( e ) => {
1723 e . preventDefault ( ) ;
1824 try {
1925 // Read clipboard content
@@ -27,13 +33,13 @@ $(document).ready(() => {
2733 const blob = await clipboardItem . getType ( type ) ;
2834 const reader = new FileReader ( ) ;
2935 reader . onload = ( _event ) => {
30- if ( $ screenshotFileInput. length > 0 ) {
36+ if ( screenshotFileInput !== null ) {
3137 // Load the file data into the form input
3238 const fileName = `screenshot_${ Date . now ( ) } .${ type . split ( "/" ) [ 1 ] } ` ;
3339 const imageFile = new File ( [ blob ] , fileName , { type : type } ) ;
3440 const dataTransfer = new DataTransfer ( ) ;
3541 dataTransfer . items . add ( imageFile ) ;
36- $ screenshotFileInput[ 0 ] . files = dataTransfer . files ;
42+ screenshotFileInput . files = dataTransfer . files ;
3743 // Inform paste success
3844 showInfo ( "success" , gettext ( "Image Pasted!" ) ) ;
3945 } else {
@@ -65,10 +71,16 @@ $(document).ready(() => {
6571 * @param {string } message - The content of the message.
6672 */
6773function showInfo ( type , message ) {
68- const $pasteScreenshotInfo = $ ( "#paste-screenshot-info-label" ) ;
69- const span = `<span class="text-${ type } ">${ message } </span>` ;
70- $pasteScreenshotInfo . html ( span ) ;
71- $pasteScreenshotInfo
72- . css ( "transform" , "scale(1)" )
73- . removeClass ( "animate__animated animate__fadeIn" ) ;
74+ const pasteScreenshotInfo = document . getElementById (
75+ "paste-screenshot-info-label" ,
76+ ) ;
77+ if ( pasteScreenshotInfo === null ) {
78+ return ;
79+ }
80+ const span = document . createElement ( "span" ) ;
81+ span . classList . add ( `text-${ type } ` ) ;
82+ span . textContent = message ;
83+ pasteScreenshotInfo . replaceChildren ( span ) ;
84+ pasteScreenshotInfo . style . transform = "scale(1)" ;
85+ pasteScreenshotInfo . classList . remove ( "animate__animated" , "animate__fadeIn" ) ;
7486}
0 commit comments