简体   繁体   English

刷新后在网站上运行javascript

[英]Run javascript on website after refreshing

What I would like to achieve is this: refresh website run my javascript script on it (just as if I put it in Chrome console). 我要实现的是:刷新网站在其上运行我的javascript脚本(就像我将其放在Chrome控制台中一样)。

Question is: how to achieve it? 问题是:如何实现呢?

I can also use PHP for this (set server on my computer that wil be redirecting to the desired website and execute my javascript program on it). 我也可以为此使用PHP(在我的计算机上设置服务器,该服务器将重定向到所需的网站并在其上执行我的JavaScript程序)。

Some example of functionality I want to achieve: 我要实现的一些功能示例:

Go to stackoverflow.com -> click StackExchange (you can see it in the top left corner, it can be accessed by a querySelector() in JS) -> refresh stackoverflow.com -> click StackExchange again -> repeat... 转到stackoverflow.com->单击StackExchange(您可以在左上角看到它,可以通过JS中的querySelector()访问它)->刷新stackoverflow.com->再次单击StackExchange->重复...

如果要刷新单击DOM元素后的页面,可以将事件处理程序附加到该元素,然后在事件处理程序中执行location.reload()重新加载页面

 //When the document has loaded, call the function
    document.addEventListener("DOMContentLoaded", function(event) { 
        if(window.location.hash == "#page"){
            console.log("Page reloaded!");
        }else{
            console.log("Page has a new hit!");
        }
    });

You can use DOMContentLoaded event listener to solve your problem. 您可以使用DOMContentLoaded事件侦听器解决您的问题。

You can do something like this - 你可以做这样的事情-

var value = localStorage.getItem("logdata");

if(value != null) {
    eval(value);
    logdata();
}

And try to add logdata to localStorage from console. 并尝试从控制台将logdata添加到localStorage Example - localStorage.setItem("logdata", "var logdata = function(e) { console.log('data logged.'); }"); 示例localStorage.setItem("logdata", "var logdata = function(e) { console.log('data logged.'); }");

Now when you load the page, logdata function will be called and you can have your code executed. 现在,当您加载页面时,将调用logdata函数,并可以执行代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM