[英]How to have Tampermonkey script run once per page load?
I'm trying to make a tampermonkey script that will once run once per page load, and will only run again once the page has been refreshed.我正在尝试制作一个tampermonkey脚本,该脚本将在每次页面加载时运行一次,并且只有在页面刷新后才会再次运行。 Here is my script so far:
到目前为止,这是我的脚本:
function(){
dostuff()
setInterval(location.reload.bind(location), 1000*60*60*24);
}
The problem is, tampermonkey will continuously run the script, so dostuff() will just keep running over and over again, which I don't want.问题是,tampermonkey 会不断运行脚本,所以 dostuff() 只会一遍又一遍地运行,这是我不想要的。 I've tried solutions such as GM_setValue and GM_getValue, but the solutions I found will only run once per install, which I don't want.
我尝试过诸如 GM_setValue 和 GM_getValue 之类的解决方案,但我发现的解决方案每次安装只会运行一次,这是我不想要的。
Here is a more complete sample of my code, which you can put in for https://stackoverflow.com (WARNING: This will crash your browser:):这是我的代码的更完整示例,您可以将其放入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();
}
}
I'm an absolute idiot.我是个十足的白痴。 The script was actually running once correctly, but my document.getElementsByClassName was calling recursively every time it opened a new tab.
该脚本实际上运行一次正确,但我的 document.getElementsByClassName 每次打开一个新选项卡时都会递归调用。 I just had to add an if statement to check for an element exclusively on the page before it opened and the problem was fixed.
我只需要添加一个 if 语句来在页面打开之前专门检查页面上的元素并解决问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.