Arantor

  • As powerful as possible, as complex as necessary.
  • Posts: 14,278
Quick little plugin
« on October 23rd, 2011, 01:59 AM »
OK, tonight's little project is one of those that I'm doing mostly under protest. Which begs the question why I'm doing it at all, and the answer is stupid user prevention.

I would generally rather inform users about the true facts of what they're trying to do, than pander to their ill-informed and frankly dangerous misinformation, but you know users: too many of them are a bit naive.

Tonight's project is one such example: disable right click.

It's useless as plugins go, because it's insanely trivial to bypass it, but you know how some users feel it's useful for protecting their content, and it's one more thing that I can publish, they can find and use and feel happier in its 'glory', or they can download it and then read the readme that covers it in detail. But hey, either way, I've done the best I can.
Re: Quick little plugin
« Reply #1, on October 23rd, 2011, 02:57 AM »
And I'm done. The only real hold-up was trying to figure out the best hook to put this on, and to make sure that I wrote a suitably worded readme file :niark:

(Oh, and while there are some cute scripts out there to do the job, most of the ones I saw briefly in passing do it the old-school way of testing for IE vs NS and expecting anything later to adhere to the NS method, mine however appears to be a bit safer, even if it does use jQuery.)

If you're curious or bored, here's what I came up with.

(click to show/hide)
Code: [Select]
$("body").bind("contextmenu", function (btn) { return false; }).mousedown(function (btn) { if (btn.which & 2 == 2) { btn.stopImmediatePropagation(); } });

It's compact :)

Any browser not capable of running jQuery is going to have a seriously bad time on the web anyway (we're talking pre IE6 here or fringe browsers no-one has heard of or cares about), so it's not really a problem.
Posted: October 23rd, 2011, 02:52 AM

Also, that's disgraceful, I have to write more lines of code to get that one line into the system than I do the code it adds :/
Posted: October 23rd, 2011, 02:52 AM

In other news, spoiler has a bug, presumably related to a related bug in SMF pre final (that may still be in final) where a 'you have hidden this post' message won't expand properly if it had code in it because the code's size is not reset properly.
When we unite against a common enemy that attacks our ethos, it nurtures group solidarity. Trolls are sensational, yes, but we keep everyone honest. | Game Memorial

Nao

  • Dadman with a boy
  • Posts: 16,082
Re: Quick little plugin
« Reply #2, on October 23rd, 2011, 06:50 AM »
Oh...

Have you tried expanding the spoiler with a jQ anim? Maybe it'll take care of that for you...
Re: Quick little plugin
« Reply #3, on October 23rd, 2011, 08:58 AM »
BTW, in jQuery, if you only need to return false, most of its binders support simply saying false instead of providing an anonymous function saying return false. Also, at least on my computer, mousedown(false) has always worked to disable text selection...
Untested but that should work...

Code: [Select]
$("body").bind("contextmenu", false).mousedown(false);

Arantor

  • As powerful as possible, as complex as necessary.
  • Posts: 14,278
Re: Quick little plugin
« Reply #4, on October 23rd, 2011, 01:58 PM »
I tried that but IE cried.

As for spoiler bug, I haven't tested it on Wedge but it is a known SMF bug.

Nao

  • Dadman with a boy
  • Posts: 16,082
Re: Quick little plugin
« Reply #5, on October 23rd, 2011, 04:07 PM »
How exactly did IE react...? Because I'm doing that in Wedge... :-/

Arantor

  • As powerful as possible, as complex as necessary.
  • Posts: 14,278
Re: Quick little plugin
« Reply #6, on October 23rd, 2011, 04:55 PM »
It gets upset with the context menu binding to false for some reason, it gives me "Object expected"

The binding on mousedown to false works though. Mind you, the code doesn't bind all mousedown, because I don't want to trap left clicks, and deliberately only indicates prevention of immediate propagation so that normal bubbling still works on left button, or middle button.

Nao

  • Dadman with a boy
  • Posts: 16,082
Re: Quick little plugin
« Reply #7, on October 23rd, 2011, 06:45 PM »
Maybe bind doesn't have the facility.

As for mousedown I'd trap all clicks in case user can select a text and then press ctrl+c but it's not important and could lead to other problems. So your code is the best for the job ;)

Arantor

  • As powerful as possible, as complex as necessary.
  • Posts: 14,278
