[英]How to have Tampermonkey script run once per page load?
我正在尝试制作一个tampermonkey脚本,该脚本将在每次页面加载时运行一次,并且只有在页面刷新后才会再次运行。 到目前为止,这是我的脚本:
function(){
dostuff()
setInterval(location.reload.bind(location), 1000*60*60*24);
}
问题是,tampermonkey 会不断运行脚本,所以 dostuff() 只会一遍又一遍地运行,这是我不想要的。 我尝试过诸如 GM_setValue 和 GM_getValue 之类的解决方案,但我发现的解决方案每次安装只会运行一次,这是我不想要的。
这是我的代码的更完整示例,您可以将其放入https://stackoverflow.com (警告:这会使您的浏览器崩溃:):
$(document).ready(function() {
'use strict';
if (!sessionStorage.getItem("session")) {
sessionStorage.setItem("session", "storage");
myFunction();
}
})();
function myFunction(){
if (sessionStorage.getItem("session"));
const galleryList = document.getElementsByClassName('question-hyperlink');
for(var y=0; y<galleryList.length;y++){
GM_openInTab(galleryList[y].href);
console.log(galleryList[y].href);
}
setTimeout(location.reload.bind(location), 1000*60*60*24);
};
function() {
//checks if value exists
if (!sessionStorage.getItem("___thing___")) {
//if it does not then set it and run the function
sessionStorage.setItem("___thing___", "");
yourFunction();
}
}
我是个十足的白痴。 该脚本实际上运行一次正确,但我的 document.getElementsByClassName 每次打开一个新选项卡时都会递归调用。 我只需要添加一个 if 语句来在页面打开之前专门检查页面上的元素并解决问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.