Reputation: 77416
I'm wondering whether it's possible to use JavaScript to intercept or prevent the user from using the browser's "Find" feature to find text on the page. (Trust me, I have a good reason!) I'm guessing the answer is "no," beyond the obvious intercepting Cmd/Ctrl+F.
A second-best solution would be to intercept the text highlighting that the browser performs during a Find. Is there any way to do this, in any browser?
Upvotes: 8
Views: 2308
Reputation: 1723
To disable the effect pf the find feature, you can use this Jquery Plugin.
It can also be done with plain JavaScript, as follows:
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;
}
}
Additionally, you can prevent the default behavior of CTRL/CMD+F, with the following code:
window.addEventListener("keydown", function(e){
if(e.which == 70 && (e.ctrlKey || e.metaKey)) e.preventDefault();
});
Upvotes: 2
Reputation: 37778
If you really, absolutely have to do that - then there's a (really bad) solution: Render the page as an image.
Upvotes: 1
Reputation: 449415
Not without the help of a browser-specific extension I think, if at all. This is a process that is entirely outside the JavaScript context.
Upvotes: 5