Re: Quick little plugin
« Reply #8, on October 23rd, 2011, 09:17 PM »
I did consider doing that, but in the end I concluded that this plugin is pretty implicitly designed not to be used. It does exactly what it claims to do, which is disable right click. If you know your community is full of technologically disadvantaged users, that's cool, because it'll block them - but that's it.

You should read the readme, it makes it very clear that it does exactly what is requested, except that what is requested doesn't solve the problem, but honestly that's the point.

There's also a comment in the source that simply reads:
Code: [Select]
// If I can't convince you it's a bad idea, I might as well give you enough rope.

The plugin exists, primarily, because it's going to be requested. Instead of my going round and telling everyone it's a bad idea and that they're wasting their time, I've made the plugin. If they never read the readme, they're never going to care that they aren't much better off than they were before in terms of copy protection, and if they do, they're going to be a little educated :)

Dismal Shadow

  • Madman in a Box
  • Me: Who is Arantor? Cleverbot: It stands for time and relative dimensions in space.
  • Posts: 1,185
“I will stand on my ground as an atheist until your god shows up...If my irreligious bothers you much, and if you think everything I do is heresy to your god I don't care. Heresy is for those who believe, I don't. So, it isn't heresy at all!


   Jack in, Wedge,
   EXECUTE!

Arantor

  • As powerful as possible, as complex as necessary.
  • Posts: 14,278
Re: Quick little plugin
« Reply #10, on October 24th, 2011, 09:24 PM »
It will, but each person that gets it has a chance of being educated, which is better than no chance at all - there, it relies on people finding the tip and reading the whole thread.

Also, I find it interesting to note that he's copied that from DynamicDrive without the credit...

snoopy-virtual

  • Posts: 152
Re: Quick little plugin
« Reply #11, on October 25th, 2011, 03:07 PM »
I found interesting the answer by Paracelsus.

Apparently he has a site with content with a Creative Commons license, so instead of something to forbid the use of right click he would like something like a "warning" message (something like... "remember to add our link as source, since our content is protected bla bla bla...") but allowing the users to actually copy whatever they want.

I haven't thought too much about it, but I suppose that if you have an alert(message) and you change the return false to return true that would do the trick. Won't it?

Not that I would use anything like that in any of my sites though, but interesting anyway for future references. Maybe I could use that idea for some customers.

Arantor

  • As powerful as possible, as complex as necessary.
  • Posts: 14,278
Re: Quick little plugin
« Reply #12, on October 25th, 2011, 03:09 PM »
You could even have it remember after the first time, so that it only did it once per page rather than every click.

To change it, you would have to change the stopImmediatePropagation() call though, because if that fires, it still prevents event bubbling or chaining. It's just covering two different angles, both triggering the context menu as well as triggering an actual right mouse click.

snoopy-virtual

  • Posts: 152
Re: Quick little plugin
« Reply #13, on October 25th, 2011, 03:47 PM »
Quote
You could even have it remember after the first time, so that it only did it once per page rather than every click.
That would be a lot better yes.

I need to remember it the next time a customer ask me to do something like that in any site. Sometimes it doesn't matter how much you try to explain to them it's something stupid. They are the ones paying and they want it even if it sucks. At least having an alternative half useful is better than nothing.

Arantor

  • As powerful as possible, as complex as necessary.
  • Posts: 14,278
Re: Quick little plugin
« Reply #14, on October 25th, 2011, 03:50 PM »
*nods* Though as much as I wrote the thing, I made it abundantly clear in the readme etc. that the entire idea is flawed and how badly it is flawed.

In fact, let me grab the readme I have. It's not the first draft (which was far more abusive! :lol:) but it's what's in SVN and what I'm happy to ship the plugin with.
Quote
This plugin attempts to stop users right-clicking their mouse, in a half-hearted attempt to stop users copying. I only wrote this plugin because people will keep asking for it as though it magically protects your content.

It doesn't. It won't. It can't. Because no matter how clever you think you are, the web wasn't designed to be protected in that fashion, and it's trivial to bypass.

In an ideal world, you'd remove this plugin and have learned something, but if not, this plugin will use some JavaScript to attempt to block copying through the right mouse button. Turning off JavaScript will evade it, as will using the keyboard to copy things, also it should be defeated by mobile browsers that don't issue normal mouse 'click' things, and some browsers have a specific option to prevent you from doing what you think you need to do. This is not fixable, please don't ask.
On the flip side, it's about the simplest possible plugin that actually *does* something meaningful, as opposed to something that crudely hacks about with the main menu like my original demo plugin did.