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

技术问询:如何用App Script阻止测验/表单填写时打开新窗口及替代方案

针对Google平台表单/测验的窗口拦截方案

一、Google Apps Script的实现说明

原生Google表单无法通过Apps Script直接控制浏览器的窗口/标签页行为——这是浏览器同源安全策略的限制,Google的脚本环境没有权限干涉用户的浏览器原生操作。但如果是你自己用Apps Script构建的自定义表单/测验页面,可以结合前端JavaScript实现部分拦截功能。

自定义表单的拦截代码示例

1. 后端脚本(Code.gs)

function doGet() {
  return HtmlService.createHtmlOutputFromFile('FormPage')
    .setTitle('自定义测验')
    .setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
}

// 处理表单提交,示例:存入Google Sheets
function submitForm(formData) {
  const sheet = SpreadsheetApp.openById('你的表格ID').getSheetByName('提交记录');
  sheet.appendRow([formData.username, formData.response, new Date()]);
  return { success: true };
}

2. 前端页面(FormPage.html)

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <script>
      // 拦截页面跳转/关闭的提示
      window.addEventListener('beforeunload', (e) => {
        e.preventDefault();
        e.returnValue = '测验未完成,离开会丢失进度!';
      });

      // 拦截通过JS调用的新窗口打开
      window.open = () => {
        alert('测验期间禁止打开新窗口!');
        return null;
      };

      // 拦截右键菜单的新标签页选项
      document.addEventListener('contextmenu', (e) => {
        e.preventDefault();
        alert('测验期间禁用右键菜单的页面跳转功能!');
      });

      // 表单提交逻辑
      function submitQuiz() {
        const username = document.getElementById('username').value;
        const response = document.getElementById('response').value;
        
        google.script.run.withSuccessHandler((res) => {
          if (res.success) {
            alert('提交成功!');
            window.removeEventListener('beforeunload', () => {}); // 移除拦截
          }
        }).submitForm({ username, response });
      }
    </script>
  </head>
  <body>
    <h3>测验题目</h3>
    <label>姓名:<input type="text" id="username" required></label><br>
    <label>问题回答:<textarea id="response" rows="5" required></textarea></label><br>
    <button onclick="submitQuiz()">提交答案</button>
  </body>
</html>

注意:这种方法只能拦截通过脚本或右键菜单触发的新窗口行为,无法阻止用户手动点击浏览器的"+"号新建标签页——这属于浏览器原生功能,前端脚本无权限制。

二、其他可行解决方案

1. 原生Google表单的内置限制

Google表单自带一些防作弊功能,虽不能阻止开新标签,但能降低作弊风险:

  • 开启「限制一次性响应」,防止重复提交
  • 设置「测验计时器」,压缩作弊时间窗口
  • 开启「禁止查看成绩直到所有用户提交」,避免提前泄露答案

2. 专业测验工具替代

如果防作弊需求严格,建议使用专门的在线测验平台:

  • Google Classroom 测验:搭配Chrome扩展,能监控学生是否切换窗口(仅适用于教育域账号)
  • Quizizz/Kahoot!:提供全屏模式、禁止切窗口的防作弊选项(高级功能需付费)
  • ProctorU/Examity:专业远程监考工具,支持屏幕监控、窗口锁定,适合高风险测验

3. 浏览器扩展监控(受控环境)

在企业/学校的受控网络中,可以部署自定义浏览器扩展,实时监控用户的窗口行为——一旦检测到新建标签页,就触发警告或强制返回测验页面。但需要用户统一安装扩展,仅适用于内部场景。

4. 强制全屏模式

在自定义页面中加入全屏代码,增加用户切换窗口的门槛:

function enterFullscreen() {
  const elem = document.documentElement;
  if (elem.requestFullscreen) elem.requestFullscreen();
  else if (elem.webkitRequestFullscreen) elem.webkitRequestFullscreen();
  else if (elem.msRequestFullscreen) elem.msRequestFullscreen();
}

window.onload = enterFullscreen;

用户仍可手动退出全屏,但能大幅降低随意切换窗口的概率。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 10:35:35