以设定的间隔跟踪 URL

tracking URL with set interval

首先post,大家好! 我正在为一项研究创建一个 chrome 扩展,需要跟踪用户浏览器交互,基本上是跟踪他们在执行信息搜索任务时访问的网页。

我阅读了许多关于 SO 的示例和各种文档(mozilla 开发人员、w3schools)关于跟踪 URL 的,例如散列更改、popstate 更改等,并发现

setInterval(function(){ console.log("Currently URL is " + window.location.href); }, 1000);

非常直截了当并且完全按照我的需要去做,尽管有点矫枉过正。它运行但如果我导航到第一页之外它似乎停止跟踪,这就是重点。我知道如果 window 关闭,间隔就会停止,但肯定不是这种情况?我也试过 document.URL 但并不开心。

回复评论

为了澄清,我想做的是观察人们在 google 搜索页面和他们查看的各种网站之间穿梭时与之交互的网站。我使用间隔的原因是因为它不仅仅是选项卡切换,尽管这是我需要注意的一种交互。我想,如果我每秒或半秒检查 URL 中的变化,那么它会识别用户何时浏览网站,而不是为超链接、选项卡等设置多个事件侦听器。我想,如果用户使用多个 tabs/windows 那么间隔就不起作用了?但在我的测试案例中我不是,所以我不知道为什么间隔会停止。我希望这有助于澄清。

感谢任何帮助。

SO外朋友的回答:

The reason it doesn't track beyond the first page is because the manifest specifies that contentScript should be loaded on every Google search result page. Therefore if the user navigates away from a Google result page the extension doesn't load the script and the extension doesn't do anything. You could move the interval location tracking to a separate background script?