防考试作弊切屏

21 篇文章 1 订阅
文章介绍了两种方法防止考试作弊,一是通过监听页面失焦和聚焦事件来计算切屏时间,当超过1分钟时自动交卷;二是监听页面显示隐藏状态,仅在全屏时记录。关键点在于确保事件处理函数在页面销毁时正确移除。
摘要由CSDN通过智能技术生成

防考试作弊切屏

方法一:监听页面失焦聚焦事件:防止任何操作

  • 监听考试页面失焦事件记录切出时间
  • 页面聚焦时累积记录切入时间,累积时间大于1分钟自动交卷并移除时间
  • 页面销毁移出事件
  • ***bug:必须把事件回调定义为方法,在销毁的时候才可以销毁
 mounted() {
     window.addEventListener('blur', this.blurDom);
     window.addEventListener('focus', this.focusDom);
  },
  destroyed() {
    console.log(1111111, '页面销毁')
    window.removeEventListener('blur', this.blurDom)
    window.removeEventListener('focus', this.focusDom)
  },

  methods: {
    blurDom() {
      this.start = Date.now()
      console.log(1111111, '失去焦点', this.start)
    },
    focusDom() {
      if (this.start == 0) return
      this.end = Date.now()
      this.hideTime += this.end - this.start
      console.log(1111111, '页面聚焦', this.end, this.hideTime)

      if (this.hideTime > 1 * 60 * 1000) {
        console.log('没机会了')
        this.hilarity(1)
        window.removeEventListener('blur', this.blurDom)
        window.removeEventListener('focus', this.focusDom)
      } else {
        this.$message.warning('切屏时间超过' + this.setTime + '分钟自动交卷');
      }
    },
 }

方法二:监听页面显示隐藏方法:只能监听页面被全部覆盖的情况,小窗口监听不到

  • 监听考试页面隐藏记录切出时间
  • 页面显示时累积记录切入时间,累积时间大于1分钟自动交卷并移除时间
  • 页面销毁移出事件
  • ***bug:必须把事件回调定义为方法,在销毁的时候才可以销毁
 mounted() {
     document.addEventListener("visibilitychange", this.watchDom);
  },
  destroyed() {
    console.log(1111111, '页面销毁')
    document.removeEventListener("visibilitychange", this.watchDom) 
  },

  methods: {
     watchDom() {
      if (document.visibilityState == 'hidden') {
        this.start = Date.now()
        console.log(1111111, 'start', this.start, this.hideTime)
      } else {
        this.end = Date.now()
        this.hideTime += this.end - this.start
        console.log(1111111, 'end', this.end, this.end - this.start, this.hideTime)

        if (this.hideTime > 1 * 60 * 1000) {
          this.hilarity(1)
          document.removeEventListener("visibilitychange", this.watchDom) 
        } else {
          this.$message.warning('切屏时间超过' + this.setTime + '分钟将自动交卷');
        }
      }
    },
 }
  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小曲曲

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值