Click here to Skip to main content
16,022,069 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
We've created a stream using navigator.mediadevices.getdisplaymedia which ask permissions to take a screenshot.

All code written in _layout page and it's working, but there is problem, it asking for the (same) permission every time when opening a new menu.

Is there is any way to avoid the fact that the permissions need to be approved every time, or any other solution to take screenshot in mvc 4 c#

For this issue i was tried to store stream details in session, searched another solution to take screenshot, also searched to avoid _layout page load only once, but proper solution not found.

What I have tried:

JavaScript
var videoStream;
                
function handleStreamEnded(stream) {
                
    stream.getVideoTracks()[0].addEventListener('ended', () => {
        requestScreenCapturePermission();
    });
        
    var strm = stream.getVideoTracks()[0];

};
        
async function requestScreenCapturePermission() {
     
    try {
        
        videoStream = await navigator.mediaDevices.getDisplayMedia({
            video: {
                mediaSource: 'screen',
                displaySurface: "monitor"
            },

            preferCurrentTab: false,

            //selfBrowserSurface: "exclude",

            //surfaceSwitching: "include",

            monitorTypeSurfaces: "include",

        });

        handleStreamEnded(videoStream);
                        
        return true;

    } catch (error) {
        console.error('Error capturing screen:', error);

        return false;
    }
};
        
async function takeScreenShot(sender) {
     
    try {
        if (!videoStream) {
             requestScreenCapturePermission();
        }
          
        var video = document.createElement('video');

        video.srcObject = videoStream;

        await video.play();
            
        var canvas = document.createElement('canvas');

        canvas.width = video.videoWidth;
        canvas.height = video.videoHeight;
            
        var context = canvas.getContext('2d');

        context.drawImage(video, 0, 0, canvas.width, canvas.height);
            
        var base64image = canvas.toDataURL('image/png');

        $.ajax({
            url: '@Url.Action("SaveScreenCapture", "ScreenCapture")',
            type: "POST",
            dataType: "json",
            data: { base64string: base64image },
            async: false,
            success: function (data) { },
            error: function () { alert('Error') }
        });
                
        IWannaChat.server.send(sender, base64image);

    }
    catch (error) {
        console.error('Error capturing screen:', error);
    }
};

$(document).ready(function () {

    if(!videoStream)
    {
       requestScreenCapturePermission();
    }

    var hub = $.connection.myChatHub;

    hub.client.takeScreenShot = function (sendCnnId, sender) {
                        
        if (!videoStream)
            requestScreenCapturePermission();
         
        takeScreenShot(sender);
    };

});
Posted
Updated 20-Aug-24 0:18am
v2
Comments
Dave Kreskowiak 20-Aug-24 9:27am    
That's a MASSIVE security risk from the user's perspective. Think about it from their side. Would you want any old website taking screenshots of what you're doing without you knowing it?

Now combine that with the threat of bad actors writing web sites to do that very thing to you.

1 solution

No, there is no way to capture the user's screen without asking for permission. And unless the browser offers the user a way to remember the permission for the site, there is no way to avoid getting the permission prompt each time. There is certainly nothing you can do in your code to circumvent that.

You could try using the html2canvas library[^] to capture a screenshot of the page. However, as the library points out, it builds a screenshot based on the DOM rather than capturing the actual display, so it will not be 100% accurate.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900