Detect opening new browser window on mouse button 3 click or ctrl (shift) + mouse click on link from PDF file

Petr Binko 41 Reputation points
2021-12-01T13:07:19.683+00:00

Hi,
we want to use WebView2 component in out application for web browsing and also for as PDF viewer. Problem is, we have specific configuration of machines, on which our application is running and we need to prevent users to reach file system. For this reason we need to prevent opening dialog windows from WebView (because they have context menu with save as option, which leads to file system). This is no problem when browsing web, because we can detect event NewWindowRequested, but when viewing PDF file which contains url link there is no event raised (or we did not find one) when clicking link with mouse 3 button or ctrl (shift) + mouse button. Is there some way to detect this situation and prevent opening new window?

Microsoft Edge Microsoft Edge development
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2021-12-02T07:50:58.917+00:00

    Hi @Petr Binko ,

    What do you mean by mouse 3 button, is it the middle button (scroll wheel) of the mouse?

    As far as I know, if you need to disable ctrl+left-click to open a new tab, you can try to implement it through javascript, a simple example:

                let links = document.getElementsByTagName('a');  
                    for (let i = 0; i < links.length; i++) {  
                        links[i].addEventListener('click', function (e) {  
                            if (e.ctrlKey || e.shiftKey) {  
                                e.preventDefault();  
                                return false;  
                            }  
                        });  
                    }  
    

    If I understand correctly, add auxclick event can achieve a similar feature to prevent middle click event, like this:

              links[i].addEventListener('auxclick', function (e) {  
                    if (e.which === 2 || e.button ===1) {  
                        e.stopPropagation();  
                        e.preventDefault();  
                        return false;  
                    }  
                })  
    

    Hope this can help.

    Best regards,
    Xudong Peng


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.