463463
464464 <div class =" flex items-center justify-end m-4 space-x-4" >
465465 <b-button
466- v-if =" result.url"
466+ v-if =" result && result .url"
467467 icon-left =" download-outline"
468468 @click =" download"
469469 type =" is-success is-light"
491491
492492 <div class =" h-full overflow-y-auto box bg-blueGray-900" >
493493 <p class =" title is-5" >Website Screenshot</p >
494- <div v-if =" ! result.url " class =" mb-3" >
494+ <div v-if =" result == null " class =" mb-3" >
495495 <p class =" title is-4" >Capture a website screenshot online</p >
496496 <p class =" mt-4 text-white subtitle is-6" >
497497 Generate a full web-page screenshot with our service Site-Shot: Web
504504 <b-progress v-if =" loading" type =" is-info" ></b-progress >
505505
506506 <embed
507- v-if =" result.url"
507+ v-if =" result && result .url"
508508 :src =" result.url"
509509 width =" 100%"
510510 style =" height : 50vh "
@@ -569,10 +569,7 @@ export default {
569569 },
570570 imageFormats: [" png" , " jpeg" , " webp" ],
571571 pdfFile: null ,
572- result: {
573- url: null ,
574- filename: null ,
575- },
572+ result: null ,
576573 loading: false ,
577574 resolutions: [
578575 {
@@ -687,16 +684,9 @@ export default {
687684 mounted () {},
688685 methods: {
689686 async generateScreenshot () {
690- if (
691- this .selectedType == " multiple-imgs" ||
692- ! this .loading ||
693- this .params .url !== null
694- ) {
687+ if (! this .loading ) {
695688 this .loading = true ;
696- this .result = {
697- url: null ,
698- filename: null ,
699- };
689+ this .result = null ;
700690 this .pdfFile = null ;
701691 const selectedType = this .selectedType ;
702692 let mimeType = " " ;
@@ -708,40 +698,32 @@ export default {
708698 }
709699
710700 await this .$axios
711- .post (
712- " /api/screenshot" ,
713- {
714- ... this .params ,
715- scale: this .params .scale / 100 ,
716- size: [this .params .size ],
717- type: selectedType,
718- mimeType,
719- },
720- {
721- responseType: " blob" ,
722- }
723- )
724- .then ((response ) => {
725- let blob = new window.Blob ([response .data ], {
701+ .$post (" /api/screenshot" , {
702+ ... this .params ,
703+ scale: this .params .scale / 100 ,
704+ size: [this .params .size ],
705+ type: selectedType,
706+ mimeType,
707+ })
708+ .then (({ base64, filename, success }) => {
709+ const blob = new window.Blob ([this .convertBase64ToBlob (base64)], {
726710 type: mimeType,
727711 });
728712
729- let headerLine = response .headers [" content-disposition" ];
730- let startFileNameIndex = headerLine .indexOf (' "' ) + 1 ;
731- let endFileNameIndex = headerLine .lastIndexOf (' "' );
732- let filename = headerLine .substring (
733- startFileNameIndex,
734- endFileNameIndex
735- );
736-
737713 if (selectedType == " multiple-imgs" ) {
738- const link = document . createElement ( " a " );
739- link . href = window .URL .createObjectURL (blob);
740- link . download = filename;
741- link . click ( );
714+ this . download ({
715+ url : window .URL .createObjectURL (blob),
716+ filename: filename,
717+ } );
742718 } else {
743- this .result .url = window .URL .createObjectURL (blob);
744- this .result .filename = filename;
719+ this .result = {
720+ url: window .URL .createObjectURL (blob),
721+ filename,
722+ };
723+ }
724+
725+ if (success) {
726+ this .$success (" Screenshot generated successfully" );
745727 }
746728
747729 this .loading = false ;
@@ -752,11 +734,11 @@ export default {
752734 });
753735 }
754736 },
755- download () {
756- if (this .result ) {
737+ download (result ) {
738+ if (result || this .result ) {
757739 const link = document .createElement (" a" );
758- link .href = this .result .url ;
759- link .download = this .result .filename ;
740+ link .href = result . url || this .result .url ;
741+ link .download = result . filename || this .result .filename ;
760742 link .click ();
761743 }
762744 },
@@ -770,6 +752,27 @@ export default {
770752 urls .splice (index, 1 );
771753 }
772754 },
755+ convertBase64ToBlob (base64Image ) {
756+ // Split into two parts
757+ const parts = base64Image .split (" ;base64," );
758+
759+ // Hold the content type
760+ const imageType = parts[0 ].split (" :" )[1 ];
761+
762+ // Decode Base64 string
763+ const decodedData = window .atob (parts[1 ]);
764+
765+ // Create UNIT8ARRAY of size same as row data length
766+ const uInt8Array = new Uint8Array (decodedData .length );
767+
768+ // Insert all character code into uInt8Array
769+ for (let i = 0 ; i < decodedData .length ; ++ i) {
770+ uInt8Array[i] = decodedData .charCodeAt (i);
771+ }
772+
773+ // Return BLOB image after conversion
774+ return new Blob ([uInt8Array], { type: imageType });
775+ },
773776 },
774777 watch: {
775778 " params.size" () {
@@ -784,6 +787,7 @@ export default {
784787 },
785788 selectedType () {
786789 this .params .url = null ;
790+ this .result = null ;
787791 this .params .urls = [
788792 {
789793 url: null ,
0 commit comments