admin管理员组文章数量:1332377
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?
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?
Share asked May 3, 2010 at 19:52 Trevor BurnhamTrevor Burnham 77.4k33 gold badges164 silver badges199 bronze badges 4- 5 Just curious, what is the reason? – Andy White Commented May 3, 2010 at 19:54
- It's for a web-based text editor. Ideally, I'd be able to intercept every Find and display my own Find & Replace dialogue. Having two different Finds is just confusing! There's more to it than that, but basically, the browser's Find would just be confusing in the context of this app. I guess I'll just intercept Cmd/Ctrl+F and pray that the user is using the standard keyboard shortcut. – Trevor Burnham Commented May 4, 2010 at 21:11
- intercepting Ctrl+F is slightly dangerous as it may be mapped differently in other languages, but I think it's your best bet. Because even if you managed to detect a "Find", how would you find out what the original search string was, when the search was case insensitive? And whether the "Find next / Find previous" button was used? Sounds like mission impossible to me. – Pekka Commented May 4, 2010 at 21:31
- I have never seen any web application - including all kinds of editors - that does this. But I admit, that I haven't even tried it (because I simply don't expect the find operation on Ctrl-/Cmd-f for a web app). If it can be done, and if it is demanded by many users, then you will most likely find a solution by looking at what the "big ones" are doing. – Chris Lercher Commented May 4, 2010 at 22:48
3 Answers
Reset to default 5 +100Not 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.
本文标签: javascriptOverride browser quotFindquot featureStack Overflow
版权声明:本文标题:javascript - Override browser "Find" feature - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742296174a2448767.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论