Override Find Browser Feature

I am wondering if JavaScript can be used to intercept or prevent the user from using the Find feature of the browser to find text on the page. (Believe me, I have a good reason!) I assume that the answer is no, beyond the obvious Cmd / Ctrl + F interception.

A better solution would be to intercept the selection of text that the browser performs during the search. Is there any way to do this in any browser?

+8
source share
3 answers

Not do without a browser extension, I think, if at all. This is a process that is completely outside the scope of JavaScript.

+6

, Jquery.

JavaScript, :

        function disableFind() {
            var all = document.getElementsByTagName("*");
            var end = false;
            for(let idx in all){
                let currentElement = all[idx];
                let html = currentElement.innerHTML;
                if(!html) continue;
                let newHTML = "";
                for(var i = 0; i < html.length; i++) {
                    newHTML += html[i];
                    if (html[i] == '<') end = true;
                    if (html[i] == '>') end = false ;
                    if (end == false) {
                        newHTML += '<span style="position:absolute; left:-9999px;">.</span>';
                    }
                    if (html[i] == ' ') newHTML += ' ';   // insert a space if the current character is a space
                }
                currentElement.innerHTML = newHTML;
            }


        }

, CTRL/CMD + F :

 window.addEventListener("keydown", function(e){
     if(e.which == 70 && (e.ctrlKey || e.metaKey)) e.preventDefault(); 
  });
+2

If you really need to do this, then there is a (very bad) solution: display the page as an image.

+1
source

Source: https://habr.com/ru/post/1743884/


All Articles