首页 > 解决方案 > Javascript可以将变量的值保存到外部源吗?

问题描述

我会尽量简洁。

我是一名教师,我们目前都在网上教学。上学期我发现很多学生在我使用的在线作业系统中作弊,我知道这是因为我拒绝使用 Proctorio(它非常具有侵入性)。

在我编写到这个在线作业系统中的问题中,我想要的(就监考能力而言)是能够知道学生何时离开数字作业。在一些很棒的人的帮助下,我离这里越来越近了,但是,我还有一点路要走。

这是修改后的代码。事件侦听器工作正常,但我想按下页面上的那个按钮,它就是不工作。该按钮没有 ID 或名称,但 innerHTML 始终是“提交问题”。

<script type="text/javascript">
  var myWindow
  window.addEventListener("blur", function(){
    refocusWindow();
  })
  window.addEventListener("focus", function(){
    myWindow.removeEventListener("blur", refocus);
    myWindow.close();
  })
  function refocusWindow(){
    myWindow = window.open("", "myWindow");
    myWindow.document.write("<h1>During this assessment, you are not allowed any resources other than what is provided within the assessment. Going to another website, tab, page, or application is considered cheating. This event has been logged. Return to the assessment at this time or further actions will be taken!</h1>");
    myWindow.addEventListener("blur", refocus);
  }
  function refocus(){
    myWindow.close();
    setTimeout(function(){
      if (document.visibilityState != "visible" || document.hidden){
        refocusWindow();
      }
    },1)
  }
  function clickButton() {
    var buttons = document.getElementsByClassName("primary");
    for(var i = 0; i < buttons.length; i++) {
      if(buttons[i].type == 'button' && buttons[i].innerHTML == " Submit Question ") {
        buttons[i].click();
        break;
      }
    }
  }
</script>

按钮的类名是“primary”,这就是我搜索它的原因。

标签: javascript

解决方案


您有几个选项(evercookies、服务器等),我能想到的没有服务器的最佳选项是简单地阻止用户离开选项卡。除此之外,您可能必须添加一个系统,在关闭选项卡时通过电子邮件将结果发送给您,因为如果您不这样做,用户可以在知道问题的情况下关闭选项卡,然后作弊,然后只需键入在答案中。您还可以包括一个谷歌登录,以确保回答问题的是学生。

Anways,足够的谈话 - 这里是如何防止他们离开网站。请注意,这可能不适用于在线文本编辑器,因此您必须在计算机上创建一个 HTML 文件,复制并粘贴代码,然后在浏览器本地打开它(如果您根据您使用的内容,使用 windows 或其他东西)。要查看它的实际效果,只需离开选项卡。

<!DOCTYPE html>
<html>
<body>

Enter your name: <input type="text" id="fname">

<p>When you leave the input field, a function is triggered which transforms the input text to upper case.</p>

<script>
var myWindow
window.addEventListener("blur", function(){
refocusWindow()
})
window.addEventListener("focus", function(){
myWindow.removeEventListener("blur", refocus)
myWindow.close()
})

function refocusWindow(){
myWindow=window.open("", "myWindow")
myWindow.document.write("<h1>Please return to the HW - no cheating!</h1>")
myWindow.addEventListener("blur", refocus)
}

function refocus(){
myWindow.close()
setTimeout(function(){
if (document.hidden){
refocusWindow()
}
},1)
}
</script>

</body>
</html>

请注意,您还可以将全屏模式与此集成,这样无论何时keydown被触发或被click/mousedown/mouseup触发,用户都会进入全屏模式。

此外,您的部分代码可以缩短以更简洁。替换这个:

var buttons = document.getElementsByClassName("primary");
for(var i = 0; i < buttons.length; i++) {
  if(buttons[i].type == 'button' && buttons[i].innerHTML == " Submit Question ") {
    buttons[i].click();
    break;
     }
  }

有了这个:

[...document.querySelectorAll("button.primary")].some(function(element){if(element.innerHTML===" Submit Question "){element.click(); return true}})

查询选择器选择一个带有主类的按钮, [...] 将 HTMLCollection 转换为列表, .some 循环遍历元素,直到到达带有“提交问题”的 innerHTML 的元素,然后单击它。之后,它返回 true ,因此它停在那里并且不会循环其余部分。

[...document.querySelectorAll("button.primary")].some(function(element){if(element.innerHTML===" Submit Question "){element.click(); return true}})
<button class="primary">Don't select</button>
<button class="primary" onclick="alert(1)"> Submit Question </button>
<button class="primary" onclick="alert(1)"> Submit Question </button> <!--Don't select-->
<!--The bottom isn't selected, as there is only 1 alert.-->


推荐阅读