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 guessi开发者_如何学JAVAng 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?
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.
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();
});
If you really, absolutely have to do that - then there's a (really bad) solution: Render the page as an image.
精彩评论