0

I am doing a task which requires me to find some websites that have their right-click disabled, and I want to write a simply Python script to scan a list of websites to find some samples. As there are many ways to disable right-click, I wonder if there is any way where I can use scripts to test if a website has right-click disabled?

4
  • This sounds like an XY Problem. What are you really trying to do? That is, if you found a website that disabled right-click (or intercepted right-click with their own handler), what would you do with it?
    – selbie
    Commented Jun 29 at 19:08
  • Oh, nothing special. I ran into some websites, e.g., e-commerce, artists, etc, and find some disabled the right-click, supposedly to disallow copying of the images. Out of curiosity, I want to find more samples and see what sites commonly disable right-click, which then spurs me to think how can I write a script to find such websites.
    – SamTest
    Commented Jun 29 at 19:30
  • "As there are many ways to disable right-click" - are there really?
    – Bergi
    Commented Jun 29 at 19:31
  • 1
    The only thing you can do is to load the page, run its javascript, fire a right click event, and see whether its default action (opening the browser context menu) is prevented.
    – Bergi
    Commented Jun 29 at 19:33

1 Answer 1

1

The dispatchEvent method will return true if the event hasn't been canceled and false otherwise.
You can thus fire an Event whose name is "contextmenu" with a cancelable flag and check the return value, (or the defaultPrevented property of the fired Event object).

const test = () => dispatchEvent(new Event("contextmenu", { cancelable: true }));

console.log("before blocking:", test());

addEventListener("contextmenu", (evt) => evt.preventDefault());

console.log("after blocking:", test());

But this would tell only if such an event handler was added on the window. It seems it's quite common to also use an ugly <body oncontextmenu="return false"> attribute event (don't do that please), and it is also very probable that only a few elements have their contextmenu disabled.

To catch these, you'll want to fire your event deeper in the tree, or at least on the target element, and with a bubbles flag to catch them.

Below is an example code telling you if any element has its contextmenu disabled. It will fire a bubbling event to all the leaf-elements in the tree (below the <body>) and stop at the first match. With a bit more work you could modify it to climb back to where the events where actually added, but that seem an overkill for this question.

// Now be bubble up the tree
const isMenuBlocked = (target) => !target.dispatchEvent(new Event("contextmenu", { cancelable: true, bubbles: true }));

// We could make it way more performant by using a TreeWalker
// but for a one shot, simplicity wins
const leaves = [...document.body.querySelectorAll("*:not(:has(*))")];

console.log("before blocking:", leaves.some((elem) => isMenuBlocked(elem)));

document.querySelector(".target").setAttribute("oncontextmenu", "return false");

console.log("after blocking:", leaves.some((elem) => isMenuBlocked(elem)));
<div>
  <span>You can right-click me</span>
</div>

<div>
  <span class="target"><i>But You can't right-click me</i></span>
</div>

Not the answer you're looking for? Browse other questions tagged or ask your own question.