Javascript 是否可以禁用在页面中查找的 Ctrl + F?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7091538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 00:48:45  来源:igfitidea点击:

Is it possible to disable Ctrl + F of find in page?

javascripthtmlcssbrowser

提问by Gabor Magyar

I have a puzzle site and its an awful way of cheating. Its okay if only partially, but can it be done?
Something I had in mind was replacing the letters with images, but anything easier than that?

我有一个拼图网站,这是一种糟糕的作弊方式。如果只是部分是可以的,但是可以做到吗?
我想到的是用图像替换字母,但有什么比这更容易的吗?

回答by wukong

Code

代码

window.addEventListener("keydown",function (e) {
    if (e.keyCode === 114 || (e.ctrlKey && e.keyCode === 70)) { 
        e.preventDefault();
    }
})

回答by nickf

Rather than disable the Find function, you could make it so that Find won't find the words! One way to do this would be to use the CSS contentdeclaration to inject the words. Find won't find them:

与其禁用 Find 功能,不如让它让 Find 无法找到单词!一种方法是使用 CSScontent声明来注入单词。Find 不会找到它们:

<div class="word-foobar"></div>

.word-foobar:before {
    content: "Foobar";
}

You could quite easily write some Javascript to automatically generate all the necessary CSS rules for you, too.

您也可以很容易地编写一些 Javascript 来自动为您生成所有必要的 CSS 规则。

回答by thirtydot

You can disable the keyboard shortcut in most browsers (IE9, Firefox, Chrome, Opera), but you can't stop someone using Find by clicking it in the browser.

您可以在大多数浏览器(IE9、Firefox、Chrome、Opera)中禁用键盘快捷键,但您无法通过在浏览器中单击来阻止某人使用“查找”。

Here's some jQuery-powered JavaScript that does it:

这里有一些 jQuery 驱动的 JavaScript 可以做到这一点:

$(window).keydown(function(e){
    if ((e.ctrlKey || e.metaKey) && e.keyCode === 70) {
        e.preventDefault();
    }
});

Taken from http://caniuse.com/, where this feature regularlyirritates me. On that site, it's used to make CTRL+F do a custom search on the page, instead of disabling it completely.

取自http://caniuse.com/,此功能经常让我恼火。在该站点上,它用于使 CTRL+F 在页面上进行自定义搜索,而不是完全禁用它。

回答by Joachim Sauer

I don't think there's a way to disable the feature altogether and there shouldnot be a way to disable it.

我不认为有一种方法来完全禁用功能,并有应该没有一种方法来禁用它。

Howeveryou can ensure that some text will not be found by Ctrl+Fby writing it in away that the browser doesn't consider continous text.

但是,您可以确保某些文本不会被Ctrl+找到,方法F是将其写入浏览器不考虑连续文本的地方。

Using images is one approach that's relatively simply.

使用图像是一种相对简单的方法。

Alternatively you can randomize the letters and re-arrange them with some CSS magic (my CSS-fu is too weak to give an example, unfortunately). For example if you want to "hide" the word "hello", then write out "lehol" with each letter in a separate <div>and apply some CSS styles so that visually the letters will be in the correct order.

或者,您可以随机化字母并使用一些 CSS 魔法重新排列它们(不幸的是,我的 CSS-fu 太弱,无法举例)。例如,如果您想“隐藏”单词“hello”,那么将每个字母单独写出“lehol”<div>并应用一些 CSS 样式,以便在视觉上字母的顺序正确。

Note that this (and probably all other working solutions as well) will alsobreak copy-and-paste of the text.

请注意,这(也许所有其他的工作方案,以及)将将文本复制和粘贴。

回答by Sean Gransee

Here's a jQuery pluginthat does what you're looking for.

这是一个jQuery 插件,可以满足您的需求。

回答by Schroedingers Cat

I would presume that using keydown would enable this, however as a matter of principle, changing the way that a browser behaves is a bad idea. Although it is more of a pain for you to do, there are font replacement techniques that should make it easier.

我认为使用 keydown 可以实现这一点,但原则上,改变浏览器的行为方式是一个坏主意。虽然这对您来说更痛苦,但有一些字体替换技术可以让您更轻松。

If you do find a means of doing this, there is always a danger that someone will get around it. It is far better to write the page to work whatever than hack the browser.

如果您确实找到了这样做的方法,那么总是存在有人绕过它的危险。编写页面来工作比破解浏览器要好得多。

回答by totallyNotLizards

you can do it with javascript - this is only pseudocode (written in jQuery) as I'm not certain how to listen for both a ctrl AND an f, but you get the idea:

你可以用 javascript 来做——这只是伪代码(用 jQuery 编写),因为我不确定如何同时监听 ctrl 和 f,但你明白了:

$(body).keypress(function(e)
{
    if (e.keyCode===17)
    {
        //ctrl has been pressed, listen for subsequent F press (keyCode 70)
        //if next keyCode===70
        return false;
    }
});

Returning false like this will stop the browser doing anything when the keys are pressed, as far as I know. you could also use e.preventDefault();to try to prevent anything happening if return false;isn't enough.

据我所知,像这样返回 false 将阻止浏览器在按下键时执行任何操作。e.preventDefault();如果return false;还不够,您还可以用来尝试防止发生任何事情。

Hope this helps

希望这可以帮助

回答by bezmax

No. Replacing with images is the fastest and the safest way. (Unless you are willing to switch to Flash/Java Applet/etc.)

不。用图像替换是最快和最安全的方法。(除非你愿意切换到 Flash/Java Applet/等。)

回答by Samuel Bouffard

Why not use the Shadow DOM ? This will allow your content to be inaccessible by search engines or screen readers. And has the added bonus of having the exact same behavior for the user.

为什么不使用 Shadow DOM 呢?这将使搜索引擎或屏幕阅读器无法访问您的内容。并且具有为用户提供完全相同的行为的额外好处。

Take a look here for more info: Introduction to Shadow DOM

在这里查看更多信息:Shadow DOM 简介