使脚本在每次重新加载页面时运行

时间:2020-02-05 23:05:27

标签: javascript

我希望每次重新加载页面时都运行此脚本,问题是我只知道如何在chrome的控制台窗口中运行它。我需要学习如何进行扩展,还是有另一种方法可以做到这一点?

var data = document.getElementsByTagName("td");
var newOpponent = document.getElementsByTagName("img");
newOpponent[32].id = "newOpponent";
document.getElementById("newOpponent");
document.getElementById("newOpponent").click();
//if(data[67].value > 20){
//    data.innerHTML = "<td style="width:40%; text-align:left; border:2px solid red;">23</td>";
//}

console.log("Strength: " + data[67].innerHTML);
console.log("Attack: " + data[70].innerHTML);
console.log("Defense: " + data[73].innerHTML);
console.log("Agility: " + data[76].innerHTML);
console.log("Stamina: " + data[79].innerHTML);

var strength = parseInt(data[67].innerHTML);
var attack = parseInt(data[70].innerHTML);
var defense = parseInt(data[73].innerHTML);
var agility = parseInt(data[76].innerHTML);
var stamina = parseInt(data[79].innerHTML);

if (attack > 20 || strength > 20 || defense > 20 || agility > 20 || stamina > 20){
    alert("One of the stats is too high, finding new opponent.");
}

if (Number.isInteger(attack) == true && attack > 20 ) {
    data[67].style.border = "thick solid red";
    document.getElementById("newOpponent").click();
    }

  

3 个答案:

答案 0 :(得分:1)

您正在寻找window.onload吗?

triggerOnChange(selector) {
    if (browser.isIOS) {
        browser.execute((elementSelector) => {
            let input = document.querySelector(elementSelector);
            let lastValue = '';
            let event = new Event('input', { bubbles: true });
            let tracker = input._valueTracker;
            if (tracker) {
                tracker.setValue(lastValue);
            }
            input.dispatchEvent(event);
        }, selector);
    }
}

答案 1 :(得分:1)

使用jQuery:

$(document).ready(function() {
    // Here your code!
});

jQuery learning site

jQuery API site

答案 2 :(得分:0)

如果我很了解您的需求,那么您可以在该页面上运行自定义脚本。如果为true,则可以将扩展名Tampermonkey Chrome用于您的对象,以便在页面上注入自定义脚本

https://chrome.google.com/webstore/detail/tampermonkey/dhdgffkkebhmkfjojejmpbldmpobfkfo?hl=fr