Tampermonkey-刷新后跟踪迭代

发布于 2025-02-11 03:18:51 字数 3148 浏览 1 评论 0 原文

我正在玩浏览器游戏,我遇到了这个脚本,但它并不是我想要的。

通常,此脚本仅运行一次,而不会在Tampermonkey上运行。脚本本身正在按预期工作,但每秒运行。

我试图使它连续两次运行,然后等待X弹出量的X弹出时间。

这是初始脚本。

    // ==UserScript==
// @name         Shinko BT Renamer
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        https://*/game.php*mode=incomings*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=devilicious.dev
// @grant        GM_setValue
// @grant        GM_getValue
// ==/UserScript==

var FORMAT = "#unit# from #coords# -> BT: #backtime#";
let refreshTime = 5; // time to refresh the page in minutes

function main()
{
    if(window.location.href.indexOf("subtype=attacks")==-1)
    {
        window.location.href = window.location.pathname+ "?screen=overview_villages&mode=incomings&mode=incomings&subtype=attacks";
        return;
    }

    if (typeof FORMAT === 'undefined')
        FORMAT = "#unit# from #coords# - #player# Backtime: #backtime#";
    FORMAT = FORMAT.replace(/#/g, "%");
    if(FORMAT.match(/%backtime%/g)!=null)
        FORMAT = ("`" + FORMAT + "`").replace(/%backtime%/, "%return%:${('00' + ((arrivalSeconds+parseInt('%duration%'.split(':')[2]))%60).toString()).slice(-2)}");

    console.log(FORMAT);
    $('input[name=label_format]').val(FORMAT);

    let tagRows =$("#incomings_table > tbody > tr").not(':first').not(':last');
    tagRows = $.grep(tagRows, (obj)=> $(".quickedit-label", obj)[0].innerText.match(/`.*`/g)!=null);

    if(tagRows.length >0)
        $.map(tagRows, (obj, key)=>setTimeout(()=>{
            evalTag(obj);
            /*if(key == tagRows.length -1)
                tagCommands();*/
        }, 250* key));
    else
        tagCommands();
}


function renameCommand(command, name)
{
    $('.rename-icon', command).click();
    $('.quickedit-edit input[type="text"]', command).val(name);
    $('.quickedit-edit input[type="button"]', command).click();
}

function evalTag(row)
{
    let command = $("td:nth-child(1)", row)[0];
    let arrivalTime = $("td:nth-child(6)", row)[0].innerText;
    let arrivalSeconds = parseInt(arrivalTime.split(":")[2]);
    let evalCommandTag;
    try
    {
        evalCommandTag = eval(command.innerText);
    }
    catch(err)
    {
        evalCommandTag = command.innerText;
    }
    renameCommand(command, evalCommandTag);
}

function tagCommands()
{
    if($("#incomings_form").length > 0)
    {
        selectAll($("#incomings_form")[0], true);
        $("[name='label']")[0].click();
    }
}

main();


I tried to do this but with no luck.
    
        for (i ; i < 2; i++){
        if(i<2){
            main();
            console.log(i);
        }else{
            setTimeout(function(){ location.reload(); },refreshTime * 60 * 1000);
            console.log(i);
        }
    }
    
    exec();

我最初认为的是。因此,我想运行两次,然后一个IF语句应该做到这一点,然后我做到了,可以跑一个cicle,但是当页面重新加载时,Tampermonkey失去了“ I”的

跟踪在Tampermonkey变量中i的值,但我无法使其起作用。任何输入都非常感谢。

谢谢!

var i = GM_getValue('i', 0);
    GM_setValue('i', i+1);

Im'm playing a browser game and i've came across this script but its not intended for the use I want.

Usually this script is ran only once and not on tampermonkey. The script itself is working as intended but is running everysecond.

I was trying to make it run twice in a row and then wait for x ammount of minutes defined by the var refreshTime .

This is the initial script.

    // ==UserScript==
// @name         Shinko BT Renamer
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        https://*/game.php*mode=incomings*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=devilicious.dev
// @grant        GM_setValue
// @grant        GM_getValue
// ==/UserScript==

var FORMAT = "#unit# from #coords# -> BT: #backtime#";
let refreshTime = 5; // time to refresh the page in minutes

function main()
{
    if(window.location.href.indexOf("subtype=attacks")==-1)
    {
        window.location.href = window.location.pathname+ "?screen=overview_villages&mode=incomings&mode=incomings&subtype=attacks";
        return;
    }

    if (typeof FORMAT === 'undefined')
        FORMAT = "#unit# from #coords# - #player# Backtime: #backtime#";
    FORMAT = FORMAT.replace(/#/g, "%");
    if(FORMAT.match(/%backtime%/g)!=null)
        FORMAT = ("`" + FORMAT + "`").replace(/%backtime%/, "%return%:${('00' + ((arrivalSeconds+parseInt('%duration%'.split(':')[2]))%60).toString()).slice(-2)}");

    console.log(FORMAT);
    $('input[name=label_format]').val(FORMAT);

    let tagRows =$("#incomings_table > tbody > tr").not(':first').not(':last');
    tagRows = $.grep(tagRows, (obj)=> $(".quickedit-label", obj)[0].innerText.match(/`.*`/g)!=null);

    if(tagRows.length >0)
        $.map(tagRows, (obj, key)=>setTimeout(()=>{
            evalTag(obj);
            /*if(key == tagRows.length -1)
                tagCommands();*/
        }, 250* key));
    else
        tagCommands();
}


function renameCommand(command, name)
{
    $('.rename-icon', command).click();
    $('.quickedit-edit input[type="text"]', command).val(name);
    $('.quickedit-edit input[type="button"]', command).click();
}

function evalTag(row)
{
    let command = $("td:nth-child(1)", row)[0];
    let arrivalTime = $("td:nth-child(6)", row)[0].innerText;
    let arrivalSeconds = parseInt(arrivalTime.split(":")[2]);
    let evalCommandTag;
    try
    {
        evalCommandTag = eval(command.innerText);
    }
    catch(err)
    {
        evalCommandTag = command.innerText;
    }
    renameCommand(command, evalCommandTag);
}

function tagCommands()
{
    if($("#incomings_form").length > 0)
    {
        selectAll($("#incomings_form")[0], true);
        $("[name='label']")[0].click();
    }
}

main();


I tried to do this but with no luck.
    
        for (i ; i < 2; i++){
        if(i<2){
            main();
            console.log(i);
        }else{
            setTimeout(function(){ location.reload(); },refreshTime * 60 * 1000);
            console.log(i);
        }
    }
    
    exec();

what i initially thought was. So, i want to run it twice then an If statement should do the trick and i made it, ran ok for one cicle but as the page reloads the tampermonkey loses track of the "i"

So i googled and i came across on storing the value of i in tampermonkey variable but i was not able to make it work. Any inputs are very much appreciated.

Thanks!

var i = GM_getValue('i', 0);
    GM_setValue('i', i+1);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤云独去闲 2025-02-18 03:18:51

查看LocalStorage,以保留页面刷新之间的数据 - 快速简便的解决方案。

示例:

在脚本的顶部:

let shinkoCnt = 0;
if (localStorage.getItem("shinkoCnt") !== undefined){
    shinkoCnt = localStorage.getItem("shinkoCnt");
}

将计数例程更改为:

for (shinkoCnt; shinkoCnt<2; shinkoCnt++){
   if(shinkoCnt<2){
      main();
      console.log(shinkoCnt);
   }else{
      setTimeout(function(){ location.reload(); },refreshTime * 60 * 1000);
      console.log(shinkoCnt);
   }
   localStorage.setItem("shinkoCnt", shinkoCnt);
}

参考:


https://wwwww.w3schools.com/jschools.com/jsref/jsref/jsref/met_storage_setitem.storage_setitem.asp
https://www.w3schools.com/jsref/jsref/api_storage.asp

Look into localStorage to preserve data between page refreshes - quick and easy solution.

Example:

At the top of your script:

let shinkoCnt = 0;
if (localStorage.getItem("shinkoCnt") !== undefined){
    shinkoCnt = localStorage.getItem("shinkoCnt");
}

Change your count routine to:

for (shinkoCnt; shinkoCnt<2; shinkoCnt++){
   if(shinkoCnt<2){
      main();
      console.log(shinkoCnt);
   }else{
      setTimeout(function(){ location.reload(); },refreshTime * 60 * 1000);
      console.log(shinkoCnt);
   }
   localStorage.setItem("shinkoCnt", shinkoCnt);
}

Reference:

https://www.w3schools.com/jsref/met_storage_getitem.asp
https://www.w3schools.com/jsref/met_storage_setitem.asp
https://www.w3schools.com/jsref/api_storage.asp

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文