简体   繁体   English

如何禁止用户从DOM手动删除HTML元素?

[英]How to disallow user to remove HTML element manually from DOM?

In web development it's quite popular idea to create the element with fixed position and transparent background color which covers the whole page until the user interacts somehow (by accepting consent or paying for the service). 在Web开发中,创建具有固定位置和透明背景颜色的元素是非常流行的想法,该元素覆盖整个页面,直到用户以某种方式进行交互(通过接受同意或支付服务)。 Then the element is removed and user can use the web page. 然后删除该元素,用户可以使用该网页。

Here's the example: 这是一个例子:

在此输入图像描述

However user can remove the element manually, using dev tools in the browser. 但是,用户可以使用浏览器中的dev工具手动删除元素。

Is there any way to prevent it? 有什么办法可以预防吗? Is it possible to disallow some particular element to be deleted? 是否可以禁止某些特定元素被删除?

You can't. 你不能。

The browser belongs to the user and is completely under their control. 浏览器属于用户,完全由他们控制。

If you don't want to provide them with something before they accept terms, then don't send the something to the browser until they have. 如果您不想在接受条款之前向他们提供某些内容,那么在他们拥有之前不要将内容发送到浏览器

This is not possible. 这是不可能的。 You can make it harder for the user to get around it, but you can't prevent them from removing it from the page. 您可以让用户更难以绕过它,但是您无法阻止它们从页面中删除它。

I've seen websites use a lot of different solutions for making it harder to get around, ranging from locking the scrolling of the page to fully removing the content behind the pop up, but ultimately you can't prevent the user from modifying the DOM. 我已经看到网站使用了许多不同的解决方案,使其更难以解决,从锁定页面的滚动到完全删除弹出窗口后面的内容,但最终你无法阻止用户修改DOM 。

You need to add control server side. 您需要添加控制服务器端。 Your user can control the data they receive. 您的用户可以控制他们收到的数据。 Don't send the data if you don't want them to access data. 如果您不希望他们访问数据,请不要发送数据。

As far as it seems to be impossible to prevent the user from removing an element it is possible to detect the element removal. 就防止用户移除元素似乎是不可能的,可以检测元素移除。

The MutationObserver API allows us to detect changes of a particular DOM element. MutationObserver API允许我们检测特定DOM元素的更改。

Here's example by Jakub Jankiewicz 以下是Jakub Jankiewicz的例子

var in_dom = document.body.contains(element);
var observer = new MutationObserver(function(mutations) {
    if (document.body.contains(element)) {
        if (!in_dom) {
            console.log("element inserted");
        }
        in_dom = true;
    } else if (in_dom) {
        in_dom = false;
        console.log("element removed");
    }

});
observer.observe(document.body, {childList: true});

When the element is removed we can of course add it again or redirect to error page indicating that any manipulations of the page are illegal. 当元素被删除时,我们当然可以再次添加它或重定向到错误页面,指示对页面的任何操作都是非法的。

In particular the deletion of the element is easy with this React extension . 特别是使用此React扩展可以轻松删除元素。 If we use React then we can just wrap the element we want to prevent from being deleted in the component: 如果我们使用React,那么我们可以将我们想要阻止的元素包装在组件中:

import { WatchForRemoval } from 'react-mutation-observer';

...

<WatchForRemoval onRemoval={console.log.bind(null, 'Child removal triggered.')}>
  <div>
    Removing this element will trigger callback
  </div>
</WatchForRemoval>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM