My code runs well in the console as intended to display all images on a webpage in grid with content around the images. In each griditem, there’s a save button to save the image.  The code is running OK in Console. But when pasted in a bookmarklet it does not work. Here is the code. Can you identify what's the problem? How to edit to make it work?  javascript:void((function(){ 

  
      var clscontainer = "entry-content";
      var container = document.querySelector("."+clscontainer);
  
      var gridContainer = document.createElement("div");
      
      gridContainer.style.display = "grid";
      gridContainer.style.zIndex = 1000000;
      gridContainer.style.gridTemplateColumns = "repeat(4, 1fr)";
      
      gridContainer.style.gap = "10px";
      gridContainer.style.backgroundColor = "#999";
      
      gridContainer.style.position = "absolute";
      
      gridContainer.style.top = 0;
      
      gridContainer.style.left = 0;
      
      gridContainer.style.width = "100%";
      
      gridContainer.style.height = "auto";
     
      document.body.appendChild(gridContainer);
      
   
      
      var images = container.querySelectorAll("img");
        
      images.forEach(function(image) {
       
       
        var imageClone = image.cloneNode(true);
      let url;
          if (image.hasAttribute("data-src") ){
           url = image.getAttribute("data-src");
            
        }else{
           url = image.src;
          
        }
          imageNamewithext = url.substring(url.lastIndexOf('/')+1);
          imageName = url.substring(url.lastIndexOf('/')+1, url.lastIndexOf('.'));
      
        var gridItem = document.createElement("div");
        gridItem.style.padding = '10px';
      
        gridItem.style.border = '1px solid black';
      
      
        gridItem.appendChild(imageClone);
        
        const subdiv = document.createElement('div');
      
        const button = document.createElement('button');
      
        button.innerText = 'Save Image';
        button.innerText = 'Save Image';
        const contentabove = document.createElement('div');
        contentabove.style.fontWeight = "bold";
        const text = getTextAboveImage(image);
        let myText = document.createTextNode(text);
        contentabove.appendChild(myText);
        gridItem.appendChild(contentabove);
  
  
        const altdiv = document.createElement('div');
        let alttext = image.getAttribute("alt");
        if(alttext=="") {
          alttext = "alt text is null";
        }
        myText = document.createTextNode(alttext);
        altdiv.appendChild(myText);
         gridItem.appendChild(altdiv);
        
        const contentbelow = document.createElement('div');
        contentbelow.style.fontWeight = "bold";
        const  texts = getTextBelowImage(image);
         myText = document.createTextNode(texts);
        contentbelow.appendChild(myText);
        gridItem.appendChild(contentbelow);
  
        const textarea = document.createElement('textarea');
        textarea.value = imageName;
      
        textarea.style.width = "100%";
      
       
      
        subdiv.appendChild(textarea);
      
      
        subdiv.appendChild(button);
      
        subdiv.style.display = 'flex';
        subdiv.style.flexDirection ='column';
        subdiv.style.justifyContent = 'center';
      
        subdiv.style.alignItems = 'center';
      
        subdiv.style.marginTop = '10px';
      
        gridItem.appendChild(subdiv);
      
        gridContainer.appendChild(gridItem);
        button.addEventListener('click', () => {
      
          downloadImages(imageClone);
      
        });
      
      });
      
      function getTextAboveImage(imgElement) {
        const maxSearchDepth = 1;
        
        const getText = (element) => {
          let text = '';
          if (element.nodeType === Node.TEXT_NODE) {
            text += element.textContent.trim();
          } else if (element.nodeType === Node.ELEMENT_NODE) {
             if(element.tagName === "IMG"){
               text +=  element.getAttribute("alt");
             }else if(element.tagName === "P"){
                text +=  element.innerText.trim();    
              }else if(element.tagName === "H2"||element.tagName === "H3"){
                 text +=  element.innerText.trim();
              }
            
          }
          return text;
        };
      
        const searchSiblingsPrevious = (element, depth) => {
          if (!element || depth > maxSearchDepth) {
            return '';
          }
          let text = '';
          text += searchSiblingsPrevious(element.previousSibling, depth + 1);
          text +=  ' ' + getText(element);
          
          return text;
        };
        
   
        
      
        const searchParents = (element) => {
          if (!element || element.className === clscontainer) {
            return '';
          }
          let text = '';
          text += searchSiblingsPrevious(element.previousSibling, 0);
          text += ' ' + searchParents(element.parentElement);
      
          return text;
        };
      
        return searchParents(imgElement);
      }
      
      function getTextBelowImage(imgElement) {
        const maxSearchDepth = 1;
        
        const getText = (element) => {
          let text = '';
          if (element.nodeType === Node.TEXT_NODE) {
            text += element.textContent.trim();
          } else if (element.nodeType === Node.ELEMENT_NODE) {
             if(element.tagName === "IMG"){
               text +=  element.getAttribute("alt");
             }else if(element.tagName === "P"){
                text +=  element.innerText.trim();
       
              }else if(element.tagName === "H2"||element.tagName === "H3"){
                 text +=  element.innerText.trim();
              }
            
          }
          return text;
        };
      
   
        
         const searchSiblingsNext = (element, depth) => {
          if (!element || element.tagName=="IMG" ||depth > maxSearchDepth) {
            return '';
          }
          let text = '';     
          text +=  ' ' + getText(element);
          text += ' ' + searchSiblingsNext(element.nextSibling, depth + 1);
          return text;
        };
        
      
        const searchParents = (element) => {
          if (!element || element.className === clscontainer) {
            return '';
          }
          let text = '';
          text += ' ' + searchParents(element.parentElement);
          text += ' ' + searchSiblingsNext(element.nextSibling, 0);
          return text;
        };
      
        return searchParents(imgElement);
      }
    
   
  
  
      function convertToJPEG(image,imageName) {
       
   
          if(image.type==undefined) return image;
        
          if (image.type !== 'image/jpeg') {
         
            const canvas = document.createElement('canvas');
        
            const context = canvas.getContext('2d');
        
           
            canvas.width = image.width;
        
            canvas.height = image.height;
        
           
            context.drawImage(image, 0, 0);
        
           
            const dataURL = canvas.toDataURL('image/jpeg');
        
          
            const convertedImage = new Image();
        
            convertedImage.setAttribute('name', imageName.replace(/\.[^/.]+$/, "").replace(' ',"-") + ".jpg");
          
            convertedImage.src = dataURL;
        
          
            return convertedImage;
        
          } else {
        
         
            return image;
        
          }
        
        }
      
      
        function resizeto600(image,imageName ) {
          if(image.width!=600){
      
            image.addEventListener('load', () => {
              const canvas = document.createElement('canvas');
            
              const context = canvas.getContext('2d');
      
              const maxWidth = 600;
      
              const ratio = maxWidth / image.width;
      
              const height = image.height * ratio;
      
              canvas.width =  maxWidth;
      
              canvas.height = height;
      
              context.drawImage(image, 0, 0, canvas.width, canvas.height);
      
              const dataURI = canvas.toDataURL();
       
              image.src = dataURL;
          });
      
          return image; 
            
          }else{
            return image;
      
          }
        }
       
      function downloadImages(img) {
          let url = img.src;
          
           imageName = img.parentNode.querySelector("textarea").value
      
          img = convertToJPEG(img,imageName);
        
          img = resizeto600(img,imageName);
      
          if (url.endsWith(".webp")) {
      
            webpToJpeg(url).then((dataUrl) => {
      
              xhr_download(img);
      
            });
      
          } else {
      
            xhr_download(img); 
      
          }
      }
      
      
      function xhr_download(img){
        const xhr = new XMLHttpRequest();
        let url = img.src;
       
        imageName = img.parentNode.querySelector("textarea").value;
      
        xhr.open('GET', img.src, true);
      
        xhr.responseType = 'blob';
      
        xhr.onload = () => {
      
          if (xhr.status === 200) {
      
            const blob = new Blob([xhr.response], { type: 'image/jpeg' }); 
      
            const url = URL.createObjectURL(blob);
      
            const a = document.createElement('a');
      
            a.href = url;
      
            a.download =  imageName + ".jpeg";
      
            a.click();
      
            URL.revokeObjectURL(url);
 
          }
      
        };
      
      xhr.send();
      }
      
      
      function webpToJpeg(url) {
      
        return new Promise((resolve) => {
      
          let img = new Image();
      
          img.onload = () => {
      
            let canvas = document.createElement("canvas");
      
            canvas.width = img.width;
      
            canvas.height = img.height;
      
            canvas.getContext("2d").drawImage(img, 0, 0);
      
            let dataUrl = canvas.toDataURL("image/jpeg");
      
            resolve(dataUrl);
      
          };
      
          img.src = url;
      
        });
      
      }
  
  
  })());