我正在尝试跟踪网页上的用户活动,例如,如果用户在当前标签上,那么它应该返回TRUE,然后它应该跟踪鼠标活动,如左键单击次数,右键单击次数,用户是否滚动,鼠标按下的最大值,鼠标按下,鼠标左键,鼠标右键,用户在当前选项卡上的大部分时间以及从页面加载到页面上的第一次点击等的时间段。 我正在导入dom.document以引用网页,代码是:
import scala.scalajs.js.JSApp
import org.scalajs.dom
import dom.document
object test extends JSApp{
def main():Unit = {
val title=document.title.toString()
}
def appendPar(targetNode: dom.Node, text: String): Unit = {
val parNode = document.createElement("p")
val textNode = document.createTextNode(text)
parNode.appendChild(textNode)
targetNode.appendChild(parNode)
}
}
我正在建立一个scalajs项目。我该怎么做上述任务?请从这里引导我。提前谢谢。
答案 0 :(得分:2)
对于鼠标事件的一般操作,您可以从这开始:
dom.document.onmousedown = (e: dom.MouseEvent) => println("mouseup")
dom.document.onmouseup = (e: dom.MouseEvent) => println("mouseup")
dom.document.onmousemove = (e: dom.MouseEvent) => println("mousemove")
dom.document.onmouseout = (e: dom.MouseEvent) => println("mouseout")
dom.document.onmouseover = (e: dom.MouseEvent) => println("mouseout")
dom.document.onmousewheel = (e: dom.MouseEvent) => println("mousewheel")
如果您需要计算每个时间段内的点击次数(例如每秒):
var count = 0 //your counter
var n = 0 //number of times
dom.document.onmousedown = (e: dom.MouseEvent) => count += 1
// ms - duration of an interval in ms
def countMousedown(ms: Int): Unit ={
if (n == 100) return
n += 1
if(n != 0) println(count)
count = 0
dom.setTimeout(() => countMousedown(ms), ms)
}
countMousedown(1000)
获取坐标:
dom.document.onmousedown = (e: dom.MouseEvent) => {
val coords = (e.clientX, e.clientY)
println(s"x: ${coords._1}, y: ${coords._2}")
}