Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

271
Views
Can I "freeze" fixed elements to take a screenshot with getDisplayMedia?

I'm implementing a chrome extension with javascript to take a screenshot of a full page, so far I've managed to take the screenshot and make it into a canvas in a new tab, it shows the content of a tweet perfectly, but as you can see, the sidebars repeat all the time (Ignore the red button, that's part of my extension and I know how to delete it from the screenshots) screenshot of a tweet

This is the code I'm using to take the screenshot:

async function capture(){
  navigator.mediaDevices.getDisplayMedia({preferCurrentTab:true}).then(mediaStream=>{
    scrollTo(0,0);
    var defaultOverflow = document.body.style.overflow;
    //document.body.style.overflow = 'hidden';
    var totalHeight = document.body.scrollHeight;
    var actualHeight = document.documentElement.clientHeight;
    var leftHeight = totalHeight-actualHeight;
    var scroll = 200;
    var blob = new Blob([document.documentElement.innerHTML],{ type: "text/plain;charset=utf-8" });
    console.log('total Height:'+totalHeight+'client height:'+actualHeight+'Left Height:'+leftHeight);
    var numScreenshots = Math.ceil(leftHeight/scroll);
    var arrayImg = new Array();
    var i = 0;                  
    function myLoop() {         
      setTimeout(function() {   
        var track = mediaStream.getVideoTracks()[0];
        let imgCapture = new ImageCapture(track);
        imgCapture.grabFrame().then(bitmap=>{
          arrayImg[i] = bitmap;
          window.scrollBy(0,scroll);
          console.log(i);
          i++;
        });                  
        if (i <= numScreenshots) {           
          myLoop();              
        }else{
          document.body.style.overflow = defaultOverflow;
          saveAs(blob, "static.txt");
          printBitMaps(arrayImg, numScreenshots, totalHeight);
        }                       
      }, 250)
    }
    myLoop();
  })
}
async function printBitMaps(arrayImg, numScreenshots, totalHeight){
  var win = window.open('about:blank', '_blank');
  win.document.write('<canvas id="myCanvas" width="'+arrayImg[0].width+'px" height="'+totalHeight+'px" style="border:5px solid #000000;"></canvas>');
  var e = numScreenshots+1;
  function printToCanvas(){
    setTimeout(function(){
      var canvas = win.document.getElementById("myCanvas");
      var ctx = canvas.getContext("2d");
      ctx.drawImage(arrayImg[e], 0, 200*e);
      e--;
      if(e>=0){
        printToCanvas();
      }
    },10);

  }
  printToCanvas();
}

Do you know any way by CSS or javascript that I can use to make the sidebars stay at the top of the page so they don't keep coming down with the scroll?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

It's not really a case of "the sidebars ... coming down with the scroll" - the code you're using is taking a first screenshot, scrolling the page, taking another screenshot and then stitching it onto the last one and iterating to the bottom of the page. Thus it's inevitable you're seeing what you see on the screen at the point you take the subsequent screenshots.

To resolve your issue, after your first screenshot you would need to set the div element for the side bar to be display=None by use of CSS. You can find details of the side bar by using the browser Dev Tools, right clicking an using "Inspect" in Chrome.

From what I can see, Twitter seems to use fairly cryptic class names, so it might be easiest and more robust to identify the div for the side bar with some other attribute. It appears they set data-testid="sidebarColumn" on it so give using that a go (but YMMV).

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!