[英]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.