28

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?

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
Gabor Magyar
  • 926
  • 2
  • 9
  • 20
  • You can always display them using canvas/images... – Pacerier Aug 18 '16 at 22:35
  • 5
    google brought me here, while searching for a way to DISABLE this hijacking of ctrl-f (now done for "convenience" in some sites to pull up a "search this site" box, disabling "search this page" browser functionality.) So, for others looking to "undo" this horrendously bad UX accessibility faux pas, one option is alt-e ctrl-f , OR try pressing ctrl-f ctrl-f (twice in rapid succession) see also https://superuser.com/questions/1038172/chrome-search-in-page-when-ctrlf-shortcut-is-hijacked – michael Jan 07 '20 at 03:59
  • This is awful ui. Don't hijack standardized browser hotkeys. Store the hidden data in javascript so it is not on the page at all and inject it when it is supposed to be visible. It then accomplishes your objective without being disruptive to expected functionality. Doing it the way that you are asking about is lazy and likely to anger your users. – mopsyd Oct 14 '21 at 18:35
  • @michael you can use a userscript with greasemonkey/violentmonkey/etc to negate this behavior on offending sites, but there isn't really a universal solution that won't also break a ton of other stuff. There should be a universal option, because your question is one of the most common and infuriating questions on the web. – mopsyd Oct 14 '21 at 18:36

10 Answers10

85

Code

window.addEventListener("keydown",function (e) {
    if (e.keyCode === 114 || (e.ctrlKey && e.keyCode === 70)) { 
        e.preventDefault();
    }
})
wukong
  • 2,430
  • 2
  • 26
  • 33
24

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 content declaration to inject the words. Find won't find them:

<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.

dmi3y
  • 3,482
  • 2
  • 21
  • 32
nickf
  • 537,072
  • 198
  • 649
  • 721
11

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.

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

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

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

thirtydot
  • 224,678
  • 48
  • 389
  • 349
4

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

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

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.

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

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • 4
    It is possible to override default browser shortcuts, and there are some more than valid times do it too... just take JSfiddle or Google Docs for example – Blowsie Aug 17 '11 at 13:10
  • I stand corrected. Still, I think the answer by @nickf is probably the best one. – Joachim Sauer Aug 17 '11 at 13:12
  • How this works ? https://community.particle.io/t/simple-solution-to-send-gmail-via-smtp/3549/21 – N.K May 28 '19 at 17:24
3

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

Code Lღver
  • 15,573
  • 16
  • 56
  • 75
Sean Gransee
  • 281
  • 1
  • 3
  • 9
2

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.

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.

Schroedingers Cat
  • 3,099
  • 1
  • 15
  • 33
  • 1
    It is possible to override default browser shortcuts, and there are some more than valid times do it too... just take JSfiddle or Google Docs for example – Blowsie Aug 17 '11 at 13:11
  • Yes true - as with most of these types of changes, there are valid reasons, and in those cases it is valid. What I meant is that for most browser-delivered applications, this is not really the way to go. In the specific case, I think making the application inaccessible to searches is a better option than trying to desiable searching in all browsers. – Schroedingers Cat Aug 17 '11 at 19:53
1

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:

$(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.

Hope this helps

totallyNotLizards
  • 8,489
  • 9
  • 51
  • 85
  • additional reading: keycode test page here: http://asquare.net/javascript/tests/KeyCode.html and a jquery ctrl plugin to help detect ctrl press aswell as a key press: http://www.gmarwaha.com/blog/2009/06/16/ctrl-key-combination-simple-jquery-plugin/ – totallyNotLizards Aug 17 '11 at 10:55
  • Note that even if this works, it will not prevent the user from going to the menu and select "find in page" from there. Or from using an alternative shortcut if he's using a locale that uses some other key-combination for searching. – Joachim Sauer Aug 17 '11 at 10:57
  • @Joachim: no web based technology that I'm aware of is capable of preventing the user using their browser menu, nor should it be. Using the plugin I linked to, you can easily catch the ctrl-f event and stop that doing what it normally does - but that is (as far as I know) all js can do in this case. To stop the find function from actually finding anything, nickF's answer would appear to be the best. – totallyNotLizards Aug 17 '11 at 13:02
  • I absolutely agree. I wasn't trying to say that your solution is a bad one, but I think that the "don't try to stop the from searching" approach in general is not as good as the "let them search but make it unfindable" one. – Joachim Sauer Aug 17 '11 at 13:07
0

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.

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

0

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

bezmax
  • 25,562
  • 10
  • 53
  • 84
  • It is possible to override default browser shortcuts, and there are some more than valid times do it too... just take JSfiddle or Google Docs for example – Blowsie Aug 17 '11 at 13:10
  • @Blowsie: That's exactly why I mentioned the word "safest" too. If you block the key by some way in online game - someone can still easily unblock the key by writing "javascript:unblockingCode..." in address bar. – bezmax Aug 17 '11 at 14:04
0

Render your website inside a <canvas> instead of using HTML.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
cfr6
  • 1
  • 1
  • 2
  • 2
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 01 '22 at 15:04