You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NuxtJS自动重复添加特定脚本至body末尾问题求助

关于NuxtJS自动添加禁用右键/复制脚本的问题解答

首先明确:NuxtJS本身不会默认添加这类脚本,这不属于框架的原生行为,肯定是项目中的某个依赖、插件或自定义代码导致的。

你提到的重复添加的脚本内容如下:

<script type="text/javascript">
        document.oncontextmenu = null;
        document.onselectstart = null;
        document.ondragstart = null;
        document.onmousedown = null;
        document.body.oncontextmenu = null;
        document.body.onselectstart = null;
        document.body.ondragstart = null;
        document.body.onmousedown = null;
        document.body.oncut = null;
        document.body.oncopy = null;
        document.body.onpaste = null;
</script>

给你几个排查方向:

  • 检查项目依赖:看看有没有安装防盗版、内容保护类的npm包(比如一些禁止复制的插件),这类工具通常会动态注入这类脚本
  • 核对全局配置:打开nuxt.config.js(Nuxt3是nuxt.config.ts),检查head.scripts字段有没有配置相关内容;同时查看plugins目录下的所有全局插件,有没有在生命周期钩子中添加脚本的逻辑
  • 排查布局文件:检查layouts目录下的所有布局组件,尤其是默认布局,看看是否在mounted/created钩子中动态创建并插入了这个脚本
  • 页面组件检查:逐个查看页面组件的生命周期钩子,有没有单独添加这类限制逻辑
  • Nuxt3额外检查:如果是Nuxt3,还要看看app.vue中的逻辑,以及是否使用了某些会注入这类代码的Nuxt模块

快速定位来源的小技巧:
在浏览器控制台执行以下代码,当脚本被插入时会打印出对应的元素和调用栈,帮你找到是谁在添加:

document.body.addEventListener('DOMNodeInserted', (e) => {
  if (e.target.tagName === 'SCRIPT' && e.target.textContent.includes('oncontextmenu')) {
    console.log('脚本来源:', e.target);
    console.trace();
  }
});

内容的提问来源于stack exchange,提问作者Eliott

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 08:15:35